-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgnig.ts
135 lines (120 loc) · 3.51 KB
/
pgnig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { ChartData, fixContinuousReadings } from "./utils.js";
const _LOGIN = process.env.PGNIG_LOGIN;
const _PASSWORD = process.env.PGNIG_PASSWORD;
if (!_LOGIN) throw new Error(`Missing PGNIG_LOGIN!`);
const LOGIN = _LOGIN;
if (!_PASSWORD) throw new Error(`Missing PGNIG_PASSWORD!`);
const PASSWORD = _PASSWORD;
export async function getData() {
const loginData = await login();
const meterData = await readMeter(loginData.Token);
const values = [
...fixContinuousReadings(
meterData.MeterReadings.map((el) => el.Value).reverse()
),
].reverse();
return {
gasConsumed: meterData.MeterReadings.map((el, idx) => {
return [new Date(el.ReadingDateUtc).getTime(), values[idx]] as const;
}).reverse() as ChartData,
};
}
const commonHeaders = {
accept: "application/json",
"accept-language": "en-US,en;q=0.9,pl;q=0.8",
"content-type": "application/json",
"sec-ch-ua":
'"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"macOS"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"Referrer-Policy": "strict-origin-when-cross-origin",
};
async function login() {
const res = await fetch("https://ebok.pgnig.pl/auth/login?api-version=3.0", {
headers: {
...commonHeaders,
Referer: "https://ebok.pgnig.pl/",
},
body: `{"identificator":"${LOGIN}","accessPin":"${PASSWORD}","rememberLogin":true,"DeviceId":"54c80d5e64dc61f0fcf764a060012e35","DeviceName":"Chrome wersja: 109.0.0.0<br>","DeviceType":"Web"}`,
method: "POST",
});
if (!res.ok) {
console.error(`login`, res.statusText);
console.error(`login`, await res.json());
throw new Error(res.statusText);
}
const json = await res.json();
return json as LoginResponse;
}
async function readMeter(token: string) {
const res = await fetch(
"https://ebok.pgnig.pl/crm/get-all-ppg-readings-for-meter?pageNumber=1&pageSize=36&idPpg=8018590365500030056003&api-version=3.0",
{
headers: {
...commonHeaders,
authtoken: token,
Referer: "https://ebok.pgnig.pl/odczyt",
},
method: "GET",
}
);
if (!res.ok) {
console.error(`readMeter`, res.statusText);
console.error(`readMeter`, await res.json());
throw new Error(res.statusText);
}
const json = await res.json();
return json as GetAllReadingsResponse;
}
interface LoginResponse {
DateExpirationUtc: string;
Token: string;
AutomaticLoginToken: string;
NewUser: boolean;
IsActiveTransfer: boolean;
HasBetaAccess: boolean;
ForceChangePasswordByInvalidPolicy: boolean;
ForceChangeUserNameToEmail: boolean;
Code: number;
Message?: any;
DisplayToEndUser: boolean;
EndUserMessage?: any;
TokenExpireDate: string;
TokenExpireDateUtc: string;
}
export interface GetAllReadingsResponse {
MeterReadings: ReadonlyArray<{
Status: string;
CollectionPointAddress: {
Ulica: string;
NrBudynku: string;
NrLokalu: string;
KodPocztowy: string;
Miejscowosc: string;
};
ReadingDateLocal: string;
ReadingDateUtc: string;
PpId: string;
Value: number;
Value2?: any;
Value3?: any;
ClientNumber: string;
MeterNumber: string;
PhoneNumber: string;
RegionCode: string;
PeselNip: string;
Wear: number;
Type: string;
Color: string;
AgreementName: string;
}>;
Code: number;
Message?: any;
DisplayToEndUser: boolean;
EndUserMessage?: any;
TokenExpireDate: string;
TokenExpireDateUtc: string;
}