-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
248 lines (232 loc) · 9.16 KB
/
main.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import axios from "axios";
import https from "https";
import { JSDOM } from 'jsdom';
import { musicScore, maiScore, musicDifficulty } from './class'
// Disable maimaidx.jp Cert Check
const agent = new https.Agent({
rejectUnauthorized: false
});
// Endpoints
const
dxNetJpUrl = 'https://maimaidx.jp',
dxNetJpUrlLogin = 'https://maimaidx.jp/maimai-mobile/',
dxNetJpAime = 'https://maimaidx.jp/maimai-mobile/aimeList/';
// Global headers
const
// Endpoint Headers
headerOriginJp = dxNetJpUrl,
headerRefererJp = dxNetJpUrlLogin,
headerAimeJp = dxNetJpAime,
// Browser Headers
headerAcceptPost = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
headerAcceptLanguage = 'zh-CN,zh-TW;q=0.9,zh;q=0.8',
headerCacheControl = 'max-age=0',
headerConnection = 'keep-alive',
headerContentTypeForm = 'application/x-www-form-urlencoded',
headerDNT = '1',
headerUpgradeInsecureRequests = '1',
// Chromium "Sec-Fetch" & "sec-ch-ua" Headers
headerSFDest = 'document',
headerSFMode = 'navigate',
headerSFSite = 'same-origin',
headerSFSiteNone = 'none',
headerSFUser = '?1',
headerSCU = '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
headerSCUMobile = '?0',
headerSCUPlatform = '"Windows"',
// UA
headerUA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function buildToken() { // Cookie '_t'
let os = require('os');
let { createHash } = require('crypto');
let date = new Date();
let machineId = createHash('md5')
.update(os.hostname() + os.totalmem() + os.homedir() + date.getUTCFullYear() + 'Kalium')
.digest('hex');
return machineId;
}
async function refreshIdJp(segaId: string, passwd: string) {
let token = buildToken();
let aimeId = '0';
// Step 1-1: Login DX Net
let loginNet = await axios({
method: 'post',
url: 'https://maimaidx.jp/maimai-mobile/submit/',
data: {
segaId: segaId,
password: passwd,
token: token
},
headers: {
'Accept': headerAcceptPost,
'Accept-Language': headerAcceptLanguage,
'Cache-Control': headerCacheControl,
'Connection': headerConnection,
'Content-Type': headerContentTypeForm,
'Cookie': '_t=' + token,
'DNT': headerDNT,
'Origin': headerOriginJp,
'Referer': headerRefererJp,
'Sec-Fetch-Dest': headerSFDest,
'Sec-Fetch-Mode': headerSFMode,
'Sec-Fetch-Site': headerSFSite,
'Sec-Fetch-User': headerSFUser,
'Upgrade-Insecure-Requests': headerUpgradeInsecureRequests,
'sec-ch-ua': headerSCU,
'sec-ch-ua-mobile': headerSCUMobile,
'sec-ch-ua-platform': headerSCUPlatform,
'User-Agent': headerUA
},
httpsAgent: agent,
maxRedirects: 0,
validateStatus: function (status) {
return status == 302;
}
}).then(response => {
// response.data is for debug only, do NOT remove, will be dropped in 1-2
return response.data + 'Response Headers:' + response.headers;
}).catch(error => {
console.error('Error:', error);
}) as string;
// Step 1-2: Update userId After Login
let IdAfterLogin = loginNet.match(/userId=([^;]+)/)![1];
// Step 2-1: Sleep
await sleep(100);
// Step 2-2: Select AIME
let selectAime = await axios({
method: 'get',
url: 'https://maimaidx.jp/maimai-mobile/aimeList/submit/?idx=' + aimeId,
headers: {
'Accept': headerAcceptPost,
'Accept-Language': headerAcceptLanguage,
'Connection': headerConnection,
'Cookie': '_t=' + token + '; userId=' + IdAfterLogin,
'DNT': headerDNT,
'Referer': headerAimeJp,
'Sec-Fetch-Dest': headerSFDest,
'Sec-Fetch-Mode': headerSFMode,
'Sec-Fetch-Site': headerSFSiteNone,
'Sec-Fetch-User': headerSFUser,
'Upgrade-Insecure-Requests': headerUpgradeInsecureRequests,
'sec-ch-ua': headerSCU,
'sec-ch-ua-mobile': headerSCUMobile,
'sec-ch-ua-platform': headerSCUPlatform,
'User-Agent': headerUA
},
httpsAgent: agent,
maxRedirects: 0,
validateStatus: function (status) {
return status == 302;
}
}).then(response => {
return 'Response Headers:' + response.headers + 'Code:' + response.status;
}).catch(error => {
console.error('Error:', error);
}) as string;
// Step 2-3: Finish GetId
let IdFin = selectAime.match(/userId=(\d+)/)![1];
return IdFin;
}
export async function maiRankJp(sid: string, segaId: string, passwd: string): Promise<maiScore[]> {
// ReM only now
let userId = await refreshIdJp(segaId, passwd);
let rankPage = await axios({
method: 'get',
url: 'https://maimaidx.jp/maimai-mobile/ranking/musicRankingDetail/?idx=' + sid + '&scoreType=2&rankingType=99&diff=4',
headers: {
'Accept': headerAcceptPost,
'Accept-Language': headerAcceptLanguage,
'Connection': headerConnection,
'Cookie': '_t=' + buildToken() + '; userId=' + userId,
'DNT': headerDNT,
'Referer': headerAimeJp,
'Sec-Fetch-Dest': headerSFDest,
'Sec-Fetch-Mode': headerSFMode,
'Sec-Fetch-Site': headerSFSiteNone,
'Sec-Fetch-User': headerSFUser,
'Upgrade-Insecure-Requests': headerUpgradeInsecureRequests,
'sec-ch-ua': headerSCU,
'sec-ch-ua-mobile': headerSCUMobile,
'sec-ch-ua-platform': headerSCUPlatform,
'User-Agent': headerUA
},
httpsAgent: agent
}).then(response => {
return response.data;
}).catch(error => {
console.error('Error:', error);
}) as string;
//let rank1n = rankPage.split('\n')[225].trim();
//let rank1d = rankPage.split('\n')[227].match(/.*f_r t_c">([^<]*)<\/div>.*/)![1];
//let rank1l = rankPage.split('\n')[228].match(/.*<div class="p_15 p_r_10 p_b_0 f_r t_r f_16 f_b">([^<]*)<\/div>.*/)![1];
//let rank2n = rankPage.split('\n')[238].trim();
//let rank2d = rankPage.split('\n')[240].match(/.*f_r t_c">([^<]*)<\/div>.*/)![1];
//let rank2l = rankPage.split('\n')[241].match(/.*<div class="p_15 p_r_10 p_b_0 f_r t_r f_16 f_b">([^<]*)<\/div>.*/)![1];
//let rank3n = rankPage.split('\n')[251].trim();
//let rank3d = rankPage.split('\n')[253].match(/.*f_r t_c">([^<]*)<\/div>.*/)![1];
//let rank3l = rankPage.split('\n')[254].match(/.*<div class="p_15 p_r_10 p_b_0 f_r t_r f_16 f_b">([^<]*)<\/div>.*/)![1];
//let result =
// '[1] ' + rank1n + '\n' +
// rank1d + '\n' +
// rank1l + '\n' +
// '[2] ' + rank2n + '\n' +
// rank2d + '\n' +
// rank2l + '\n' +
// '[3] ' + rank3n + '\n' +
// rank3d + '\n' +
// rank3l + '\n';
return dxNetParser(rankPage);
}
// Html Parser
function getElements(htmlContent: string, selector: string): string[] | undefined {
if (!htmlContent)
return undefined;
let dom = new JSDOM(htmlContent);
let doc = dom.window.document;
const elements = doc.querySelectorAll(selector);
return Array.from(elements).map(x => stringHandle(x.textContent ?? ""));
}
function stringHandle(s: string): string {
return s.replace(/[\uff01-\uff5e]/g,
c => String.fromCharCode(c.charCodeAt(0) - 0xfee0))
.replace(/\s/g, '');
}
function dxNetParser(html: string): maiScore[] {
let rankers: string[] = getElements(html, ".f_l.p_t_10.p_l_10.f_15") ?? [];
let scores: number[] = (getElements(html, ".p_15.p_r_10.p_b_0.f_r.t_r.f_16.f_b") ?? []).map(x => parseFloat(x));
let date: Date[] = (getElements(html, ".ranking_music_date.f_r.t_c") ?? []).map(x => new Date(x));
let tCount: number[] = (getElements(html, ".ranking_theory_count.t_r") ?? []).map(x => parseInt(x));
let result: maiScore[] = [];
let i: number = 0;
for (i = i; i < rankers.length; i++) {
result.push(new maiScore(rankers[i],
scores[i],
i,
tCount[i],
date[i]));
}
return result;
}
// Get All Music Score
function getMusicScore(htmlData: string, diff: musicDifficulty): musicScore[]
{
let names = getElements(htmlData, ".music_name_block.t_l.f_13.break") ?? [];
let levels = getElements(htmlData,".music_lv_block.f_r.t_c.f_14") ?? [];
let scores = (getElements(htmlData,".music_score_block.w_112.t_r.f_l.f_12") ?? []).map(x => parseFloat(x));
let dxScores = (getElements(htmlData,".music_score_block.w_190.t_r.f_l.f_12") ?? []).map(x => parseInt(x));
let result: musicScore[] = [];
let i = 0;
for( i = i;i < names.length;i++) {
result.push(new musicScore(
names[i],
levels[i],
scores[i],
dxScores[i],
diff
))
}
return result;
}