写了个小脚本下载teahour音频

快下班时逛了the5fire的博客,看到了之前我就关注的一个小站http://teahour.fm/,一直想把这里的音频都听一遍,可转眼间怎么着也有两年了,却什么也没做。有些伤感,于是马马虎虎写了个脚本,抓了下音频的下载链接,等下载下来后一定要认真听听。

时间仓促,加调试也就那么十几分钟,脚本写的很烂,以后有时间,再修修。

teahour.py

 1 #!/usr/bin/env python
 2 #coding: utf-8
 3 
 4 import sys
 5 import requests
 6 from BeautifulSoup import BeautifulSoup
 7 
 8 reload(sys)
 9 sys.setdefaultencoding( "utf-8" )
10 
11 def parse_index():
12     l = []
13     r = requests.get("http://teahour.fm/")
14     text = r.text
15     html = BeautifulSoup(text)
16     a = html.findAll("ul")[-3].findAll("a")
17     for item in a:
18         url = "".join(["http://teahour.fm",item.attrs[0][1]])
19         title = item.text
20         title = title.strip("#")
21         l.append((title,url))
22     return l
23 
24 def write(lines):
25     with open("teahour.sh","a") as fh:
26         fh.writelines(lines)
27 
28 def parse_sub(t):
29     title,url = t
30     r = requests.get(url)
31     text = r.text
32     html = BeautifulSoup(text)
33     audio_url = html.find("audio").attrs[-1][-1]
34     ext = audio_url.split(".")[-1]
35     line = "wget '%s' -SO '%s.%s'\n" % (audio_url,title,ext)
36     #print line
37     return line
38 
39 def main():
40     lines = []
41     l = parse_index()
42     for t in l:
43         line = parse_sub(t)
44         lines.append(line)
45     write(lines)
46 
47 if __name__ == "__main__":
48     main()

文本teahour.sh的部分内容如下:

peiqiang@budongdeMacBook-Pro:~/Downloads/teahour$ cat teahour.sh
wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_89.m4a' -SO '89 和玎玎聊聊如何搭建精益创业团队.m4a'
wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_88.m4a' -SO '88 与墨刀创始人张元一聊创业从 0 到 1.m4a'
wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_87.m4a' -SO '87 和互联网老兵高春辉聊 IPIP.net.m4a'
wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_86.mp3' -SO '86 和『掘金』创始人阴明聊聊创业和技术.mp3'
wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_85.mp3' -SO '85 这次我们聊聊远程工作.mp3'

执行脚本sh teahour.sh下载的部分内容如下:

peiqiang@budongdeMacBook-Pro:~/Downloads/teahour$ ll
total 1077896
-rw-r--r--  1 peiqiang  staff  130416640  3 16 20:02 83 这次我们聊聊超酷的Ethereum.m4a
-rw-r--r--  1 peiqiang  staff  109631418  3  3  2016 84 继续跟 Robbin 聊聊技术人的战略与战术.m4a
-rw-r--r--  1 peiqiang  staff   89519924  6 12  2016 85 这次我们聊聊远程工作.mp3
-rw-r--r--  1 peiqiang  staff   87766246  8  8  2016 86 和『掘金』创始人阴明聊聊创业和技术.mp3
-rw-r--r--  1 peiqiang  staff   59058564  9 11  2016 87 和互联网老兵高春辉聊 IPIP.net.m4a
-rw-r--r--  1 peiqiang  staff   32408522 10 26 15:55 88 与墨刀创始人张元一聊创业从 0 到 1.m4a
-rw-r--r--  1 peiqiang  staff   43051575 11  8 18:49 89 和玎玎聊聊如何搭建精益创业团队.m4a
-rwxr-xr-x@ 1 peiqiang  staff       1055  3 16 20:02 teahour.py
-rw-r--r--  1 peiqiang  staff      11465  3 16 19:57 teahour.sh