-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
151 lines (144 loc) · 4.59 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
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
const fs = require('fs').promises
const io = require('socket.io-client')
const socket = io('http://0.0.0.0:9001')
const get = (e, target) => new Promise(resolve => socket.emit(e, target, resolve))
const biliAPI = require('bili-api')
const LiveWS = require('bilibili-live-ws')
const race = (...args) => new Promise((resolve, reject) => {
setTimeout(reject, 1000 * 5)
biliAPI(...args)
.then(resolve)
})
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
let rooms = {}
const openRoom = ({ roomid, speakers = {}, currentFilename = undefined }) => {
let ws = new LiveWS(roomid)
rooms[roomid] = ws
let lastTime = ''
// let storm = []
ws.once('live', () => {
console.log(`READY: ${roomid}`)
ws.on('DANMU_MSG', async ({ info }) => {
if (!info[0][9]) {
let message = info[1]
if (!message.includes('TIME') || !message.includes('ONLINE')) {
let mid = info[2][0]
let date = new Date()
let filename = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}.txt`
let time = `${date.getHours()}:${date.getMinutes()}`
if (!currentFilename) {
currentFilename = filename
}
if (currentFilename !== filename) {
let speakerNum = Object.keys(speakers).length
let lastFIleName = currentFilename
currentFilename = filename
if (speakerNum) {
speakers = {}
await fs.appendFile(`${roomid}/${lastFIleName}`, `SPEAKERNUM${speakerNum}\n`)
}
}
speakers[mid] = true
if (lastTime !== time) {
lastTime = time
await fs.appendFile(`${roomid}/${filename}`, `TIME${lastTime}ONLINE${ws.online}\n`)
}
await fs.appendFile(`${roomid}/${filename}`, `${message}\n`)
// console.log(`${roomid}: ${message}`)
}
}
})
})
// ws.on('SEND_GIFT', ({ data }) => {
// if (data.giftName === '节奏风暴') {
// console.log(data)
// storm.push(data.metadata)
// }
// })
ws.on('heartbeat', async () => {
let date = new Date()
let filename = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}.txt`
if (!currentFilename) {
currentFilename = filename
}
if (currentFilename !== filename) {
let speakerNum = Object.keys(speakers).length
let lastFIleName = currentFilename
currentFilename = filename
if (speakerNum) {
speakers = {}
await fs.appendFile(`${roomid}/${lastFIleName}`, `SPEAKERNUM${speakerNum}\n`)
}
}
})
ws.on('heartbeat', async online => {
if (online > 1) {
let date = new Date()
let time = `${date.getHours()}:${date.getMinutes()}`
let filename = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}.txt`
if (lastTime !== time) {
lastTime = time
await fs.appendFile(`${roomid}/${filename}`, `TIME${lastTime}ONLINE${online}\n`)
}
}
})
ws.once('open', () => {
ws.once('close', async () => {
console.log(`CLOSE: ${roomid}`)
if (rooms[roomid]) {
await wait(500)
console.log(`REOPEN: ${roomid}`)
openRoom({ roomid, speakers, currentFilename })
}
})
})
ws.on('error', async () => {
console.log(`ERROR: ${roomid}`)
ws.terminate()
if (rooms[roomid]) {
await wait(500)
console.log(`REOPEN: ${roomid}`)
openRoom({ roomid, speakers, currentFilename })
}
})
}
(async () => {
for (;;) {
let folders = await fs.readdir('.')
let vtbs = await get('vtbs')
let roomsEnable = []
for (let i = 0; i < vtbs.length; i++) {
let { mid } = vtbs[i]
let object = await race({ mid }, ['roomid'], { wait: 1000 }).catch(() => undefined)
if (!object) {
i--
console.log(`RETRY: ${mid}`)
await wait(1000 * 5)
continue
}
if (object.roomid) {
let roomid = object.roomid
roomsEnable.push(roomid)
if (!rooms[roomid]) {
if (!folders.includes(String(roomid))) {
await fs.mkdir(String(roomid))
}
console.log(`OPEN: ${i + 1}/${vtbs.length} - ${mid} - ${roomid}`)
openRoom({ roomid: roomid })
}
}
}
let roomsOpen = Object.keys(rooms)
for (let i = 0; i < roomsOpen.length; i++) {
if (rooms[roomsOpen[i]]) {
if (!roomsEnable.includes(Number(roomsOpen[i]))) {
console.log(`DISABLE: ${roomsOpen[i]}`);
rooms[roomsOpen[i]].close()
rooms[roomsOpen[i]] = false
}
}
}
console.log('REFRESH')
await wait(1000 * 60 * 60)
}
})()