-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_utilities.js
45 lines (42 loc) · 1.74 KB
/
build_utilities.js
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
export function parse_time(time) {
let split = time.split(':').map(Number);
return split[0]*60+split[1];
}
var sofiatraffic_session_cookie;
var sofiatraffic_xsrf_token;
async function fetch_tokens() {
let cookies_request = await fetch('https://sofiatraffic.bg/bg/public-transport', {"method": "HEAD"});
let cookies = cookies_request.headers.getSetCookie();
sofiatraffic_xsrf_token = decodeURIComponent(cookies[0].split(';')[0].split('=')[1]);
sofiatraffic_session_cookie = decodeURIComponent(cookies[1].split(';')[0].split('=')[1]);
}
export async function fetch_data_from_sofiatraffic(url, body={}) {
if(!sofiatraffic_session_cookie) {
await fetch_tokens();
}
return fetch(url, {
"method": "POST",
"credentials": "include",
"headers": {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0",
"Accept": "application/json, text/plain, */*",
"Accept-Language": "en-GB,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br, zstd",
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/json",
"X-XSRF-TOKEN": sofiatraffic_xsrf_token,
"Cookie": `sofia_traffic_session=${sofiatraffic_session_cookie}`,
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Priority": "u=1",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"Referrer": "https://www.sofiatraffic.bg/bg/public-transport",
"TE": "trailers"
},
"referrer": "https://www.sofiatraffic.bg/bg/public-transport",
"body": JSON.stringify(body),
"mode": "cors"
});
}