-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_tdoll_db.js
126 lines (96 loc) · 3.47 KB
/
update_tdoll_db.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
const axios = require('axios');
const path = require('path');
const fs = require('fs');
const cheerio = require('cheerio');
const TARGET_URL = 'https://iopwiki.com/wiki/T-Doll_Index';
const TARGET_TEMP_DIR = path.join(__dirname, 'temp');
const SOURCE_HTML_NAME = 'index.html';
const DB_NAME = 'tdoll_db.json';
if (!fs.existsSync(TARGET_TEMP_DIR)) {
fs.mkdirSync(TARGET_TEMP_DIR);
}
const RARITY_STARS_PREFIX = 'doll-rarity-';
const RARITY_CLASS_PREFIX = 'doll-classification-';
const getTdollInfo = (element) => {
const _$1 = cheerio.load(element);
const attributes = element.attributes;
let rarityStars = '';
let rarityClass = '';
attributes.forEach(attr => {
if (attr.name === 'class') {
const classVal = attr.value;
const clsList = classVal.split(' ');
clsList.forEach(cls => {
if (cls.startsWith(RARITY_STARS_PREFIX)) {
rarityStars = cls.split(RARITY_STARS_PREFIX)[1];
} else if (cls.startsWith(RARITY_CLASS_PREFIX)) {
rarityClass = cls.split(RARITY_CLASS_PREFIX)[1];
}
})
}
});
const dataOptions = _$1.root().data();
console.log(attributes);
console.log(dataOptions);
const name = _$1('span.name').text();
const index = parseInt(_$1('span.index').text().trim());
const classAttr = attributes.filter(attr => attr.name === 'class')[0].value;
const rarityStarsSrc = classAttr;
const rarityClassSrc = classAttr;
let targetClass = '';
if (rarityClassSrc.includes('doll-classification-SMG')) {
targetClass = 'SMG';
} else if (rarityClassSrc.includes('doll-classification-MG')) {
targetClass = 'MG';
} else if (rarityClassSrc.includes('doll-classification-AR')) {
targetClass = 'AR';
} else if (rarityClassSrc.includes('doll-classification-HG')) {
targetClass = 'HG';
} else if (rarityClassSrc.includes('doll-classification-RF')) {
targetClass = 'RF';
} else if (rarityClassSrc.includes('doll-classification-SG')) {
targetClass = 'SG';
}
let targetStars = '';
if (rarityStarsSrc.includes('doll-rarity-6')) {
targetStars = '6';
} else if (rarityStarsSrc.includes('doll-rarity-5')) {
targetStars = '5';
} else if (rarityStarsSrc.includes('doll-rarity-4')) {
targetStars = '4';
}
else if (rarityStarsSrc.includes('doll-rarity-3')) {
targetStars = '3';
}
else if (rarityStarsSrc.includes('doll-rarity-2')) {
targetStars = '2';
}
else if (rarityStarsSrc.includes('doll-rarity-EXTRA')) {
targetStars = 'Extra';
}
const resItem = {
name,
id: isNaN(index) ? -1 : index,
class: rarityClass,
star: rarityStars
};
console.log('resItem', resItem);
return resItem;
}
const saveDataToFile = (jsonData) => {
fs.writeFileSync(path.join(__dirname, DB_NAME), JSON.stringify(jsonData, null, 2));
}
axios.get(TARGET_URL).then(res => {
const htmlContent = res.data;
// write to temp/index.html
fs.writeFileSync(path.join(TARGET_TEMP_DIR, SOURCE_HTML_NAME), htmlContent);
const $ = cheerio.load(htmlContent);
const tdollList = $('span.gfl-doll-card');
const jsonData = [];
Array.from(tdollList).forEach(element => {
const info = getTdollInfo(element);
jsonData.push(info);
});
saveDataToFile(jsonData);
console.log('Update tdoll db Completed.');
});