-
Notifications
You must be signed in to change notification settings - Fork 0
/
xedule-scraper.js
157 lines (138 loc) · 3.89 KB
/
xedule-scraper.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
const fs = require("fs");
const keytar = require("keytar");
const cron = require("node-cron");
const path = require("path");
const puppeteer = require("puppeteer");
const util = require("util");
const yargs = require("yargs");
cron.schedule(
"0 0 * * 1",
async () => {
const path = await getICS();
console.log(`downloaded file to ${path}`);
process.exit(0);
},
{ timezone: "Europe/Amsterdam" }
);
/**
* Gets .ics file
* @returns {Promise<string>} the path of the downloaded file
*/
async function getICS() {
const [{ account: username, password }] = await keytar.findCredentials(
"xedule-scraper"
);
const browser = await puppeteer.launch({
headless: process.env.NODE_ENV === "production",
});
const page = await browser.newPage();
await page.goto("https://sa-nhlstenden.xedule.nl");
try {
await page.type("#userNameInput", username);
await page.type("#passwordInput", password);
await page.click("#submitButton");
} catch {
await page.authenticate({
username,
password,
});
}
await page.waitForSelector(".search-toggle");
await page.click(".search-toggle");
const classesPath = path.resolve(__dirname, "classes.json");
const json = await util.promisify(fs.readFile)(classesPath);
const classes = JSON.parse(json);
for (const text of classes) {
await page.type("#rosterEntitiesSelector-entity-filter", text);
await page.keyboard.down("Enter");
await page.waitForSelector("#rosterEntitiesSelector-entity-filter");
}
await page.click(".search-toggle");
await page.click("li.k-current-view>a.k-link");
const downloadPath = path.resolve(__dirname, "downloads");
if (!(await util.promisify(fs.exists)(downloadPath)))
await util.promisify(fs.mkdir)(downloadPath);
await page._client.send("Page.setDownloadBehavior", {
behavior: "allow",
downloadPath,
});
await page.click(".k-state-default.k-view-ics");
let fileName;
while (!fileName || fileName.endsWith(".crdownload")) {
await new Promise(resolve => setTimeout(resolve, 100));
[fileName] = await util.promisify(fs.readdir)(downloadPath);
}
await browser.close();
const filePath = path.resolve(downloadPath, fileName);
return filePath;
}
/**
* Sets the classes
* @returns {Promise<string>} the path of the rosters file
*/
async function setClasses(classes) {
const classesPath = path.resolve(__dirname, "classes.json");
const json = JSON.stringify(classes, "", 2);
await util.promisify(fs.writeFile)(classesPath, json);
return classesPath;
}
yargs
.scriptName("xedule-scraper")
.usage("$0 <command> [args]")
.command(
"login [username] [password]",
"authenticate",
args => {
args.positional("username", {
type: "string",
describe: "your nhl-stenden username",
});
args.positional("password", {
type: "string",
describe: "your nhl-stenden password",
});
},
async ({ username, password }) => {
if (!username) {
console.error("no username");
process.exit(1);
}
if (!password) {
console.log("no password");
process.exit(1);
}
await keytar.setPassword("xedule-scraper", username, password);
process.exit(0);
}
)
.command(
"fetch-roster",
"fetches the current roster",
() => {},
async () => {
const path = await getICS();
console.log(`downloaded file to ${path}`);
process.exit(0);
}
)
.command(
"set-classes [classes]",
"sets the classes to be scraped",
args => {
args
.positional("classes", {
type: "string",
describe: "the classes you want to scrape",
})
.array("classes");
},
async ({ classes }) => {
if (!classes) {
console.log("no classes");
process.exit(0);
}
await setClasses(classes);
process.exit(0);
}
).argv;