-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfairytails.js
220 lines (195 loc) · 6.29 KB
/
fairytails.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const puppeteer = require("puppeteer");
const request = require("request");
const fs = require("fs");
const axios = require("axios");
const yargs = require("yargs");
const { hideBin } = require("yargs/helpers");
const MEDIA_URL = "https://api.rtvslo.si/ava/getMedia";
const FAIRY_TAIL_URL = "https://ziv-zav.rtvslo.si/predvajaj/lahko-noc-otroci";
const FAIRY_TAIL_API_ENDPOINT =
"https://api.rtvslo.si/ava/getSearch2?client_id=82013fb3a531d5414f478747c1aca622&showId=54&clip=show&sort=date&order=desc";
async function getMediaUrl(url) {
let mp3Url = null;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on("request", (interceptedRequest) => {
if (interceptedRequest.isInterceptResolutionHandled()) {
return;
}
if (interceptedRequest.url().includes(MEDIA_URL)) {
mp3Url = interceptedRequest.url();
interceptedRequest.abort();
} else {
interceptedRequest.continue();
}
});
await page.goto(url);
await browser.close();
return mp3Url;
}
async function getTitle(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const websiteContent = await page.content();
const regex = /<h1 class=\"funky-font h3\">(.*?)<\/h1>/;
const match = websiteContent.match(regex);
await browser.close();
return match[1];
}
async function getMp3Url(url) {
try {
url = await getMediaUrl(url);
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const websiteContent = await page.content();
await browser.close();
const regex = /\"https"\:\"(.*?)\?/;
const match = websiteContent.match(regex);
return match[1];
} catch (err) {
console.log("Could not resolve the browser instance => ", err);
}
}
async function downloadMp3(id, title, date) {
const url = `${FAIRY_TAIL_URL}/${id}`;
const filePath = `downloads/${title}.mp3`;
// // Check if file already exists in the downloads directory
// if (fs.existsSync(filePath)) {
// console.log(`File ${filePath} already exists.`);
// return true;
// }
// Read the downloaded.txt file and check if the title already exists
let downloadedTitles = [];
if (fs.existsSync("downloaded.txt")) {
downloadedTitles = fs.readFileSync("downloaded.txt", "utf8").split("\n");
}
if (downloadedTitles.includes(title)) {
console.log(`Title "${title}" already exists in downloaded.txt.`);
fs.writeFile("date.txt", date, function (err) {
if (err) throw err;
});
return true;
}
try {
const mp3Url = await getMp3Url(url);
request.get(mp3Url).pipe(fs.createWriteStream(filePath));
// After successful try execution, store the title in a downloaded.txt file
fs.appendFile("downloaded.txt", title + "\n", function (err) {
if (err) throw err;
});
} catch (err) {
console.log(err);
}
fs.writeFile("date.txt", date, function (err) {
if (err) throw err;
});
return false;
}
//downloadMp3("https://ziv-zav.rtvslo.si/predvajaj/lahko-noc-otroci/174867111");
async function scrapeFairyTaleIds(url) {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const websiteContent = await page.content();
await browser.close();
const regex = /\d{9}/g;
const match = websiteContent.match(regex);
return [...new Set(match)];
} catch (err) {
console.log("Could not resolve the browser instance => ", err);
}
}
async function getFairyTales(last = 1) {
return axios
.get(FAIRY_TAIL_API_ENDPOINT, {
params: {
pageSize: last,
},
})
.then((res) => {
return res.data.response.recordings;
})
.catch((error) => {
console.error(error);
});
}
async function downloadLastFairyTale() {
const fairytales = await getFairyTales();
await downloadMp3(
fairytales[0].id,
fairytales[0].title,
fairytales[0].prettyDates.iso
);
}
// dowload last 10 fairytales
async function downloadLatest(last, date) {
let fairytales = await getFairyTales(last);
// Revert the array
fairytales = fairytales.reverse();
// Remove objects from the array until fairy_tale.prettyDates.iso equals date
const index = fairytales.findIndex(
(fairy_tale) => fairy_tale.prettyDates.iso === date
);
if (index !== -1) {
fairytales = fairytales.slice(index + 1);
}
// Count how many objects remain in the array and log the number out
console.log(`Number of fairytales to download: ${fairytales.length}`);
// Initialize progress counter
let progressCounter = 0;
// For is waiting for promise to resolve
for (const fairy_tale of fairytales) {
let title = fairy_tale.title;
// Check if title contains / and replace it with -
if (title.includes("/")) {
title = title.replace(/\//g, "-");
}
// Start and wait for file to be downloaded
const fileExists = await downloadMp3(
fairy_tale.id,
title,
fairy_tale.prettyDates.iso
);
if (!fileExists) {
// Wait for 10 seconds after file is downloaded before it starts downloading the new one
await new Promise((resolve) => setTimeout(resolve, 10000));
}
// Increment progress counter
progressCounter++;
// Calculate progress percentage
const progressPercentage = (progressCounter / fairytales.length) * 100;
// Log progress bar
if (!fileExists) {
console.log(
`Progress: ${progressPercentage.toFixed(2)}% - ${
fairy_tale.prettyDates.iso
}: ${title} -> downloaded.`
);
}
}
}
const argv = yargs(hideBin(process.argv)).argv;
if (typeof argv.latest !== "undefined") {
downloadLastFairyTale();
} else {
// Read date from date.txt file if it exists and is not empty
let date = null;
if (fs.existsSync("date.txt")) {
const fileContent = fs.readFileSync("date.txt", "utf8");
if (fileContent && !isNaN(Date.parse(fileContent))) {
date = fileContent;
}
}
if (date !== null) {
// Log the date read from the file
console.log(`Downloading from ${date} onwards`);
}
var last = typeof argv.last !== "undefined" ? argv.last : 1400;
// var date = typeof argv.date !== "undefined" ? argv.date : null;
// last = last > 200 ? 200 : last;
downloadLatest(last, date);
}