-
Notifications
You must be signed in to change notification settings - Fork 0
/
gacha.js
53 lines (50 loc) · 1.89 KB
/
gacha.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
const request = require('request');
const cheerio = require('cheerio');
const URL_PREFIX = 'https://bgm.tv';
let username = 'aflyhorse';
let index = 0;
const fetchUrl = (url) => {
return new Promise((resolve, reject) => {
request(url, (error, response, body) => {
if (!error && response.statusCode === 200) {
resolve(cheerio.load(body));
return;
}
reject();
});
});
};
module.exports = {
run: (username) => {
if (!username) {
return Promise.reject();
}
return new Promise((resolve, reject) => {
fetchUrl(`${URL_PREFIX}/anime/list/${username}/wish`)
.then(($) => {
const totalCount = parseInt($('#headerProfile .navSubTabsWrapper .navSubTabs').children().first().find('span').html().match(/\d+/g).pop() || 0, 10);
const luckyNum = Math.floor(Math.random() * totalCount);
let page = Math.ceil(luckyNum / 24);
index = luckyNum % 24;
if (index <= 0) {
index += 24;
}
if (page === 1) {
let suffix = $('.mainWrapper #browserItemList').children().eq(index - 1).find('a.subjectCover').attr('href');
if (suffix) {
resolve(URL_PREFIX + suffix);
}
} else {
fetchUrl(`${URL_PREFIX}/anime/list/${username}/wish?page=${page}`)
.then(($) => {
let suffix = $('.mainWrapper #browserItemList').children().eq(index - 1).find('a.subjectCover').attr('href');
if (suffix) {
resolve(URL_PREFIX + suffix);
}
}).catch(reject);
}
})
.catch(reject);
});
}
};