-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (64 loc) · 1.96 KB
/
app.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
const http = require('http')
const express = require('express')
const { Server } = require("socket.io");
const bili = require('bilibili-live-ws')
const axios = require('axios').default
const danmuLogger = require('./logger')
const getArgv = (key) => {
const argv = process.argv.slice(2)
const index = argv.indexOf(key)
if (index === -1) return null
return argv[index + 1]
}
const app = express()
const server = http.createServer(app)
const io = new Server(server);
app.use('/', express.static('public'))
io.on('connection', (socket) => {
socket.on('conn', async (room) => {
try {
console.log(`connecting to room ${room}`)
const live = new bili.LiveTCP(await bili.getRoomid(Number(room)))
live.on('DANMU_MSG', data => {
const info = data.info;
const username = info[2][1]
const uid = info[2][0]
const msg = info[1]
socket.emit('msg', data)
danmuLogger(room, `${username} (${uid})`, msg)
})
socket.on('disconnect', () => {
live.close()
console.log(`disconnected from room ${room}`)
})
console.log('connected to room', room)
} catch (error) {
socket.emit('error', error.message)
}
})
})
app.get('/avatar/:uid', async (req, res) => {
try {
const resp = await axios.get(`https://api.bilibili.com/x/web-interface/card?mid=${req.params.uid}`)
const face = resp.data.data.card.face
res.redirect(301, `/proxy/image?url=${encodeURIComponent(face)}`)
} catch (error) {
res.status(404).send('404')
}
})
app.get('/proxy/image', async (req, res) => {
try {
const url = req.query.url
const resp = await axios.get(url, {
responseType: 'arraybuffer'
})
res.setHeader('Content-Type', resp.headers['content-type'])
res.send(resp.data)
} catch (error) {
res.status(500).send()
}
})
const port = Number(getArgv('--port'))
server.listen(port || 3000, () => {
console.log(`listening on *:${port || 3000}`)
})