-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (89 loc) · 2.92 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
import express from 'express'
import logger from 'morgan'
import Redis from 'ioredis'
import fs from 'fs'
import bodyParser from 'body-parser'
import srtToVtt from 'srt-to-vtt'
import assToVtt from 'ass-to-vtt'
import str from 'string-to-stream'
import toString from 'stream-to-string'
const app = express()
const redis = new Redis()
app.use(logger('dev'))
app.use(bodyParser.urlencoded({ extended: false }))
app.get('/:anime/:season/:episode/:lang', async (req, res) => {
const { anime, season, episode, lang } = req.params
const content = await redis.get(`${anime}/${season}/${episode}/${lang}`)
if (!content) {
res.sendStatus(404)
return
}
const info = extractInfos(content)
const toSend = insertHeader(content, info)
res.set('info', JSON.stringify(info))
res.type('text')
res.end(toSend)
})
const conversion = {
vtt: content => content,
ass: content => toString(str(content).pipe(assToVtt())),
srt: content => toString(str(content).pipe(srtToVtt())) //is there a better way?
}
app.post('/:anime/:season/:episode/:lang', async (req, res) => {
const { anime, season, episode, lang } = req.params
const { info, content, type } = req.body
const conv = conversion[type]
if (!content || !type || !conv) {
res.sendStatus(400)
return
}
const converted = await conv(content)
const i = info || extractInfos(converted) || {}
const withInfo = info ? insertInfo(converted, i) : converted
redis.set(`${anime}/${season}/${episode}/${lang}`, withInfo)
redis.sadd(`${anime}/${season}/${episode}`, lang)
res.set('info', JSON.stringify(info))
res.type('text')
res.end(withInfo)
})
app.get('/:anime/:season/:episode', async (req, res) => {
const { anime, season, episode, lang } = req.params
const a = await redis.smembers(`${anime}/${season}/${episode}`)
res.type('json')
res.end(JSON.stringify(a))
})
app.delete('/:anime/:season/:episode/:lang', (req, res) => {
const { anime, season, episode, lang } = req.params
redis.del(`${anime}/${season}/${episode}/${lang}`)
redis.srem(`${anime}/${season}/${episode}`, lang)
res.end()
})
const header = fs.readFileSync('header.txt').toString()
function insertHeader(content, info = {}) {
const subber =
(info.subber || 'Unknown') +
(info.subber_link ? ' <' + info.subber_link + '>' : '')
return insertNote(content, header.replace('$INFO', info).replace('$SUBBER', subber))
}
function insertInfo(content, info = {}) {
return insertNote(content, `NOTE ${JSON.stringify({ info })}\n`)
}
function insertNote(content, insertion) {
const index = content.indexOf('\n') + 1 //Insert after first \n
return (
content.substring(0, index) + '\n' + insertion + content.substring(index)
)
}
function extractInfos(subs) {
const res = /{"info": ?{.+}}/.exec(subs)
if (!res) return
try {
return JSON.parse(res[0])
} catch (e) {
return
}
}
app.listen(3333, () => console.log('Listening on port 3333'))
process.on('unhandledRejection', error =>
console.error('unhandledRejection', error)
)