-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (58 loc) · 1.88 KB
/
index.js
File metadata and controls
72 lines (58 loc) · 1.88 KB
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
import express from 'express'
import cors from 'cors'
import he from 'he'
const app = express()
const PORT = 3001
app.use(cors())
const getSubtitles = async (id, lang) => {
const encodedId = encodeURIComponent(id)
const res = await fetch(`https://apiv2.anthiago.com/transcript?get_video=${encodedId}&codeL=${lang}&status=false`, {
headers: {
'Referer': 'https://anthiago.com/desgrabador/'
}
})
const data = await res.json()
return data.status === 'ok' ? data : []
}
const getEmbedLink = (id) => {
return `https://www.youtube.com/embed/${id}`
}
app.get('/api/get', async (req, res) => {
try {
const { id } = req.query
if (!id) return res.status(400).json({ error: 'Missing id' })
const [dataVi, dataEn] = await Promise.all([
getSubtitles(id, 'vi'),
getSubtitles(id, 'en')
])
console.log(dataEn);
const mergedMap = {}
for (const sub of dataVi.subtitles) {
mergedMap[sub.t] = {
time: sub.t,
vi: he.decode(sub.f),
en: ''
}
}
for (const sub of dataEn.subtitles) {
if (mergedMap[sub.t]) {
mergedMap[sub.t].en = he.decode(sub.f)
} else {
mergedMap[sub.t] = {
time: sub.t,
vi: '',
en: he.decode(sub.f)
}
}
}
const mergedArray = Object.values(mergedMap).sort((a, b) => a.time - b.time)
const embedLink = getEmbedLink(id)
res.json({ subtitles: mergedArray, embed: embedLink, title: dataVi.title})
} catch (error) {
console.error(error)
res.status(500).json({ error: 'Something went wrong' })
}
})
app.listen(PORT, () => {
console.log(`🚀 Server running at http://localhost:${PORT}`)
})