-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.mjs
93 lines (83 loc) · 2.2 KB
/
fetch.mjs
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
import fetch from 'node-fetch';
import dotenv from 'dotenv';
import client, { dbName, collectionName } from "./mongo.mjs";
import logger from './logger.mjs';
dotenv.config();
const fetchLeaderboard = async (id, year) => {
try {
const session = process.env.SESSION;
const url = `https://adventofcode.com/${year}/leaderboard/private/view/${id}.json`;
logger.info(`fetching ${year}/${id}...`);
const response = await fetch(url, {
headers: {
"Cookie": `session=${session}`,
},
});
if (response.status !== 200) {
throw new Error(`Invalid session: ${response.status}`);
}
logger.info(`fetched ${url}`);
logger.info(`parsing response`);
const json = await response.json();
return json;
} catch (e) {
if (e instanceof SyntaxError) {
e = Error("Invalid session");
}
logger.error(e);
throw e;
}
}
const saveLeaderboard = async (id, year) => {
try {
logger.info("fetching leaderboard...");
const json = await fetchLeaderboard(id, year);
logger.info("done fetching, connecting to db...");
await client.connect();
const db = client.db(dbName);
const collection = db.collection(collectionName);
logger.info("connected, checking if exists...");
const exists = await collection.findOne({ id, year });
let result;
if (exists) {
logger.info("exists, updating...");
result = await collection.updateOne({
id,
year,
}, {
$set: {
json,
updated: new Date(),
},
});
} else {
logger.info("does not exist, inserting...");
result = await collection.insertOne({
id,
year,
json,
updated: new Date(),
inserted: new Date(),
});
}
} catch (e) {
logger.error(e);
throw e;
} finally {
logger.info("closing connection");
await client.close();
}
logger.info("done");
}
const main = async () => {
const year = process.argv[2];
const id = process.argv[3];
if (!year || !id) {
logger.error("Usage: node fetch.mjs <year> <id>");
return;
}
await saveLeaderboard(2238062, 2022);
process.exit(0);
}
export { fetchLeaderboard, saveLeaderboard };
// main();