You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
8.2 KiB

11 years ago
11 years ago
11 years ago
11 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from .subtitles import SubtitlesInfoExtractor
  5. from ..utils import (
  6. compat_urllib_request,
  7. unescapeHTML,
  8. parse_iso8601,
  9. compat_urlparse,
  10. clean_html,
  11. compat_str,
  12. )
  13. class BlipTVIE(SubtitlesInfoExtractor):
  14. _VALID_URL = r'https?://(?:\w+\.)?blip\.tv/(?:(?:.+-|rss/flash/)(?P<id>\d+)|((?:play/|api\.swf#)(?P<lookup_id>[\da-zA-Z+_TESTS]+)))'
  15. _TESTS = [
  16. {
  17. 'url': 'http://blip.tv/cbr/cbr-exclusive-gotham-city-imposters-bats-vs-jokerz-short-3-5796352',
  18. 'md5': 'c6934ad0b6acf2bd920720ec888eb812',
  19. 'info_dict': {
  20. 'id': '5779306',
  21. 'ext': 'mov',
  22. 'title': 'CBR EXCLUSIVE: "Gotham City Imposters" Bats VS Jokerz Short 3',
  23. 'description': 'md5:9bc31f227219cde65e47eeec8d2dc596',
  24. 'timestamp': 1323138843,
  25. 'upload_date': '20111206',
  26. 'uploader': 'cbr',
  27. 'uploader_id': '679425',
  28. 'duration': 81,
  29. }
  30. },
  31. {
  32. # https://github.com/rg3/youtube-dl/pull/2274
  33. 'note': 'Video with subtitles',
  34. 'url': 'http://blip.tv/play/h6Uag5OEVgI.html',
  35. 'md5': '309f9d25b820b086ca163ffac8031806',
  36. 'info_dict': {
  37. 'id': '6586561',
  38. 'ext': 'mp4',
  39. 'title': 'Red vs. Blue Season 11 Episode 1',
  40. 'description': 'One-Zero-One',
  41. 'timestamp': 1371261608,
  42. 'upload_date': '20130615',
  43. 'uploader': 'redvsblue',
  44. 'uploader_id': '792887',
  45. 'duration': 279,
  46. }
  47. },
  48. {
  49. # https://bugzilla.redhat.com/show_bug.cgi?id=967465
  50. 'url': 'http://a.blip.tv/api.swf#h6Uag5KbVwI',
  51. 'md5': '314e87b1ebe7a48fcbfdd51b791ce5a6',
  52. 'info_dict': {
  53. 'id': '6573122',
  54. 'ext': 'mov',
  55. 'upload_date': '20130520',
  56. 'description': 'Two hapless space marines argue over what to do when they realize they have an astronomically huge problem on their hands.',
  57. 'title': 'Red vs. Blue Season 11 Trailer',
  58. 'timestamp': 1369029609,
  59. 'uploader': 'redvsblue',
  60. 'uploader_id': '792887',
  61. }
  62. }
  63. ]
  64. def _real_extract(self, url):
  65. mobj = re.match(self._VALID_URL, url)
  66. lookup_id = mobj.group('lookup_id')
  67. # See https://github.com/rg3/youtube-dl/issues/857
  68. if lookup_id:
  69. info_page = self._download_webpage(
  70. 'http://blip.tv/play/%s.x?p=1' % lookup_id, lookup_id, 'Resolving lookup id')
  71. video_id = self._search_regex(r'data-episode-id="([0-9]+)', info_page, 'video_id')
  72. else:
  73. video_id = mobj.group('id')
  74. rss = self._download_xml('http://blip.tv/rss/flash/%s' % video_id, video_id, 'Downloading video RSS')
  75. def blip(s):
  76. return '{http://blip.tv/dtd/blip/1.0}%s' % s
  77. def media(s):
  78. return '{http://search.yahoo.com/mrss/}%s' % s
  79. def itunes(s):
  80. return '{http://www.itunes.com/dtds/podcast-1.0.dtd}%s' % s
  81. item = rss.find('channel/item')
  82. video_id = item.find(blip('item_id')).text
  83. title = item.find('./title').text
  84. description = clean_html(compat_str(item.find(blip('puredescription')).text))
  85. timestamp = parse_iso8601(item.find(blip('datestamp')).text)
  86. uploader = item.find(blip('user')).text
  87. uploader_id = item.find(blip('userid')).text
  88. duration = int(item.find(blip('runtime')).text)
  89. media_thumbnail = item.find(media('thumbnail'))
  90. thumbnail = media_thumbnail.get('url') if media_thumbnail is not None else item.find(itunes('image')).text
  91. categories = [category.text for category in item.findall('category')]
  92. formats = []
  93. subtitles = {}
  94. media_group = item.find(media('group'))
  95. for media_content in media_group.findall(media('content')):
  96. url = media_content.get('url')
  97. role = media_content.get(blip('role'))
  98. msg = self._download_webpage(
  99. url + '?showplayer=20140425131715&referrer=http://blip.tv&mask=7&skin=flashvars&view=url',
  100. video_id, 'Resolving URL for %s' % role)
  101. real_url = compat_urlparse.parse_qs(msg)['message'][0]
  102. media_type = media_content.get('type')
  103. if media_type == 'text/srt' or url.endswith('.srt'):
  104. LANGS = {
  105. 'english': 'en',
  106. }
  107. lang = role.rpartition('-')[-1].strip().lower()
  108. langcode = LANGS.get(lang, lang)
  109. subtitles[langcode] = url
  110. elif media_type.startswith('video/'):
  111. formats.append({
  112. 'url': real_url,
  113. 'format_id': role,
  114. 'format_note': media_type,
  115. 'vcodec': media_content.get(blip('vcodec')),
  116. 'acodec': media_content.get(blip('acodec')),
  117. 'filesize': media_content.get('filesize'),
  118. 'width': int(media_content.get('width')),
  119. 'height': int(media_content.get('height')),
  120. })
  121. self._sort_formats(formats)
  122. # subtitles
  123. video_subtitles = self.extract_subtitles(video_id, subtitles)
  124. if self._downloader.params.get('listsubtitles', False):
  125. self._list_available_subtitles(video_id, subtitles)
  126. return
  127. return {
  128. 'id': video_id,
  129. 'title': title,
  130. 'description': description,
  131. 'timestamp': timestamp,
  132. 'uploader': uploader,
  133. 'uploader_id': uploader_id,
  134. 'duration': duration,
  135. 'thumbnail': thumbnail,
  136. 'categories': categories,
  137. 'formats': formats,
  138. 'subtitles': video_subtitles,
  139. }
  140. def _download_subtitle_url(self, sub_lang, url):
  141. # For some weird reason, blip.tv serves a video instead of subtitles
  142. # when we request with a common UA
  143. req = compat_urllib_request.Request(url)
  144. req.add_header('Youtubedl-user-agent', 'youtube-dl')
  145. return self._download_webpage(req, None, note=False)
  146. class BlipTVUserIE(InfoExtractor):
  147. _VALID_URL = r'(?:(?:(?:https?://)?(?:\w+\.)?blip\.tv/)|bliptvuser:)(?!api\.swf)([^/]+)/*$'
  148. _PAGE_SIZE = 12
  149. IE_NAME = 'blip.tv:user'
  150. def _real_extract(self, url):
  151. mobj = re.match(self._VALID_URL, url)
  152. username = mobj.group(1)
  153. page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
  154. page = self._download_webpage(url, username, 'Downloading user page')
  155. mobj = re.search(r'data-users-id="([^"]+)"', page)
  156. page_base = page_base % mobj.group(1)
  157. # Download video ids using BlipTV Ajax calls. Result size per
  158. # query is limited (currently to 12 videos) so we need to query
  159. # page by page until there are no video ids - it means we got
  160. # all of them.
  161. video_ids = []
  162. pagenum = 1
  163. while True:
  164. url = page_base + "&page=" + str(pagenum)
  165. page = self._download_webpage(
  166. url, username, 'Downloading video ids from page %d' % pagenum)
  167. # Extract video identifiers
  168. ids_in_page = []
  169. for mobj in re.finditer(r'href="/([^"]+)"', page):
  170. if mobj.group(1) not in ids_in_page:
  171. ids_in_page.append(unescapeHTML(mobj.group(1)))
  172. video_ids.extend(ids_in_page)
  173. # A little optimization - if current page is not
  174. # "full", ie. does not contain PAGE_SIZE video ids then
  175. # we can assume that this page is the last one - there
  176. # are no more ids on further pages - no need to query
  177. # again.
  178. if len(ids_in_page) < self._PAGE_SIZE:
  179. break
  180. pagenum += 1
  181. urls = ['http://blip.tv/%s' % video_id for video_id in video_ids]
  182. url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
  183. return [self.playlist_result(url_entries, playlist_title=username)]