-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (47 loc) · 1.84 KB
/
server.js
File metadata and controls
55 lines (47 loc) · 1.84 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
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3000;
const API_KEY = '495078pztmrjyz299bmkd6';
app.use(cors());
// Convert seconds to minutes and seconds format
function formatDuration(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${minutes}m ${remainingSeconds}s`;
}
// Fetch videos from Doodstream with pagination
app.get('/api/videos', async (req, res) => {
const page = parseInt(req.query.page) || 1;
const perPage = parseInt(req.query.perPage) || 20;
try {
const response = await axios.get(`https://doodapi.com/api/file/list?key=${API_KEY}&page=${page}&per_page=${perPage}`);
const videos = response.data.result.files.map(video => ({
...video,
formatted_length: formatDuration(video.length)
}));
res.json({ ...response.data, result: { ...response.data.result, files: videos } });
} catch (error) {
console.error('Error fetching videos:', error.message);
if (error.response) {
res.status(error.response.status).json({ error: error.response.data });
} else if (error.request) {
res.status(500).json({ error: 'No response from Doodstream' });
} else {
res.status(500).json({ error: 'Failed to fetch videos' });
}
}
});
// Handle 404 errors
app.use((req, res) => {
res.status(404).json({ error: 'Endpoint not found' });
});
// Global error handler
app.use((err, req, res, next) => {
console.error('Server error:', err.message);
res.status(500).json({ error: 'Internal server error' });
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});