-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.js
More file actions
111 lines (91 loc) · 2.5 KB
/
update.js
File metadata and controls
111 lines (91 loc) · 2.5 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
'use strict';
const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
const getHTML = require('./util/fetch');
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);
}
getHTML('https://www.chessgames.com/perl/gamesoftheday').then(data => {
const $ = cheerio.load(data);
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 || [])
};
wFile('pgn.json', JSON.stringify(pgn, null, ' '), () => {
console.log(`Record data(s): ${ret.length}`);
});
} else {
console.log('No need to update.')
}
});