forked from thebells1111/curiohoster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi.js
94 lines (77 loc) · 2.56 KB
/
pi.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
94
import dotenv from "dotenv";
import axios from "axios";
import crypto from "crypto";
import fs from "fs";
if (!process.env.API_KEY) {
dotenv.config();
}
const { API_KEY, API_SECRET } = process.env;
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");
// ======== Send the request and collect/show the results ========
const headers = {
"X-Auth-Date": apiHeaderTime.toString(),
"X-Auth-Key": API_KEY,
Authorization: hash4Header,
"User-Agent": "CurioHoster",
};
const baseUrl = "https://api.podcastindex.org/api/1.0/";
const query = "podcasts/bymedium?medium=music&val=lightning&max=2000";
const url = baseUrl + query;
console.log(url); // Logging the URL for debugging
const response = await axios.get(url, { headers: headers });
if (response && response.data) {
let songs = [];
const data = response.data;
let fetchedFeeds = data.feeds || data.feed || [];
let feeds = fetchedFeeds.filter((v) => {
let addFeed = true;
if (
//this removes 100% Retro Live Feed
[5718023].find((w) => v.id === w) ||
v.author === "Gabe Barrett"
) {
addFeed = false;
}
return addFeed;
});
// console.log(feeds);
for (let i = 0; i < feeds.length; i++) {
let feed = feeds[i];
const epQuery = `/episodes/bypodcastguid?guid=${feed.podcastGuid}`;
const epUrl = baseUrl + epQuery;
const epResponse = await axios.get(epUrl, { headers: headers });
const epData = epResponse.data;
let s = epData.items.map((v) => {
let w = {};
w.album = feed.title;
w.albumArt = feed.image || feed.artwork;
w.albumUpdateTime = feed.lastUpdateTime;
w.artist = feed.author;
w.podcastGuid = feed.podcastGuid;
w.title = v.title;
w.guid = v.guid;
w.datePublished = v.datePublished;
w.enclosureUrl = v.enclosureUrl;
return w;
});
songs = songs.concat(s);
}
songs = songs.sort((a, b) => {
if (a.datePublished < b.datePublished) return 1;
if (a.datePublished > b.datePublished) return -1;
return 0;
});
console.log(songs.length);
fs.writeFileSync("feeds.json", JSON.stringify(songs, null, 2));
console.log("done!!!");
}
} catch (err) {
console.error("queryindex err:", err.message);
console.log(err);
}