-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetPodcast.py
executable file
·66 lines (54 loc) · 1.72 KB
/
getPodcast.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
#! /Users/yxj/env3/bin/python
# coding=utf-8
import requests
import re
import io
import os
import shutil
# 罗辑思维,晓松奇谈,吴晓波频道
fm_list = [
'http://www.lizhi.fm/17248/',
'http://www.lizhi.fm/679472/',
'http://www.lizhi.fm/176647/'
]
def requests_file(file_url, filename):
r = requests.get(file_url)
if r.status_code == requests.codes.ok:
with io.open(filename, 'wb') as file:
file.write(r.content)
file.flush()
file.close()
def move_episodes():
source = os.listdir('.')
for file in source:
if file.endswith('.mp3'):
index = file.find('_')
folder_name = file[:index]
if folder_name not in source:
os.mkdir(folder_name)
destination = './' + folder_name + '/'
shutil.move(file, destination)
def main():
for fm in fm_list:
r = requests.get(fm)
r1 = r'data-url="(.*?\.mp3)"'
r1_comp = re.compile(r1)
r2 = r'data-radio-name="(.*?)"'
r2_comp = re.compile(r2)
r3 = r'data-title="(.*?)"'
r3_comp = re.compile(r3)
first_mp3_url = re.findall(r1_comp, r.text)[0]
radio_name = re.findall(r2_comp, r.text)[0]
title = re.findall(r3_comp, r.text)[0]
final_filename = radio_name + '_' + title + '.mp3'
f = open('latest.txt', 'r+')
if (final_filename + '\n') not in f.readlines():
print("downloading " + final_filename)
requests_file(first_mp3_url, final_filename)
f.write(final_filename + '\n')
f.flush()
f.close()
move_episodes()
print("done")
if __name__ == '__main__':
main()