-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
93 lines (78 loc) · 2.9 KB
/
server.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
const express = require('express');
const path = require('path');
const dotenv = require('dotenv');
const CryptoJS = require('crypto-js');
const fetch = require('node-fetch');
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
const authKey = process.env.AUTH_KEY;
const secretKey = process.env.SECRET_KEY;
const userAgent = process.env.USER_AGENT;
const apiEndpoint = process.env.API_ENDPOINT;
app.use(express.static(path.join(__dirname, 'public')));
// Shared authentication function
function generateAuthHeaders() {
const apiHeaderTime = Math.floor(new Date().getTime() / 1000);
const hash = CryptoJS.SHA1(authKey + secretKey + apiHeaderTime).toString(CryptoJS.enc.Hex);
return {
'User-Agent': userAgent,
'X-Auth-Key': authKey,
'X-Auth-Date': apiHeaderTime.toString(),
'Authorization': hash
};
}
// Search for podcasts
app.get('/api/search', async (req, res) => {
const query = req.query.q;
if (!query) {
return res.status(400).json({ error: 'Query parameter is required' });
}
const headers = generateAuthHeaders();
try {
const response = await fetch(`${apiEndpoint}/search/byterm?q=${encodeURIComponent(query)}`, {
method: 'GET',
headers: headers
});
if (response.ok && response.headers.get('content-type').includes('application/json')) {
const data = await response.json();
res.json(data);
} else {
const rawText = await response.text();
console.log('Raw response:', rawText);
res.status(500).json({ error: 'Invalid response from API', rawText });
}
} catch (error) {
console.error('Error fetching API:', error.message);
res.status(500).json({ error: error.message });
}
});
// Fetch episodes by podcast ID
app.get('/api/episodes', async (req, res) => {
const feedId = req.query.feedId;
const max = req.query.max;
if (!feedId) {
return res.status(400).json({ error: 'Feed ID parameter is required' });
}
const headers = generateAuthHeaders();
try {
const response = await fetch(`${apiEndpoint}/episodes/byitunesid?id=${encodeURIComponent(feedId)}&max=${max}`, {
method: 'GET',
headers: headers
});
if (response.ok && response.headers.get('content-type').includes('application/json')) {
const data = await response.json();
res.json(data);
} else {
const rawText = await response.text();
console.log('Raw response:', rawText);
res.status(500).json({ error: 'Invalid response from API', rawText });
}
} catch (error) {
console.error('Error fetching API:', error.message);
res.status(500).json({ error: error.message });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});