-
Notifications
You must be signed in to change notification settings - Fork 2
/
queryindex.js
66 lines (53 loc) · 1.68 KB
/
queryindex.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
import dotenv from "dotenv";
import axios from "axios";
import crypto from "crypto";
if (!process.env.API_KEY) {
dotenv.config();
}
const { API_KEY, API_SECRET } = process.env;
async function queryindex(req, res, next) {
let { data } = req.body;
try {
// ======== Hash them to get the Authorization token ========
const apiHeaderTime = Math.floor(Date.now() / 1000);
const sha1Hash = crypto.createHash("sha1");
const data4Hash = API_KEY + API_SECRET + apiHeaderTime;
sha1Hash.update(data4Hash);
const hash4Header = sha1Hash.digest("hex");
// ======== Construct Axios Request ========
const headers = {
"X-Auth-Date": apiHeaderTime.toString(),
"X-Auth-Key": API_KEY,
Authorization: hash4Header,
"User-Agent": "CurioHoster",
};
if (data) {
headers["Content-Type"] = "application/json";
}
const baseUrl = "https://api.podcastindex.org/api/1.0/";
const q = req.query.q;
const url = baseUrl + q;
console.log(url); // Logging the URL for debugging
// const response = await axios({
// method: data ? "POST" : "GET",
// url: url,
// headers: headers,
// data: data ? data : undefined,
// });
const response = await axios.get(url, { headers: headers });
if (response && response.data) {
if (data) {
console.log(response.data);
}
console.log(response);
console.log(response.data);
res.status(response.status).json(response.data);
} else {
res.status(404).json([]);
}
} catch (err) {
console.error("queryindex err:", err.message);
res.status(500).json({ message: "Server Error" });
}
}
export default queryindex;