Instantly share code, notes, and snippets.

Embed
What would you like to do?
60-Second Science downloader
# coding: utf-8
import thread, os, urllib, BeautifulSoup, eyeD3, HTMLParser
from datetime import datetime
html_parser = HTMLParser.HTMLParser()
ue = lambda s: html_parser.unescape(s)
soup = None
filename =None
from Tkinter import *
import ttk
root = Tk()
url = StringVar()
progress = DoubleVar()
status = StringVar()
def getLyric():
bt.config(state='normal')
p = soup.find('div',{'id':'articleContent'})
for t in p.findAll(True):
t.hidden = True
content = []
for t in p.findAll(recursive=False):
if t.name=='p':
content.append(ue(t.renderContents()).strip())
lyric = '\n'.join(content)
tag = eyeD3.Tag()
tag.link(file(filename))
tag.encoding = '\x01'
tag.removeLyrics()
tag.addLyrics(lyric)
tag.update()
status.set('Lyric OK!')
def dl_status(blocks_read,block_size,total_size):
p = 100.0*blocks_read*block_size/total_size
progress.set(p)
status.set(('%.0f' % p)+'%')
if p>=100.0:
status.set('Download OK!')
getLyric()
def bt_cmd():
global soup, filename
try:
bt.config(state='disabled')
html = urllib.urlopen(url.get()).read()
soup = BeautifulSoup.BeautifulSoup(html)
status.set('Initilize OK!')
p = soup.find('p',{'id':'episodeLinks'})
filename = url.get()[-8:]+'.mp3'
if os.path.exists(filename):
status.set('Skip downloading.')
getLyric()
else:
foo = lambda n,i: urllib.urlretrieve(p.find('a')['href'], filename, dl_status)
thread.start_new_thread(foo, (1,1))
status.set('Downloading!')
except Exception as e:
status.set(str(e))
bt.config(state='normal')
lb1 = Label(root, text = 'Url:')
et = Entry(root, textvariable = url)
pb = ttk.Progressbar(root, variable = progress)
lb2 = Label(root, textvariable = status)
bt = Button(root, text = 'Download', command = bt_cmd)
lb1.grid(row = 0, column = 0, sticky = W)
et.grid(row = 1, column = 0, ipadx = 45)
pb.grid(row = 2, column = 0, ipadx = 70)
lb2.grid(row = 3, column = 0, sticky = W)
bt.grid(row = 4, column = 0)
root.title('Science American 60s')
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment