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.

228 lines
8.5 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+_]+)))'
  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 and
  68. # https://github.com/rg3/youtube-dl/issues/4197
  69. if lookup_id:
  70. info_page = self._download_webpage(
  71. 'http://blip.tv/play/%s.x?p=1' % lookup_id, lookup_id, 'Resolving lookup id')
  72. video_id = self._search_regex(r'config\.id\s*=\s*"([0-9]+)', info_page, 'video_id')
  73. else:
  74. video_id = mobj.group('id')
  75. rss = self._download_xml('http://blip.tv/rss/flash/%s' % video_id, video_id, 'Downloading video RSS')
  76. def blip(s):
  77. return '{http://blip.tv/dtd/blip/1.0}%s' % s
  78. def media(s):
  79. return '{http://search.yahoo.com/mrss/}%s' % s
  80. def itunes(s):
  81. return '{http://www.itunes.com/dtds/podcast-1.0.dtd}%s' % s
  82. item = rss.find('channel/item')
  83. video_id = item.find(blip('item_id')).text
  84. title = item.find('./title').text
  85. description = clean_html(compat_str(item.find(blip('puredescription')).text))
  86. timestamp = parse_iso8601(item.find(blip('datestamp')).text)
  87. uploader = item.find(blip('user')).text
  88. uploader_id = item.find(blip('userid')).text
  89. duration = int(item.find(blip('runtime')).text)
  90. media_thumbnail = item.find(media('thumbnail'))
  91. thumbnail = media_thumbnail.get('url') if media_thumbnail is not None else item.find(itunes('image')).text
  92. categories = [category.text for category in item.findall('category')]
  93. formats = []
  94. subtitles = {}
  95. media_group = item.find(media('group'))
  96. for media_content in media_group.findall(media('content')):
  97. url = media_content.get('url')
  98. role = media_content.get(blip('role'))
  99. msg = self._download_webpage(
  100. url + '?showplayer=20140425131715&referrer=http://blip.tv&mask=7&skin=flashvars&view=url',
  101. video_id, 'Resolving URL for %s' % role)
  102. real_url = compat_urlparse.parse_qs(msg)['message'][0]
  103. media_type = media_content.get('type')
  104. if media_type == 'text/srt' or url.endswith('.srt'):
  105. LANGS = {
  106. 'english': 'en',
  107. }
  108. lang = role.rpartition('-')[-1].strip().lower()
  109. langcode = LANGS.get(lang, lang)
  110. subtitles[langcode] = url
  111. elif media_type.startswith('video/'):
  112. formats.append({
  113. 'url': real_url,
  114. 'format_id': role,
  115. 'format_note': media_type,
  116. 'vcodec': media_content.get(blip('vcodec')),
  117. 'acodec': media_content.get(blip('acodec')),
  118. 'filesize': media_content.get('filesize'),
  119. 'width': int(media_content.get('width')),
  120. 'height': int(media_content.get('height')),
  121. })
  122. self._sort_formats(formats)
  123. # subtitles
  124. video_subtitles = self.extract_subtitles(video_id, subtitles)
  125. if self._downloader.params.get('listsubtitles', False):
  126. self._list_available_subtitles(video_id, subtitles)
  127. return
  128. return {
  129. 'id': video_id,
  130. 'title': title,
  131. 'description': description,
  132. 'timestamp': timestamp,
  133. 'uploader': uploader,
  134. 'uploader_id': uploader_id,
  135. 'duration': duration,
  136. 'thumbnail': thumbnail,
  137. 'categories': categories,
  138. 'formats': formats,
  139. 'subtitles': video_subtitles,
  140. }
  141. def _download_subtitle_url(self, sub_lang, url):
  142. # For some weird reason, blip.tv serves a video instead of subtitles
  143. # when we request with a common UA
  144. req = compat_urllib_request.Request(url)
  145. req.add_header('Youtubedl-user-agent', 'youtube-dl')
  146. return self._download_webpage(req, None, note=False)
  147. class BlipTVUserIE(InfoExtractor):
  148. _VALID_URL = r'(?:(?:https?://(?:\w+\.)?blip\.tv/)|bliptvuser:)(?!api\.swf)([^/]+)/*$'
  149. _PAGE_SIZE = 12
  150. IE_NAME = 'blip.tv:user'
  151. _TEST = {
  152. 'url': 'http://blip.tv/actone',
  153. 'info_dict': {
  154. 'id': 'actone',
  155. 'title': 'Act One: The Series',
  156. },
  157. 'playlist_count': 5,
  158. }
  159. def _real_extract(self, url):
  160. mobj = re.match(self._VALID_URL, url)
  161. username = mobj.group(1)
  162. page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
  163. page = self._download_webpage(url, username, 'Downloading user page')
  164. mobj = re.search(r'data-users-id="([^"]+)"', page)
  165. page_base = page_base % mobj.group(1)
  166. title = self._og_search_title(page)
  167. # Download video ids using BlipTV Ajax calls. Result size per
  168. # query is limited (currently to 12 videos) so we need to query
  169. # page by page until there are no video ids - it means we got
  170. # all of them.
  171. video_ids = []
  172. pagenum = 1
  173. while True:
  174. url = page_base + "&page=" + str(pagenum)
  175. page = self._download_webpage(
  176. url, username, 'Downloading video ids from page %d' % pagenum)
  177. # Extract video identifiers
  178. ids_in_page = []
  179. for mobj in re.finditer(r'href="/([^"]+)"', page):
  180. if mobj.group(1) not in ids_in_page:
  181. ids_in_page.append(unescapeHTML(mobj.group(1)))
  182. video_ids.extend(ids_in_page)
  183. # A little optimization - if current page is not
  184. # "full", ie. does not contain PAGE_SIZE video ids then
  185. # we can assume that this page is the last one - there
  186. # are no more ids on further pages - no need to query
  187. # again.
  188. if len(ids_in_page) < self._PAGE_SIZE:
  189. break
  190. pagenum += 1
  191. urls = ['http://blip.tv/%s' % video_id for video_id in video_ids]
  192. url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
  193. return self.playlist_result(
  194. url_entries, playlist_title=title, playlist_id=username)