bliptv.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_str,
  6. compat_urllib_request,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. clean_html,
  11. int_or_none,
  12. parse_iso8601,
  13. unescapeHTML,
  14. xpath_text,
  15. xpath_with_ns,
  16. )
  17. class BlipTVIE(InfoExtractor):
  18. _VALID_URL = r'https?://(?:\w+\.)?blip\.tv/(?:(?:.+-|rss/flash/)(?P<id>\d+)|((?:play/|api\.swf#)(?P<lookup_id>[\da-zA-Z+_]+)))'
  19. _TESTS = [
  20. {
  21. 'url': 'http://blip.tv/cbr/cbr-exclusive-gotham-city-imposters-bats-vs-jokerz-short-3-5796352',
  22. 'md5': '80baf1ec5c3d2019037c1c707d676b9f',
  23. 'info_dict': {
  24. 'id': '5779306',
  25. 'ext': 'm4v',
  26. 'title': 'CBR EXCLUSIVE: "Gotham City Imposters" Bats VS Jokerz Short 3',
  27. 'description': 'md5:9bc31f227219cde65e47eeec8d2dc596',
  28. 'timestamp': 1323138843,
  29. 'upload_date': '20111206',
  30. 'uploader': 'cbr',
  31. 'uploader_id': '679425',
  32. 'duration': 81,
  33. }
  34. },
  35. {
  36. # https://github.com/rg3/youtube-dl/pull/2274
  37. 'note': 'Video with subtitles',
  38. 'url': 'http://blip.tv/play/h6Uag5OEVgI.html',
  39. 'md5': '309f9d25b820b086ca163ffac8031806',
  40. 'info_dict': {
  41. 'id': '6586561',
  42. 'ext': 'mp4',
  43. 'title': 'Red vs. Blue Season 11 Episode 1',
  44. 'description': 'One-Zero-One',
  45. 'timestamp': 1371261608,
  46. 'upload_date': '20130615',
  47. 'uploader': 'redvsblue',
  48. 'uploader_id': '792887',
  49. 'duration': 279,
  50. }
  51. },
  52. {
  53. # https://bugzilla.redhat.com/show_bug.cgi?id=967465
  54. 'url': 'http://a.blip.tv/api.swf#h6Uag5KbVwI',
  55. 'md5': '314e87b1ebe7a48fcbfdd51b791ce5a6',
  56. 'info_dict': {
  57. 'id': '6573122',
  58. 'ext': 'mov',
  59. 'upload_date': '20130520',
  60. 'description': 'Two hapless space marines argue over what to do when they realize they have an astronomically huge problem on their hands.',
  61. 'title': 'Red vs. Blue Season 11 Trailer',
  62. 'timestamp': 1369029609,
  63. 'uploader': 'redvsblue',
  64. 'uploader_id': '792887',
  65. }
  66. },
  67. {
  68. 'url': 'http://blip.tv/play/gbk766dkj4Yn',
  69. 'md5': 'fe0a33f022d49399a241e84a8ea8b8e3',
  70. 'info_dict': {
  71. 'id': '1749452',
  72. 'ext': 'mp4',
  73. 'upload_date': '20090208',
  74. 'description': 'Witness the first appearance of the Nostalgia Critic character, as Doug reviews the movie Transformers.',
  75. 'title': 'Nostalgia Critic: Transformers',
  76. 'timestamp': 1234068723,
  77. 'uploader': 'NostalgiaCritic',
  78. 'uploader_id': '246467',
  79. }
  80. },
  81. {
  82. # https://github.com/rg3/youtube-dl/pull/4404
  83. 'note': 'Audio only',
  84. 'url': 'http://blip.tv/hilarios-productions/weekly-manga-recap-kingdom-7119982',
  85. 'md5': '76c0a56f24e769ceaab21fbb6416a351',
  86. 'info_dict': {
  87. 'id': '7103299',
  88. 'ext': 'flv',
  89. 'title': 'Weekly Manga Recap: Kingdom',
  90. 'description': 'And then Shin breaks the enemy line, and he&apos;s all like HWAH! And then he slices a guy and it&apos;s all like FWASHING! And... it&apos;s really hard to describe the best parts of this series without breaking down into sound effects, okay?',
  91. 'timestamp': 1417660321,
  92. 'upload_date': '20141204',
  93. 'uploader': 'The Rollo T',
  94. 'uploader_id': '407429',
  95. 'duration': 7251,
  96. 'vcodec': 'none',
  97. }
  98. },
  99. ]
  100. @staticmethod
  101. def _extract_url(webpage):
  102. mobj = re.search(r'<meta\s[^>]*https?://api\.blip\.tv/\w+/redirect/\w+/(\d+)', webpage)
  103. if mobj:
  104. return 'http://blip.tv/a/a-' + mobj.group(1)
  105. mobj = re.search(r'<(?:iframe|embed|object)\s[^>]*(https?://(?:\w+\.)?blip\.tv/(?:play/|api\.swf#)[a-zA-Z0-9_]+)', webpage)
  106. if mobj:
  107. return mobj.group(1)
  108. def _real_extract(self, url):
  109. mobj = re.match(self._VALID_URL, url)
  110. lookup_id = mobj.group('lookup_id')
  111. # See https://github.com/rg3/youtube-dl/issues/857 and
  112. # https://github.com/rg3/youtube-dl/issues/4197
  113. if lookup_id:
  114. urlh = self._request_webpage(
  115. 'http://blip.tv/play/%s' % lookup_id, lookup_id, 'Resolving lookup id')
  116. url = compat_urlparse.urlparse(urlh.geturl())
  117. qs = compat_urlparse.parse_qs(url.query)
  118. mobj = re.match(self._VALID_URL, qs['file'][0])
  119. video_id = mobj.group('id')
  120. rss = self._download_xml('http://blip.tv/rss/flash/%s' % video_id, video_id, 'Downloading video RSS')
  121. def _x(p):
  122. return xpath_with_ns(p, {
  123. 'blip': 'http://blip.tv/dtd/blip/1.0',
  124. 'media': 'http://search.yahoo.com/mrss/',
  125. 'itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd',
  126. })
  127. item = rss.find('channel/item')
  128. video_id = xpath_text(item, _x('blip:item_id'), 'video id') or lookup_id
  129. title = xpath_text(item, 'title', 'title', fatal=True)
  130. description = clean_html(xpath_text(item, _x('blip:puredescription'), 'description'))
  131. timestamp = parse_iso8601(xpath_text(item, _x('blip:datestamp'), 'timestamp'))
  132. uploader = xpath_text(item, _x('blip:user'), 'uploader')
  133. uploader_id = xpath_text(item, _x('blip:userid'), 'uploader id')
  134. duration = int_or_none(xpath_text(item, _x('blip:runtime'), 'duration'))
  135. media_thumbnail = item.find(_x('media:thumbnail'))
  136. thumbnail = (media_thumbnail.get('url') if media_thumbnail is not None
  137. else xpath_text(item, 'image', 'thumbnail'))
  138. categories = [category.text for category in item.findall('category') if category is not None]
  139. formats = []
  140. subtitles_urls = {}
  141. media_group = item.find(_x('media:group'))
  142. for media_content in media_group.findall(_x('media:content')):
  143. url = media_content.get('url')
  144. role = media_content.get(_x('blip:role'))
  145. msg = self._download_webpage(
  146. url + '?showplayer=20140425131715&referrer=http://blip.tv&mask=7&skin=flashvars&view=url',
  147. video_id, 'Resolving URL for %s' % role)
  148. real_url = compat_urlparse.parse_qs(msg.strip())['message'][0]
  149. media_type = media_content.get('type')
  150. if media_type == 'text/srt' or url.endswith('.srt'):
  151. LANGS = {
  152. 'english': 'en',
  153. }
  154. lang = role.rpartition('-')[-1].strip().lower()
  155. langcode = LANGS.get(lang, lang)
  156. subtitles_urls[langcode] = url
  157. elif media_type.startswith('video/'):
  158. formats.append({
  159. 'url': real_url,
  160. 'format_id': role,
  161. 'format_note': media_type,
  162. 'vcodec': media_content.get(_x('blip:vcodec')) or 'none',
  163. 'acodec': media_content.get(_x('blip:acodec')),
  164. 'filesize': media_content.get('filesize'),
  165. 'width': int_or_none(media_content.get('width')),
  166. 'height': int_or_none(media_content.get('height')),
  167. })
  168. self._check_formats(formats, video_id)
  169. self._sort_formats(formats)
  170. subtitles = self.extract_subtitles(video_id, subtitles_urls)
  171. return {
  172. 'id': video_id,
  173. 'title': title,
  174. 'description': description,
  175. 'timestamp': timestamp,
  176. 'uploader': uploader,
  177. 'uploader_id': uploader_id,
  178. 'duration': duration,
  179. 'thumbnail': thumbnail,
  180. 'categories': categories,
  181. 'formats': formats,
  182. 'subtitles': subtitles,
  183. }
  184. def _get_subtitles(self, video_id, subtitles_urls):
  185. subtitles = {}
  186. for lang, url in subtitles_urls.items():
  187. # For some weird reason, blip.tv serves a video instead of subtitles
  188. # when we request with a common UA
  189. req = compat_urllib_request.Request(url)
  190. req.add_header('User-Agent', 'youtube-dl')
  191. subtitles[lang] = [{
  192. # The extension is 'srt' but it's actually an 'ass' file
  193. 'ext': 'ass',
  194. 'data': self._download_webpage(req, None, note=False),
  195. }]
  196. return subtitles
  197. class BlipTVUserIE(InfoExtractor):
  198. _VALID_URL = r'(?:(?:https?://(?:\w+\.)?blip\.tv/)|bliptvuser:)(?!api\.swf)([^/]+)/*$'
  199. _PAGE_SIZE = 12
  200. IE_NAME = 'blip.tv:user'
  201. _TEST = {
  202. 'url': 'http://blip.tv/actone',
  203. 'info_dict': {
  204. 'id': 'actone',
  205. 'title': 'Act One: The Series',
  206. },
  207. 'playlist_count': 5,
  208. }
  209. def _real_extract(self, url):
  210. mobj = re.match(self._VALID_URL, url)
  211. username = mobj.group(1)
  212. page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
  213. page = self._download_webpage(url, username, 'Downloading user page')
  214. mobj = re.search(r'data-users-id="([^"]+)"', page)
  215. page_base = page_base % mobj.group(1)
  216. title = self._og_search_title(page)
  217. # Download video ids using BlipTV Ajax calls. Result size per
  218. # query is limited (currently to 12 videos) so we need to query
  219. # page by page until there are no video ids - it means we got
  220. # all of them.
  221. video_ids = []
  222. pagenum = 1
  223. while True:
  224. url = page_base + "&page=" + str(pagenum)
  225. page = self._download_webpage(
  226. url, username, 'Downloading video ids from page %d' % pagenum)
  227. # Extract video identifiers
  228. ids_in_page = []
  229. for mobj in re.finditer(r'href="/([^"]+)"', page):
  230. if mobj.group(1) not in ids_in_page:
  231. ids_in_page.append(unescapeHTML(mobj.group(1)))
  232. video_ids.extend(ids_in_page)
  233. # A little optimization - if current page is not
  234. # "full", ie. does not contain PAGE_SIZE video ids then
  235. # we can assume that this page is the last one - there
  236. # are no more ids on further pages - no need to query
  237. # again.
  238. if len(ids_in_page) < self._PAGE_SIZE:
  239. break
  240. pagenum += 1
  241. urls = ['http://blip.tv/%s' % video_id for video_id in video_ids]
  242. url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
  243. return self.playlist_result(
  244. url_entries, playlist_title=title, playlist_id=username)