-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdventOfCodeService.ts
More file actions
43 lines (43 loc) · 1.5 KB
/
AdventOfCodeService.ts
File metadata and controls
43 lines (43 loc) · 1.5 KB
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
import { AocPrivateLeaderboard, LeaderboardConfig } from "src/contracts";
import https from "https";
import url from 'url';
export default class AdventOfCodeService {
year: number;
constructor(year: number) {
this.year = year;
}
async getLeaderboardAsync(config: LeaderboardConfig): Promise<AocPrivateLeaderboard> {
if (!config.id) {
return {
event: `${this.year}`,
members: {},
owner_id: '',
};
}
var result = await this.getFromUrlAndHeadersAsync<AocPrivateLeaderboard>(`https://adventofcode.com/${this.year}/leaderboard/private/view/${config.id}.json`, { "Cookie": `session=${config.session_id}` });
result.name = config.name;
return result;
}
getFromUrlAndHeadersAsync<T>(uri: string, headers: {
[key: string]: string;
}): Promise<T> {
return new Promise<T>((resolve, reject) => {
const { host, path } = url.parse(uri);
https.get({ host, headers, path }, function (res) {
var body = "";
res.setEncoding('utf8');
res.on("data", data => {
body += data.toString();
});
res.on("end", () => {
try {
resolve(JSON.parse(body));
}
catch (error) {
reject(error);
}
});
}).end();
});
}
}