forked from aszx87410/medium-user-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
74 lines (64 loc) · 1.81 KB
/
app.js
File metadata and controls
74 lines (64 loc) · 1.81 KB
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
const DB = require('./db')
const Queue = require('./queue')
const config = require('./config')
const medium = require('./medium')
const utils = require('./utils')
async function main() {
const db = new DB(config.db)
const queue = new Queue(db)
const existingUserIds = await db.getExistingUserIds()
const userIdMap = {}
for (let userId of existingUserIds) {
userIdMap[userId] = true
}
utils.log('Existing userId:', existingUserIds.length)
while(true) {
const userIds = await queue.get(1)
if (userIds.length === 0) {
utils.log('Done')
break;
}
const userId = userIds[0]
if (userIdMap[userId]) {
continue
}
userIdMap[userId] = true
utils.log('userId:', userId)
try {
const userInfo = await medium.getUserInfo(userId)
if (!userInfo.userId) {
utils.log('getUerrInfo error, sleep for', config.delayWhenError)
await utils.sleep(config.delayWhenError)
}
if (!userInfo.isMandarinUser) {
utils.log(userId, 'not MandarinUser')
continue
}
db.insertUserData(userInfo)
if (userInfo.followerCount > 0) {
let to = undefined
let count = 0
while (true) {
const data = await medium.getFollowers(userInfo.userId, to)
if (!data) {
break
}
const { nextTo, followers } = data
to = nextTo
count += followers.length
utils.log(userInfo.userId, 'fetching', count, 'followers')
await queue.push(followers.filter(uid => !userIdMap[uid]))
if (followers.length === 0 || !to) {
break;
}
}
}
} catch (err) {
utils.log('sleep for', config.delayWhenError)
utils.log(err)
await utils.sleep(config.delayWhenError)
}
}
process.exit()
}
main()