-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (50 loc) · 1.78 KB
/
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
var request = require('sync-request')
var sleep = require('sleep')
var fs = require('fs')
var config = JSON.parse(fs.readFileSync('config.json', 'utf-8'))
// z_c0=".+?"
var cookie = fs.readFileSync('cookie', 'utf-8').trim()
function sleepWithPrint(seconds, duration=60) {
var times = Math.trunc(seconds / duration)
var remain = seconds % duration
for(var i = 0; i < times; i++) {
console.log(`Sleep: ${i}`)
sleep.sleep(duration)
}
if(remain != 0) {
console.log(`Sleep: ${times}`)
sleep.sleep(remain)
}
}
function getUsersId(tid, start, num) {
var url = `https://www.zhihu.com/api/v4/topics/${tid}/followers?limit=${num}&offset=${start}`
var resStr = request('GET', url).body.toString()
return JSON.parse(resStr).data.map(x => x.id)
}
function sendMsg(uid, co, cookie) {
var postData = {"type": "common", "content": co, "receiver_hash": uid}
var url = 'https://www.zhihu.com/api/v4/messages'
var res = request('POST', url, {json: postData, headers : {Cookie: cookie}})
if(res.statusCode == 200)
return [1, '']
else
return [0, res.statusCode]
}
function main() {
var topics = config.topics.map(x => x[0])
var idx = Math.trunc(Math.random() * topics.length)
var start = Math.trunc(Math.random() * 201)
var uids = getUsersId(topics[idx], start, config.send_num)
for(var uid of uids) {
var res = sendMsg(uid, config.co, cookie)
if(res[0])
console.log(`${uid} 发送成功`)
else
console.log(`${uid} 发送失败:${res[1]}`)
if(res[1] == 403)
sleepWithPrint(config._403_wait_sec)
else
sleep.sleep(config.wait_sec)
}
}
if(require.main === module) main()