forked from dd-center/bilibili-vtuber-live-danmaku-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (145 loc) · 4.71 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
152
const fs = require('fs').promises
const io = require('socket.io-client')
const socket = io('http://0.0.0.0:8001')
const LiveWS = require('bilibili-live-ws')
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
let rooms = {}
const openRoom = ({ roomid, speakers = {}, currentFilename = undefined }) => new Promise(resolve => {
console.log(`OPEN: ${roomid}`)
let ws = new LiveWS(roomid)
rooms[roomid] = ws
let lastTime = ''
let lastHeartbeat = 0
let autorestart = setTimeout(() => {
console.log(`AUTORESTART: ${roomid}`)
ws.close()
resolve({ roomid, speakers, currentFilename })
}, 1000 * 60 * 60 * 18)
let timeout = setTimeout(() => {
if (new Date().getTime() - lastHeartbeat > 1000 * 30) {
console.log(`TIMEOUT: ${roomid}`)
ws.close()
clearTimeout(autorestart)
clearTimeout(timeout)
resolve({ roomid, speakers, currentFilename })
}
}, 1000 * 45)
// let storm = []
ws.once('live', () => {
console.log(`READY: ${roomid}`)
})
ws.on('DANMU_MSG:4:0:2:2:2:0', async ({ info }) => {
if (!info[0][9]) {
let message = info[1]
if (!message.includes('TIME') || !message.includes('ONLINE')) {
let mid = info[2][0]
let uname = info[2][1]
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) {
let allSpeaker = Object.keys(speakers)
.map(key => `${key}:${speakers[key].uname}:${speakers[key].count}`)
.join(',')
speakers = {}
await fs.appendFile(`${roomid}/${lastFIleName}`, `SPEAKERNUM${speakerNum};${allSpeaker}\nV1\n`)
}
}
if (!speakers[mid]) {
speakers[mid] = { count: 0, uname }
}
speakers[mid].count++
if (lastTime !== time) {
lastTime = time
await fs.appendFile(`${roomid}/${filename}`, `TIME${lastTime}ONLINE${ws.online}\n`)
}
await fs.appendFile(`${roomid}/${filename}`, `${mid}:${message}\n`)
}
}
})
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) {
let allSpeaker = Object.keys(speakers)
.map(key => `${key}:${speakers[key].uname}:${speakers[key].count}`)
.join(',')
speakers = {}
await fs.appendFile(`${roomid}/${lastFIleName}`, `SPEAKERNUM${speakerNum};${allSpeaker}\nV1\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.on('heartbeat', () => {
lastHeartbeat = new Date().getTime()
timeout = setTimeout(() => {
if (new Date().getTime() - lastHeartbeat > 1000 * 30) {
console.log(`TIMEOUT: ${roomid}`)
ws.close()
clearTimeout(autorestart)
clearTimeout(timeout)
resolve({ roomid, speakers, currentFilename })
}
}, 1000 * 45)
})
ws.on('close', async () => {
console.log(`CLOSE: ${roomid}`)
clearTimeout(autorestart)
clearTimeout(timeout)
resolve({ roomid, speakers, currentFilename })
})
ws.on('error', async () => {
console.log(`ERROR: ${roomid}`)
ws.close()
clearTimeout(autorestart)
clearTimeout(timeout)
resolve({ roomid, speakers, currentFilename })
})
})
const watch = async roomid => {
let object = { roomid }
for (;;) {
object = await openRoom(object)
await wait(250)
console.log(`REOPEN: ${roomid}`)
}
}
socket.on('info', async info => {
let folders = await fs.readdir('.')
info.map(({ roomid }) => roomid)
.filter(roomid => roomid)
.forEach(async roomid => {
if (!rooms[roomid]) {
rooms[roomid] = true
if (!folders.includes(String(roomid))) {
await fs.mkdir(String(roomid))
}
watch(roomid)
}
})
console.log('REFRESH')
})