-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserver.js
More file actions
182 lines (163 loc) · 5.65 KB
/
server.js
File metadata and controls
182 lines (163 loc) · 5.65 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const express = require('express');
const path = require('path');
const { ZingMp3 } = require('./dist');
const app = express();
const PORT = process.env.PORT || 5555;
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// health
app.get('/health', (_req, res) => {
res.json({ ok: true });
});
// basic demos
app.get('/api/top100', async (_req, res) => {
try {
const data = await ZingMp3.getTop100();
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/home', async (_req, res) => {
try {
const data = await ZingMp3.getHome();
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
// full list according to README
app.get('/api/song', async (req, res) => {
try {
const { id } = req.query;
if (!id) return res.status(400).json({ error: 'Missing id' });
const data = await ZingMp3.getSong(String(id));
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
// redirect to stream URL for simple playback (avoid CORS)
app.get('/api/song/stream', async (req, res) => {
try {
const { id, quality = '128' } = req.query;
if (!id) return res.status(400).json({ error: 'Missing id' });
const response = await ZingMp3.getSong(String(id));
const url = response?.data?.[String(quality)];
if (!url || typeof url !== 'string') {
return res.status(404).json({ error: `Stream URL not found for quality ${quality}` });
}
res.redirect(url);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/detail-playlist', async (req, res) => {
try {
const { id } = req.query;
if (!id) return res.status(400).json({ error: 'Missing id' });
const data = await ZingMp3.getDetailPlaylist(String(id));
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/chart-home', async (_req, res) => {
try {
const data = await ZingMp3.getChartHome();
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/newrelease-chart', async (_req, res) => {
try {
const data = await ZingMp3.getNewReleaseChart();
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/info-song', async (req, res) => {
try {
const { id } = req.query;
if (!id) return res.status(400).json({ error: 'Missing id' });
const data = await ZingMp3.getInfoSong(String(id));
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/artist', async (req, res) => {
try {
const { name } = req.query;
if (!name) return res.status(400).json({ error: 'Missing name' });
const data = await ZingMp3.getArtist(String(name));
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/artist-songs', async (req, res) => {
try {
const { id, page = '1', count = '15' } = req.query;
if (!id) return res.status(400).json({ error: 'Missing id' });
const data = await ZingMp3.getListArtistSong(String(id), String(page), String(count));
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/lyric', async (req, res) => {
try {
const { id } = req.query;
if (!id) return res.status(400).json({ error: 'Missing id' });
const data = await ZingMp3.getLyric(String(id));
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/search', async (req, res) => {
try {
const { q } = req.query;
if (!q) return res.status(400).json({ error: 'Missing q' });
const data = await ZingMp3.search(String(q));
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/list-mv', async (req, res) => {
try {
const { id, page = '1', count = '15' } = req.query;
if (!id) return res.status(400).json({ error: 'Missing id' });
const data = await ZingMp3.getListMV(String(id), String(page), String(count));
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/category-mv', async (req, res) => {
try {
const { id } = req.query;
if (!id) return res.status(400).json({ error: 'Missing id' });
const data = await ZingMp3.getCategoryMV(String(id));
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.get('/api/video', async (req, res) => {
try {
const { id } = req.query;
if (!id) return res.status(400).json({ error: 'Missing id' });
const data = await ZingMp3.getVideo(String(id));
res.json(data);
} catch (e) {
res.status(e?.response?.status || 500).json({ error: e?.message || 'Internal Error' });
}
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});