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.

178 lines
6.5 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. from __future__ import unicode_literals
  2. import datetime
  3. import re
  4. from .common import InfoExtractor
  5. from .subtitles import SubtitlesInfoExtractor
  6. from ..utils import (
  7. compat_str,
  8. compat_urllib_request,
  9. unescapeHTML,
  10. )
  11. class BlipTVIE(SubtitlesInfoExtractor):
  12. """Information extractor for blip.tv"""
  13. _VALID_URL = r'https?://(?:\w+\.)?blip\.tv/((.+/)|(play/)|(api\.swf#))(?P<presumptive_id>.+)$'
  14. _TESTS = [{
  15. 'url': 'http://blip.tv/cbr/cbr-exclusive-gotham-city-imposters-bats-vs-jokerz-short-3-5796352',
  16. 'md5': 'c6934ad0b6acf2bd920720ec888eb812',
  17. 'info_dict': {
  18. 'id': '5779306',
  19. 'ext': 'mov',
  20. 'upload_date': '20111205',
  21. 'description': 'md5:9bc31f227219cde65e47eeec8d2dc596',
  22. 'uploader': 'Comic Book Resources - CBR TV',
  23. 'title': 'CBR EXCLUSIVE: "Gotham City Imposters" Bats VS Jokerz Short 3',
  24. }
  25. }, {
  26. # https://github.com/rg3/youtube-dl/pull/2274
  27. 'note': 'Video with subtitles',
  28. 'url': 'http://blip.tv/play/h6Uag5OEVgI.html',
  29. 'md5': '309f9d25b820b086ca163ffac8031806',
  30. 'info_dict': {
  31. 'id': '6586561',
  32. 'ext': 'mp4',
  33. 'uploader': 'Red vs. Blue',
  34. 'description': 'One-Zero-One',
  35. 'upload_date': '20130614',
  36. 'title': 'Red vs. Blue Season 11 Episode 1',
  37. }
  38. }]
  39. def _real_extract(self, url):
  40. mobj = re.match(self._VALID_URL, url)
  41. presumptive_id = mobj.group('presumptive_id')
  42. # See https://github.com/rg3/youtube-dl/issues/857
  43. embed_mobj = re.match(r'https?://(?:\w+\.)?blip\.tv/(?:play/|api\.swf#)([a-zA-Z0-9]+)', url)
  44. if embed_mobj:
  45. info_url = 'http://blip.tv/play/%s.x?p=1' % embed_mobj.group(1)
  46. info_page = self._download_webpage(info_url, embed_mobj.group(1))
  47. video_id = self._search_regex(
  48. r'data-episode-id="([0-9]+)', info_page, 'video_id')
  49. return self.url_result('http://blip.tv/a/a-' + video_id, 'BlipTV')
  50. cchar = '&' if '?' in url else '?'
  51. json_url = url + cchar + 'skin=json&version=2&no_wrap=1'
  52. request = compat_urllib_request.Request(json_url)
  53. request.add_header('User-Agent', 'iTunes/10.6.1')
  54. json_data = self._download_json(request, video_id=presumptive_id)
  55. if 'Post' in json_data:
  56. data = json_data['Post']
  57. else:
  58. data = json_data
  59. video_id = compat_str(data['item_id'])
  60. upload_date = datetime.datetime.strptime(data['datestamp'], '%m-%d-%y %H:%M%p').strftime('%Y%m%d')
  61. subtitles = {}
  62. formats = []
  63. if 'additionalMedia' in data:
  64. for f in data['additionalMedia']:
  65. if f.get('file_type_srt') == 1:
  66. LANGS = {
  67. 'english': 'en',
  68. }
  69. lang = f['role'].rpartition('-')[-1].strip().lower()
  70. langcode = LANGS.get(lang, lang)
  71. subtitles[langcode] = f['url']
  72. continue
  73. if not int(f['media_width']): # filter m3u8
  74. continue
  75. formats.append({
  76. 'url': f['url'],
  77. 'format_id': f['role'],
  78. 'width': int(f['media_width']),
  79. 'height': int(f['media_height']),
  80. })
  81. else:
  82. formats.append({
  83. 'url': data['media']['url'],
  84. 'width': int(data['media']['width']),
  85. 'height': int(data['media']['height']),
  86. })
  87. self._sort_formats(formats)
  88. # subtitles
  89. video_subtitles = self.extract_subtitles(video_id, subtitles)
  90. if self._downloader.params.get('listsubtitles', False):
  91. self._list_available_subtitles(video_id, subtitles)
  92. return
  93. return {
  94. 'id': video_id,
  95. 'uploader': data['display_name'],
  96. 'upload_date': upload_date,
  97. 'title': data['title'],
  98. 'thumbnail': data['thumbnailUrl'],
  99. 'description': data['description'],
  100. 'user_agent': 'iTunes/10.6.1',
  101. 'formats': formats,
  102. 'subtitles': video_subtitles,
  103. }
  104. def _download_subtitle_url(self, sub_lang, url):
  105. # For some weird reason, blip.tv serves a video instead of subtitles
  106. # when we request with a common UA
  107. req = compat_urllib_request.Request(url)
  108. req.add_header('Youtubedl-user-agent', 'youtube-dl')
  109. return self._download_webpage(req, None, note=False)
  110. class BlipTVUserIE(InfoExtractor):
  111. _VALID_URL = r'(?:(?:(?:https?://)?(?:\w+\.)?blip\.tv/)|bliptvuser:)([^/]+)/*$'
  112. _PAGE_SIZE = 12
  113. IE_NAME = 'blip.tv:user'
  114. def _real_extract(self, url):
  115. mobj = re.match(self._VALID_URL, url)
  116. username = mobj.group(1)
  117. page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
  118. page = self._download_webpage(url, username, 'Downloading user page')
  119. mobj = re.search(r'data-users-id="([^"]+)"', page)
  120. page_base = page_base % mobj.group(1)
  121. # Download video ids using BlipTV Ajax calls. Result size per
  122. # query is limited (currently to 12 videos) so we need to query
  123. # page by page until there are no video ids - it means we got
  124. # all of them.
  125. video_ids = []
  126. pagenum = 1
  127. while True:
  128. url = page_base + "&page=" + str(pagenum)
  129. page = self._download_webpage(
  130. url, username, 'Downloading video ids from page %d' % pagenum)
  131. # Extract video identifiers
  132. ids_in_page = []
  133. for mobj in re.finditer(r'href="/([^"]+)"', page):
  134. if mobj.group(1) not in ids_in_page:
  135. ids_in_page.append(unescapeHTML(mobj.group(1)))
  136. video_ids.extend(ids_in_page)
  137. # A little optimization - if current page is not
  138. # "full", ie. does not contain PAGE_SIZE video ids then
  139. # we can assume that this page is the last one - there
  140. # are no more ids on further pages - no need to query
  141. # again.
  142. if len(ids_in_page) < self._PAGE_SIZE:
  143. break
  144. pagenum += 1
  145. urls = ['http://blip.tv/%s' % video_id for video_id in video_ids]
  146. url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
  147. return [self.playlist_result(url_entries, playlist_title=username)]