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.

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