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.

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