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