PageRenderTime 249ms CodeModel.GetById 4ms RepoModel.GetById 5ms app.codeStats 0ms

/mytube.py

https://github.com/arkem/hottest100
Python | 114 lines | 101 code | 2 blank | 11 comment | 1 complexity | e5a2dcc41819514e6ca066f61b6b8383 MD5 | raw file
  1. """
  2. TODO:
  3. - interface with mayer via named pipe
  4. - implement limit
  5. - pagination of results
  6. - randomisation of tracks
  7. - looping in interactive mode
  8. """
  9. import urllib
  10. import urllib2
  11. import re
  12. import htmllib
  13. import os
  14. import sys
  15. search_page = 'http://www.youtube.com/results?search_query='
  16. search_phrase = ""
  17. limit = 40
  18. home_dir = "~/"
  19. prog_dir = ".mytube"
  20. playlist = []
  21. delim = "/^^||^^/"
  22. video_output = ""
  23. def strip_tags(value):
  24. "Return the given HTML with all tags stripped."
  25. return re.sub(r'<[^>]*?>', '', value)
  26. def fetch(url):
  27. r = urllib2.Request(url)
  28. response = urllib2.urlopen(r)
  29. html = response.read()
  30. return html
  31. def unescape(s):
  32. try:
  33. p = htmllib.HTMLParser(None)
  34. p.save_bgn()
  35. p.feed(s)
  36. return p.save_end()
  37. except Exception, e:
  38. print "Error in unescape(): %s" % e
  39. return ""
  40. def search(search_phrase,limit):
  41. global playlist
  42. global delim
  43. search_phrase = re.sub(" ","+",search_phrase)
  44. search_url = search_page + search_phrase
  45. print search_url
  46. r = fetch(search_url)
  47. video_link = re.compile("""
  48. .*href="/watch.v=
  49. ([^"]*)" #video id
  50. \s*rel="nofollow">
  51. <img\s*title="
  52. ([^"]*) #title
  53. """,re.VERBOSE|re.I)
  54. count = 1
  55. for line in r.split('\n'):
  56. m = re.match(video_link, line)
  57. if m:
  58. video_id = m.group(1)
  59. title = m.group(2)
  60. #print "%s\t%s" % (count, title)
  61. playlist.append("%s%s%s" % (video_id, delim, title))
  62. count += 1
  63. playlist_select(playlist)
  64. def play_song(video_id,title):
  65. global video_output
  66. t_value = re.compile(""".*swfArgs.*
  67. "t":\s*
  68. "([^"]*)"
  69. """,re.VERBOSE|re.I)
  70. url = "http://youtube.com/watch?v=%s" % (video_id)
  71. print url
  72. result = fetch(url)
  73. for line in result.split('\n'):
  74. m = re.match(t_value, line)
  75. if m:
  76. url = "%s/get_video.php?l=165&video_id=%s&t=%s" \
  77. % ("http://youtube.com", video_id, m.group(1))
  78. print url
  79. os.system("mplayer %s \"%s\"" % (video_output, url))
  80. #urllib.urlretrieve(url, dl_dir + "/" + torrent_num + ".torrent")
  81. def playlist_select(playlist):
  82. global delim
  83. for i in range(1, len(playlist)):
  84. print "%s:\t%s" % (i, playlist[i].split(delim)[1])
  85. response = input("Enter number to play: ")
  86. (video_id,title) = playlist[int(response)].split(delim)
  87. play_song(video_id, title)
  88. for i in range(1,len(sys.argv)):
  89. if sys.argv[i].startswith("-l"):
  90. limit = int(sys.argv[i].lstrip("-l"))
  91. continue
  92. elif sys.argv[i].startswith("-n"):
  93. video_output = "-vo null"
  94. continue
  95. search_phrase += sys.argv[i] + " "
  96. search_phrase = search_phrase.rstrip(" ")
  97. search(search_phrase,limit)