Posted By

abhiomkar on 05/25/08


Tagged

python download batch YouTube


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

abhiomkar
taboularasa
Mithun
mustapha
Balamir


YouTube Batch Download Script


 / Published in: Python
 

You

  1. #!/usr/bin/python
  2. import sgmllib
  3. import sys
  4.  
  5. class MyParser(sgmllib.SGMLParser):
  6. "A simple parser class."
  7.  
  8. def parse(self, s):
  9. "Parse the given string 's'."
  10. self.feed(s)
  11. self.close()
  12.  
  13. def __init__(self, verbose=0):
  14. "Initialise an object, passing 'verbose' to the superclass."
  15.  
  16. sgmllib.SGMLParser.__init__(self, verbose)
  17. self.hyperlinks = []
  18.  
  19. def start_a(self, attributes):
  20. "Process a hyperlink and its 'attributes'."
  21.  
  22. for name, value in attributes:
  23. if name == "href":
  24. if value.startswith("/watch?v="):
  25. self.hyperlinks.append("youtube-dl -t http://youtube.com"+value)
  26.  
  27. def get_hyperlinks(self):
  28. "Return the list of hyperlinks."
  29.  
  30. return self.hyperlinks
  31.  
  32. def f2(seq):
  33. # order preserving
  34. checked = []
  35. for e in seq:
  36. if e not in checked:
  37. checked.append(e)
  38. return checked
  39.  
  40. import urllib, sgmllib
  41.  
  42. # Get something to work with.
  43. f = urllib.urlopen(sys.argv[1])
  44. s = f.read()
  45.  
  46. # Try and process the page.
  47. # The class should have been defined first, remember.
  48. myparser = MyParser()
  49. myparser.parse(s)
  50.  
  51. # Get the hyperlinks.
  52. print f2(myparser.get_hyperlinks())
  53. yt = open("youtube_links","w")
  54.  
  55. for link in f2(myparser.get_hyperlinks()):
  56. yt.write(link)
  57. yt.write("\n")
  58.  
  59. print "\nYouTube Batch Download Script \"youtube_links\" has been generated. Execute it !"

Report this snippet  

You need to login to post a comment.