Skip to content

Commit 3dd770c

Browse files
authored
Merge pull request #15 from alphatrl/silent-update
Add an option to scrape without sending push notification to firebase
2 parents 7af29a4 + 865ca45 commit 3dd770c

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"scripts": {
1010
"start": "node index.js",
11-
"start-dev": "node -r dotenv/config index.js dotenv_config_path=.env"
11+
"start-dev": "node -r dotenv/config index.js dotenv_config_path=.env",
12+
"silent:scrape": "node src/scrape.js"
1213
},
1314
"dependencies": {
1415
"dotenv": "^8.2.0",

src/scrape.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import puppeteer from 'puppeteer'
2+
import fs from 'fs';
3+
import path from 'path';
4+
import {spawnSync} from 'child_process';
5+
6+
import MODULES from '../modules.js';
7+
import {getJSON, getJSONLocal} from './utils/networking.js';
8+
import {default as verifyList} from './utils/compareList.js';
9+
10+
const isProduction = process.env.NODE_ENV === 'production';
11+
12+
if (!fs.existsSync('temp')) {
13+
fs.mkdirSync('temp')
14+
}
15+
16+
const main = async () => {
17+
18+
const browser = await puppeteer.launch({
19+
headless: isProduction,
20+
args: isProduction ? ['--no-sandbox'] : [],
21+
})
22+
23+
for (const module of MODULES) {
24+
const module_name = Object.keys(module);
25+
const filename = path.join('temp', `${module_name}.json`);
26+
const url = isProduction ? `https://alphatrl.github.io/sg-lottery-scraper/${module_name}.json` : `${path.resolve()}/${filename}`;
27+
const backup_list = isProduction ? await getJSON(url) : await getJSONLocal(url);
28+
29+
if (fs.existsSync(filename)) {
30+
fs.unlinkSync(filename)
31+
}
32+
33+
var [lottery_list, is_different_list] = await module[module_name](browser).then(async (new_list) => {
34+
return await verifyList(new_list, backup_list);
35+
});
36+
37+
fs.writeFileSync(filename, JSON.stringify(lottery_list, null, isProduction ? 0 : 2));
38+
}
39+
40+
await browser.close();
41+
42+
// push json to github
43+
if (process.env.GITHUB_TOKEN) {
44+
spawnSync(
45+
'dpl',
46+
[
47+
'--provider=pages',
48+
'--committer-from-gh',
49+
`--github-token=${process.env.GITHUB_TOKEN}`,
50+
`--repo=${process.env.GITHUB_REPO}`,
51+
'--local-dir=temp',
52+
],
53+
{
54+
stdio: 'inherit',
55+
}
56+
)
57+
}
58+
59+
return null
60+
}
61+
62+
// main thread
63+
process.on('uncaughtException', function (err) {
64+
console.log(err);
65+
})
66+
main()

0 commit comments

Comments
 (0)