-
Notifications
You must be signed in to change notification settings - Fork 10
/
spider-by-index.js
164 lines (145 loc) · 4.54 KB
/
spider-by-index.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
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
/*
* @Author: czy0729
* @Date: 2020-01-14 18:51:27
* @Last Modified by: czy0729
* @Last Modified time: 2021-12-31 07:38:36
*/
const axios = require('axios')
const fs = require('fs')
const path = require('path')
const cheerio = require('./utils/cheerio')
const utils = require('./utils/utils')
axios.defaults.timeout = 3000
// settings
const host = 'bgm.tv'
const rewrite = true
const index = 337455
const startIndex = 282580
const queue = 6
// navigator.userAgent
// document.cookie
const headers = {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
Cookie:
'chii_sec_id=pG5Jgrb5v3PhSnN%2B9S%2Bj0sTJQGDkbMC5jU2SCGE; chii_cookietime=2592000; chii_theme_choose=1; __utmz=1.1626708381.273.9.utmcsr=tongji.baidu.com|utmccn=(referral)|utmcmd=referral|utmcct=/; prg_display_mode=normal; chii_theme=dark; chii_auth=kTryE60lfNJ8LkW7SDTpUhf%2FcHK1kCSU99u5EmBuGDSVmtOfpUmVi1YLxpAT%2FPFvR%2B3p8VwETj2vFbIfw%2FujdCuTivdxlB%2FwFtTr; __utmc=1; chii_searchDateLine=0; chii_sid=88NJF5; __utma=1.1636245540.1617210056.1640904907.1640907491.377; __utmt=1; __utmb=1.1.10.1640907491',
}
// console speed
const speed = {
0: 0,
1: 0,
}
let currentIndex = 0
setInterval(() => {
if (currentIndex === 1) {
currentIndex = 0
} else {
currentIndex += 1
}
console.log(`- speed ${(speed[currentIndex] / 3).toFixed(1)}/s`)
speed[currentIndex] = 0
}, 3000)
// queue fetch
const ids = []
for (let i = startIndex; i <= index; i += 1) {
ids.push(i)
}
async function fetchSubject(id, index) {
try {
const filePath = `./data/${Math.floor(id / 100)}/${id}.json`
const exists = fs.existsSync(filePath)
if (!rewrite && exists) {
return true
}
try {
if (exists) {
const data = JSON.parse(fs.readFileSync(filePath))
if (!data.name || data.name === '坟场') {
return true
}
}
} catch (error) {
return true
}
const { data: html } = await axios({
url: `https://${host}/subject/${id}`,
headers,
})
const htmlDS = cheerio.cheerioSubjectFormHTML(html)
const { data: apiDS } = await axios({
url: `https://api.bgm.tv/subject/${id}?responseGroup=large`,
})
// 基本
const data = {
id: apiDS.id,
type: apiDS.type,
name: apiDS.name,
}
if (apiDS.images && apiDS.images.medium)
data.image = utils.smallImage(apiDS)
if (apiDS.rating) data.rating = apiDS.rating
// 详情
if (apiDS.summary) data.summary = apiDS.summary
if (htmlDS.info) data.info = htmlDS.info
// 条目收藏数章节等
if (apiDS.collection) data.collection = apiDS.collection
if (htmlDS.tags && htmlDS.tags.length) data.tags = htmlDS.tags
if (apiDS.eps && apiDS.eps.length) data.eps = apiDS.eps
if (htmlDS.disc && htmlDS.disc.length) data.disc = htmlDS.disc
// 人物
if (apiDS.crt && apiDS.crt.length) {
data.crt = apiDS.crt.map((item) => ({
id: item.id || '',
image: utils.smallImage(item, 'grid'),
name: item.name_cn || item.name || '',
desc:
(item.actors && item.actors[0] && item.actors[0].name) ||
item.role_name ||
'',
}))
}
if (apiDS.staff && apiDS.staff.length) {
data.staff = apiDS.staff.map((item) => ({
id: item.id || '',
image: utils.smallImage(item, 'grid'),
name: item.name_cn || item.name || '',
desc: (item.jobs && item.jobs[0]) || '',
}))
}
// 关联
if (htmlDS.relations && htmlDS.relations.length)
data.relations = htmlDS.relations
if (htmlDS.comic && htmlDS.comic.length) data.comic = htmlDS.comic
if (htmlDS.like && htmlDS.like.length) data.like = htmlDS.like
// 锁定
if (htmlDS.lock) data.lock = htmlDS.lock
// data._loaded = utils.getTimestamp()
const dirPath = path.dirname(filePath)
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath)
}
console.log(
`- ${exists ? 're' : ''}writing ${id}.json [${index} / ${ids.length}] ${(
(index / ids.length) *
100
).toFixed(1)}%`,
data.name
)
fs.writeFileSync(filePath, utils.decode(utils.safeStringify(data)))
speed[currentIndex] += 1
return true
} catch (error) {
const msg =
'\x1b[40m \x1b[31m[RETRY] ' +
id +
'.json [' +
index +
' / ' +
ids.length +
'] \x1b[0m'
console.log(msg)
return fetchSubject(id, index)
}
}
const fetchs = ids.map((id, index) => () => fetchSubject(id, index))
utils.queue(fetchs, queue)