-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
155 lines (140 loc) · 6.21 KB
/
server.js
File metadata and controls
155 lines (140 loc) · 6.21 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
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
153
154
155
import express from 'express'
import { config } from 'dotenv'
config()
const app = express()
const API_URL = process.env.API_URL
const TOKEN = process.env.AUTH_TOKEN
app.get('/api/foryou', async (req, res) => {
const { lang = 'in' } = req.query
try {
const response = await fetch(`${API_URL}/foryou?lang=${lang}`, {
headers: { Authorization: `Bearer ${TOKEN}`, 'User-Agent': 'Mozilla/5.0' }
})
const json = await response.json()
const books = json.data?.lists?.[0]?.books || []
const dramas = books.map(i => ({ id: i.book_id, title: i.book_title, cover: i.book_pic, episodes: i.chapter_count }))
res.json({ data: dramas })
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.get('/api/trending', async (req, res) => {
const { lang = 'in' } = req.query
try {
const response = await fetch(`${API_URL}/trending?lang=${lang}`, {
headers: { Authorization: `Bearer ${TOKEN}`, 'User-Agent': 'Mozilla/5.0' }
})
const json = await response.json()
const books = json.data?.lists?.[0]?.books || []
const dramas = books.map(i => ({ id: i.book_id, title: i.book_title, cover: i.book_pic, episodes: i.chapter_count }))
res.json({ data: dramas })
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.get('/api/suggestions', async (req, res) => {
const { lang = 'in' } = req.query
try {
const response = await fetch(`${API_URL}/search/suggestions?lang=${lang}`, {
headers: { Authorization: `Bearer ${TOKEN}`, 'User-Agent': 'Mozilla/5.0' }
})
const json = await response.json()
const books = json.data?.book_rank_data || []
const dramas = books.map(i => ({ id: i.book_id, title: i.book_title, cover: i.book_pic, episodes: i.chapter_count }))
res.json({ data: dramas })
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.get('/api/leaderboard', async (req, res) => {
const { lang = 'in' } = req.query
try {
const response = await fetch(`${API_URL}/leaderboard?lang=${lang}`, {
headers: { Authorization: `Bearer ${TOKEN}`, 'User-Agent': 'Mozilla/5.0' }
})
const json = await response.json()
const books = json.data?.lists?.books || []
const dramas = books.map(i => ({ id: i.book_id, title: i.book_title, cover: i.book_pic, episodes: i.chapter_count }))
res.json({ data: dramas })
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.get('/api/romance', async (req, res) => {
const { lang = 'in' } = req.query
try {
const response = await fetch(`${API_URL}/romance?lang=${lang}`, {
headers: { Authorization: `Bearer ${TOKEN}`, 'User-Agent': 'Mozilla/5.0' }
})
const json = await response.json()
const books = json.data?.lists?.[0]?.books || []
const dramas = books.map(i => ({ id: i.book_id, title: i.book_title, cover: i.book_pic, episodes: i.chapter_count }))
res.json({ data: dramas })
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.get('/api/completed', async (req, res) => {
const { lang = 'in' } = req.query
try {
const response = await fetch(`${API_URL}/completed?lang=${lang}`, {
headers: { Authorization: `Bearer ${TOKEN}`, 'User-Agent': 'Mozilla/5.0' }
})
const json = await response.json()
const books = json.data?.lists?.[0]?.books || []
const dramas = books.map(i => ({ id: i.book_id, title: i.book_title, cover: i.book_pic, episodes: i.chapter_count }))
res.json({ data: dramas })
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.get('/api/search', async (req, res) => {
const { q, page = '1', lang = 'in' } = req.query
if (!q) return res.status(400).json({ error: 'Missing query' })
try {
const response = await fetch(`${API_URL}/search?q=${encodeURIComponent(q)}&page=${page}&lang=${lang}`, {
headers: { Authorization: `Bearer ${TOKEN}`, 'User-Agent': 'Mozilla/5.0' }
})
res.json(await response.json())
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.get('/api/book', async (req, res) => {
const { id, lang = 'in' } = req.query
if (!id) return res.status(400).json({ error: 'Missing id' })
try {
const response = await fetch(`${API_URL}/book/${id}?lang=${lang}`, {
headers: { Authorization: `Bearer ${TOKEN}`, 'User-Agent': 'Mozilla/5.0' }
})
res.json(await response.json())
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.get('/api/chapters', async (req, res) => {
const { id, lang = 'in' } = req.query
if (!id) return res.status(400).json({ error: 'Missing id' })
try {
const response = await fetch(`${API_URL}/book/${id}/chapters?lang=${lang}`, {
headers: { Authorization: `Bearer ${TOKEN}`, 'User-Agent': 'Mozilla/5.0' }
})
res.json(await response.json())
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.get('/api/video', async (req, res) => {
const { id, chapter } = req.query
if (!id || !chapter) return res.status(400).json({ error: 'Missing id or chapter' })
try {
const response = await fetch(`${API_URL}/book/${id}/chapter/${chapter}/video`, {
headers: { Authorization: `Bearer ${TOKEN}`, 'User-Agent': 'Mozilla/5.0' }
})
res.json(await response.json())
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.get('/api/proxy', async (req, res) => {
const { url } = req.query
if (!url) return res.status(400).json({ error: 'Missing url' })
try {
const response = await fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0' } })
if (url.includes('.m3u8')) {
let m3u8 = await response.text()
m3u8 = m3u8.replace(/^([^#\n].+\.ts.*)$/gm, (match) => {
const tsUrl = match.startsWith('http') ? match : new URL(match, url).href
return `/api/proxy?url=${encodeURIComponent(tsUrl)}`
})
res.set('Content-Type', 'application/vnd.apple.mpegurl')
res.send(m3u8)
} else {
res.set('Content-Type', response.headers.get('content-type') || 'video/mp2t')
res.send(Buffer.from(await response.arrayBuffer()))
}
} catch (err) { res.status(500).json({ error: err.message }) }
})
app.use(express.static('dist'))
app.get('/{*path}', (req, res) => res.sendFile('index.html', { root: 'dist' }))
app.listen(3004, () => console.log('ReelShort server running on port 3004'))