-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep-0.py
459 lines (426 loc) · 8.96 KB
/
step-0.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# scraper to scrape the metadata from rthk.hk podcast page
import os
import requests
import pandas as pd
from tqdm import tqdm
import random
from urllib.parse import quote
import xml.etree.ElementTree as ET
import multiprocessing
import argparse
import audioread
import librosa
import soundfile as sf
#
headers = {
"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Chrome/56.0.2924.87",
"Connection": "close",
}
# Example URL:
# https://podcast.rthk.hk/podcast/episodeList.php?pid=447&display=all&year=2020
# Example format
# <?xml version="1.0" encoding="utf-8"?>
# <episodeList>
# <total>306</total>
# <remainder>0</remainder>
# <page>0</page>
# <count>306</count>
# <episodePerPage>12</episodePerPage>
# <episode>
# <pid>447</pid>
# <eid>173854</eid>
# <episodeTitle><![CDATA[中國現代作家選集 蕭紅 (第二十六至三十集)]]></episodeTitle>
# <episodeDate>2020-12-31</episodeDate>
# <cover><![CDATA[https://podcast.rthk.hk/podcast/upload_photo/item_photo/16x9_275x155_447.jpg]]></cover>
# <duration>00:51:39</duration>
# <mediafile><![CDATA[https://podcasts.rthk.hk/podcast/media/audiobook/447_2101071456_35804.mp3]]></mediafile>
# <format>audio</format>
# </episode>
# ...
# </episodeList>
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
eids = {
541: [
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013,
],
1843: [
2025,
2024,
2023,
2022,
2021,
2020,
],
292: [2021, 2020],
1223: [
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
],
42: [
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013,
2012,
2011,
2010,
2009,
2008,
2007,
2006,
2005,
],
825: [
2025,
2024,
2023,
2022,
2021,
],
836: [
2025,
2024,
],
837: [
2025,
2024,
2023,
2022,
2021,
],
916: [
2025,
2024,
2023,
2022,
],
914: [
2025,
2024,
2023,
2022,
],
918: [
2025,
2024,
2023,
2022,
],
919: [
2025,
2024,
2023,
2022,
],
293: [ # 十萬八千里
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
],
336: [
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013,
2012,
2011,
],
1886: [2021, 2022, 2023, 2024],
447: [2021, 2020, 2019, 2018, 2017],
23: [
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013,
2012,
2011,
2010,
2009,
2008,
2007,
2006,
2005,
],
833: [
2025,
2024,
2023,
2022,
2021,
],
902: [
2025,
2024,
2023,
2022,
2021,
],
662: [2017, 2016, 2015, 2014],
207: [
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013,
2012,
],
236: [
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013,
2012,
2011,
2010,
],
308: [
2025,
2024,
2023,
2022,
2021,
2020,
],
289: [2025, 2024],
307: [2025, 2024],
1788: [2025, 2024],
287: [
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013,
2012,
2011,
],
2121: [
2025,
2024,
2023,
],
356: [
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013,
2012,
],
826: [
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
],
1069: [
2025,
2024,
2023,
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
],
}
base_url = "https://podcast.rthk.hk/podcast/episodeList.php?pid={pid}&display=all"
def get_podcast_metadata(pid: int, year: int):
url = f"{base_url.format(pid=str(pid))}&year={str(year)}"
response = requests.get(url)
root = ET.fromstring(response.text)
episodes = root.findall("episode")
metadata = []
for episode in episodes:
title = episode.find("episodeTitle").text
eid = episode.find("eid").text
pid = episode.find("pid").text
date = episode.find("episodeDate").text
cover = episode.find("cover").text
duration = episode.find("duration").text
mediafile = episode.find("mediafile").text
metadata.append(
{
"eid": eid,
"pid": pid,
"title": title,
"date": date,
"cover": cover,
"duration": duration,
"mediafile": mediafile,
}
)
return metadata
def download_from_url(url, dst):
"""
@param: url to download file
@param: dst place to put the file
"""
attempt = 0
while attempt < 3:
try:
# socks5_proxy = f"socks5://{NODRVPN_SERVICE_USERNAME}:{NODRVPN_SERVICE_PASSWORD}@{proxy_server}:1080"
req = requests.get(
url,
headers=headers,
timeout=10,
# proxies={"https": http_proxy, "http": http_proxy},
)
with open(dst, "wb") as f:
f.write(req.content)
except Exception as e:
print(f"Error: {e}")
attempt += 1
else:
break
def convert_to_mono_16k_mp3(audio_file: str, out_file: str):
os.makedirs(os.path.dirname(out_file), exist_ok=True)
wav, _ = librosa.load(audio_file, sr=16_000)
sf.write(out_file, wav, 16_000, format="mp3")
def download_episode(episode):
mediafile = episode["mediafile"]
pid = episode["pid"]
eid = episode["eid"]
filename = mediafile.split("/")[-1]
extension = os.path.splitext(filename)[1]
out_file = f"audios/{pid}-{eid}{extension}"
out_16k_file = out_file.replace("audios/", "audios_16k/").replace(extension, ".mp3")
if os.path.exists(out_file):
try:
with audioread.audio_open(out_16k_file) as f:
duration = f.duration
except:
duration = 0
filesize = os.path.getsize(out_file)
if filesize > 1000 and duration > 10:
if os.path.exists(out_16k_file):
return
convert_to_mono_16k_mp3(out_file, out_16k_file)
return
try:
download_from_url(mediafile, out_file)
# if file size is too small(0.5mb), it is likely a 404 page
if os.path.getsize(out_file) < 1000:
os.remove(out_file)
print(f"Cannot download: {mediafile}, the file is too small.")
return
# convert to 16k
convert_to_mono_16k_mp3(out_file, out_16k_file)
except:
print(f"Cannot down: {mediafile}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--num_proc", type=int, default=2)
args = parser.parse_args()
if os.path.exists("metadata.csv"):
metadata = pd.read_csv("metadata.csv").to_dict("records")
else:
metadata = []
for pid, years in tqdm(eids.items()):
for year in years:
episodes = get_podcast_metadata(pid, year)
metadata.extend(episodes)
pd.DataFrame(metadata).to_csv("metadata.csv", index=False)
# download mp3 files using multiprocessing
with multiprocessing.Pool(processes=args.num_proc) as pool:
mp3_files = list(
tqdm(pool.imap(download_episode, metadata), total=len(metadata))
)
# without multiprocessing
for episode in tqdm(metadata):
download_episode(episode)
print("All files downloaded.")