Guest User

Untitled

a guest
Oct 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. import thread, os, urllib, BeautifulSoup, eyeD3, HTMLParser
  4. from datetime import datetime
  5.  
  6. html_parser = HTMLParser.HTMLParser()
  7. ue = lambda s: html_parser.unescape(s)
  8.  
  9. soup = None
  10. filename =None
  11.  
  12. from Tkinter import *
  13. import ttk
  14. root = Tk()
  15.  
  16. url = StringVar()
  17. progress = DoubleVar()
  18. status = StringVar()
  19.  
  20. def getLyric():
  21. bt.config(state='normal')
  22.  
  23. p = soup.find('div',{'id':'articleContent'})
  24. for t in p.findAll(True):
  25. t.hidden = True
  26. content = []
  27. for t in p.findAll(recursive=False):
  28. if t.name=='p':
  29. content.append(ue(t.renderContents()).strip())
  30. lyric = '\n'.join(content)
  31. tag = eyeD3.Tag()
  32. tag.link(file(filename))
  33. tag.encoding = '\x01'
  34. tag.removeLyrics()
  35. tag.addLyrics(lyric)
  36. tag.update()
  37. status.set('Lyric OK!')
  38.  
  39. def dl_status(blocks_read,block_size,total_size):
  40. p = 100.0*blocks_read*block_size/total_size
  41. progress.set(p)
  42. status.set(('%.0f' % p)+'%')
  43. if p>=100.0:
  44. status.set('Download OK!')
  45. getLyric()
  46.  
  47. def bt_cmd():
  48. global soup, filename
  49. try:
  50. bt.config(state='disabled')
  51. html = urllib.urlopen(url.get()).read()
  52. soup = BeautifulSoup.BeautifulSoup(html)
  53. status.set('Initilize OK!')
  54. p = soup.find('p',{'id':'episodeLinks'})
  55. filename = url.get()[-8:]+'.mp3'
  56. if os.path.exists(filename):
  57. status.set('Skip downloading.')
  58. getLyric()
  59. else:
  60. foo = lambda n,i: urllib.urlretrieve(p.find('a')['href'], filename, dl_status)
  61. thread.start_new_thread(foo, (1,1))
  62. status.set('Downloading!')
  63.  
  64. except Exception as e:
  65. status.set(str(e))
  66. bt.config(state='normal')
  67.  
  68. lb1 = Label(root, text = 'Url:')
  69. et = Entry(root, textvariable = url)
  70. pb = ttk.Progressbar(root, variable = progress)
  71. lb2 = Label(root, textvariable = status)
  72. bt = Button(root, text = 'Download', command = bt_cmd)
  73.  
  74. lb1.grid(row = 0, column = 0, sticky = W)
  75. et.grid(row = 1, column = 0, ipadx = 45)
  76. pb.grid(row = 2, column = 0, ipadx = 70)
  77. lb2.grid(row = 3, column = 0, sticky = W)
  78. bt.grid(row = 4, column = 0)
  79.  
  80. root.title('Science American 60s')
  81. root.mainloop()
Add Comment
Please, Sign In to add comment