-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraping.js
138 lines (113 loc) · 3.9 KB
/
scraping.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
127
128
129
130
131
132
133
134
135
136
137
class CSV {
constructor(data, keys = false) {
this.ARRAY = Symbol('ARRAY');
this.OBJECT = Symbol('OBJECT');
this.data = data;
if (CSV.isArray(data)) {
if (0 == data.length) {
this.dataType = this.ARRAY;
} else if (CSV.isObject(data[0])) {
this.dataType = this.OBJECT;
} else if (CSV.isArray(data[0])) {
this.dataType = this.ARRAY;
} else {
throw Error('Error: 未対応のデータ型です');
}
} else {
throw Error('Error: 未対応のデータ型です');
}
this.keys = keys;
}
toString() {
if (this.dataType === this.ARRAY) {
return this.data
.map((record) => record.map((field) => CSV.prepare(field)).join(','))
.join('\n');
} else if (this.dataType === this.OBJECT) {
const keys = this.keys || Array.from(this.extractKeys(this.data));
const arrayData = this.data.map((record) =>
keys.map((key) => record[key])
);
return []
.concat([keys], arrayData)
.map((record) => record.map((field) => CSV.prepare(field)).join(','))
.join('\n');
}
}
save(filename = 'data.csv') {
if (!filename.match(/\.csv$/i)) {
filename = filename + '.csv';
}
const csvStr = this.toString();
const bom = new Uint8Array([0xef, 0xbb, 0xbf]);
const blob = new Blob([bom, csvStr], {
type: 'text/csv'
});
const url = window.URL || window.webkitURL;
const blobURL = url.createObjectURL(blob);
let a = document.createElement('a');
a.download = decodeURI(filename);
a.href = blobURL;
a.type = 'text/csv';
a.click();
}
extractKeys(data) {
return new Set(
[].concat(...this.data.map((record) => Object.keys(record)))
);
}
static prepare(field) {
return '"' + ('' + field).replace(/"/g, '""') + '"';
}
static isObject(obj) {
return '[object Object]' === Object.prototype.toString.call(obj);
}
static isArray(obj) {
return '[object Array]' === Object.prototype.toString.call(obj);
}
}
try {
const URL = location.href;
if (URL != 'https://wacca.marv-games.jp/web/music') {
throw new Error(
'表示しているページが違います。プレイデータの楽曲スコアのページを開いて再度試してください。'
);
}
let results = [];
let score_ave = [0, 0, 0, 0];
let inf_num = 0;
let tmpScore, tmpResult;
let nodes = $('li.muuri-item');
for (let i = 1; i <= nodes.length; i++) {
tmpScore = nodes[i - 1].querySelectorAll(
'.playdata__score-list__song-info__score'
);
tmpResult = {
title: nodes[i - 1].querySelector(
'.playdata__score-list__song-info__name'
).innerText,
n_score: tmpScore[0].innerText.replace(/\s+/g, '').replace('SCORE', ''),
h_score: tmpScore[1].innerText.replace(/\s+/g, '').replace('SCORE', ''),
e_score: tmpScore[2].innerText.replace(/\s+/g, '').replace('SCORE', ''),
i_score: tmpScore[3].innerText.replace(/\s+/g, '').replace('SCORE', '')
};
score_ave[0] += parseInt(tmpResult.n_score);
score_ave[1] += parseInt(tmpResult.h_score);
score_ave[2] += parseInt(tmpResult.e_score);
if (tmpResult.i_score != '0') {
score_ave[3] += parseInt(tmpResult.i_score);
inf_num++;
}
results.push(tmpResult);
}
score_ave[0] = Math.round(score_ave[0] / nodes.length);
score_ave[1] = Math.round(score_ave[1] / nodes.length);
score_ave[2] = Math.round(score_ave[2] / nodes.length);
score_ave[3] = Math.round(score_ave[3] / inf_num);
alert(
`WACCAスコア集計ツール\nAuthor:@chirping_crow\n曲数:${nodes.length}\nINFERNO譜面数:${inf_num}\nAverage(小数点以下四捨五入):\nNORMAL:${score_ave[0]}\nHARD:${score_ave[1]}\nEXPERT:${score_ave[2]}\nINFERNO:${score_ave[3]}\n\nボタンを押すとCSVのダウンロードが始まります。`
);
new CSV(results).save('wacca_score.csv');
} catch (e) {
alert(e);
}