-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate2.js
More file actions
143 lines (114 loc) · 3.09 KB
/
update2.js
File metadata and controls
143 lines (114 loc) · 3.09 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
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
'use strict';
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
const cheerio = require('cheerio');
const wFile = require('./util/wFile');
const dateFormat = require('./util/dateFormat');
const now = new Date();
const mapMonth = {
'Jan': 0,
'Feb': 1,
'Mar': 2,
'Apr': 3,
'May': 4,
'Jun': 5,
'Jul': 6,
'Aug': 7,
'Sep': 8,
'Oct': 9,
'Nov': 10,
'Dec': 11
};
let prePGN = {};
if (fs.existsSync('./pgn.json')) {
try {
prePGN = require('./pgn.json');
} catch (e) {
prePGN = {};
}
}
const ret = [];
const update = prePGN.update;
const curDate = dateFormat(now);
const yesterday = dateFormat(now.getTime() - 24 * 60 * 60 * 1000);
const args = process.argv.slice(2);
const yFlag = args.indexOf('-y') === -1;
if (curDate === update || (yFlag && yesterday === update)) {
console.log(`Ignored ${update}, quit.`);
process.exit(0);
}
function sleep(time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, time);
});
}
async function updatePGNJson() {
const browser = await puppeteer.launch({
headless: false,
});
let page;
const pages = await browser.pages();
if (pages[0]) {
page = pages[0]
} else {
page = await browser.newPage();
}
await page.goto('https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=2286427');
const result = await page.evaluate(async function () {
return await fetch('https://www.chessgames.com/perl/gamesoftheday').then(res => res.text());
});
const $ = cheerio.load(result);
const $list = $('a[href^="/perl/chessgame"]');
let nowYear = now.getFullYear();
let prevMonth = now.getMonth();
let diffYear = 0;
let yearFlag = false;
$list.each((index, item) => {
const $item = $(item);
const curDate = $item.prevAll('font').eq(-1).html();
const curNote = $item.prevAll('font').eq(-2).html().replace(/<\/?b>/g, '');
const curHref = $item.attr('href');
const curText = $item.html();
const curId = curHref.split('=')[1];
const tempArr = curDate.split('-');
const curMonth = mapMonth[tempArr[0]];
const curMonth2 = curMonth < 9 ? '0' + (curMonth + 1) : (curMonth + 1);
let curYear = nowYear;
// [FIX] some special date is not exist.
if (curMonth === 11 && prevMonth === 0 && !yearFlag) {
diffYear++;
yearFlag = true;
} else if (prevMonth === 11) {
yearFlag = false;
}
const curDate2 = `${curYear - diffYear}-${curMonth2}-${tempArr[1]}`;
if (update === curDate2) {
return false;
}
ret.push({
date: curDate2,
year: (curText.match(/\d{4}$/) || [])[0] || 0,
players: curText.split(',')[0],
link: curHref,
id: curId,
note: curNote
});
prevMonth = curMonth;
});
if (ret.length > 0) {
const pgn = {
update: ret[0].date,
list: ret.concat(prePGN.list || [])
};
await wFile('pgn.json', JSON.stringify(pgn, null, ' '));
console.log(`Record data(s): ${ret.length}`);
} else {
console.log('No need to update.');
}
await sleep(10000);
await browser.close();
}
updatePGNJson();