Write a small script to download teahour audio using python

  • 2020-06-01 10:11:14
  • OfStack

preface

Recently, in my spare time, I saw a small station I had been paying attention to before, http:// teahour.fm /,1. I wanted to listen to all the audio here once, but suddenly I have been doing nothing for two years. A little sad, so I wrote a script, grabbed the audio download link, after downloading down 1 must listen carefully.

Time is short, and debugging is also so 10 minutes, the script may be some bad, you can point out the message.

teahour.py


 #!/usr/bin/env python
 #coding: utf-8
 
 import sys
 import requests
 from BeautifulSoup import BeautifulSoup
 
 reload(sys)
 sys.setdefaultencoding( "utf-8" )
 
 def parse_index():
  l = []
  r = requests.get("http://teahour.fm/")
  text = r.text
  html = BeautifulSoup(text)
  a = html.findAll("ul")[-3].findAll("a")
  for item in a:
   url = "".join(["http://teahour.fm",item.attrs[0][1]])
   title = item.text
   title = title.strip("#")
   l.append((title,url))
  return l
 
 def write(lines):
  with open("teahour.sh","a") as fh:
   fh.writelines(lines)
 
 def parse_sub(t):
  title,url = t
  r = requests.get(url)
  text = r.text
  html = BeautifulSoup(text)
  audio_url = html.find("audio").attrs[-1][-1]
  ext = audio_url.split(".")[-1]
  line = "wget '%s' -SO '%s.%s'\n" % (audio_url,title,ext)
  #print line
  return line
 
 def main():
  lines = []
  l = parse_index()
  for t in l:
   line = parse_sub(t)
   lines.append(line)
  write(lines)
 
 if __name__ == "__main__":
  main()

Part of the text teahour.sh reads as follows:


peiqiang@budongdeMacBook-Pro:~/Downloads/teahour$ cat teahour.sh
wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_89.m4a' -SO '89  Let's talk about how to build a lean startup team .m4a'
wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_88.m4a' -SO '88  With modao founder zhang yuan 1 Business from chatting  0  to  1.m4a'
wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_87.m4a' -SO '87  Talk to gao chunhui, an Internet veteran  IPIP.net.m4a'
wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_86.mp3' -SO '86  Talk about entrepreneurship and technology with "nuggets" founder Yin Ming .mp3'
wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_85.mp3' -SO '85  This time we're talking about telecommuting .mp3'

Part of the sh teahour.sh download is as follows:


peiqiang@budongdeMacBook-Pro:~/Downloads/teahour$ ll
total 1077896
-rw-r--r-- 1 peiqiang staff 130416640 3 16 20:02 83  This time we'll talk about cool Ethereum.m4a
-rw-r--r-- 1 peiqiang staff 109631418 3 3 2016 84  To continue with  Robbin  Talk about the strategies and tactics of tech people .m4a
-rw-r--r-- 1 peiqiang staff 89519924 6 12 2016 85  This time we're talking about telecommuting .mp3
-rw-r--r-- 1 peiqiang staff 87766246 8 8 2016 86  Talk about entrepreneurship and technology with "nuggets" founder Yin Ming .mp3
-rw-r--r-- 1 peiqiang staff 59058564 9 11 2016 87  Talk to gao chunhui, an Internet veteran  IPIP.net.m4a
-rw-r--r-- 1 peiqiang staff 32408522 10 26 15:55 88  With modao founder zhang yuan 1 Business from chatting  0  to  1.m4a
-rw-r--r-- 1 peiqiang staff 43051575 11 8 18:49 89  Let's talk about how to build a lean startup team .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

conclusion


Related articles: