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.

266 lines
10 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 ..compat import (
  6. compat_str,
  7. compat_urllib_request,
  8. compat_urlparse,
  9. )
  10. from ..utils import (
  11. clean_html,
  12. int_or_none,
  13. parse_iso8601,
  14. unescapeHTML,
  15. )
  16. class BlipTVIE(SubtitlesInfoExtractor):
  17. _VALID_URL = r'https?://(?:\w+\.)?blip\.tv/(?:(?:.+-|rss/flash/)(?P<id>\d+)|((?:play/|api\.swf#)(?P<lookup_id>[\da-zA-Z+_]+)))'
  18. _TESTS = [
  19. {
  20. 'url': 'http://blip.tv/cbr/cbr-exclusive-gotham-city-imposters-bats-vs-jokerz-short-3-5796352',
  21. 'md5': 'c6934ad0b6acf2bd920720ec888eb812',
  22. 'info_dict': {
  23. 'id': '5779306',
  24. 'ext': 'mov',
  25. 'title': 'CBR EXCLUSIVE: "Gotham City Imposters" Bats VS Jokerz Short 3',
  26. 'description': 'md5:9bc31f227219cde65e47eeec8d2dc596',
  27. 'timestamp': 1323138843,
  28. 'upload_date': '20111206',
  29. 'uploader': 'cbr',
  30. 'uploader_id': '679425',
  31. 'duration': 81,
  32. }
  33. },
  34. {
  35. # https://github.com/rg3/youtube-dl/pull/2274
  36. 'note': 'Video with subtitles',
  37. 'url': 'http://blip.tv/play/h6Uag5OEVgI.html',
  38. 'md5': '309f9d25b820b086ca163ffac8031806',
  39. 'info_dict': {
  40. 'id': '6586561',
  41. 'ext': 'mp4',
  42. 'title': 'Red vs. Blue Season 11 Episode 1',
  43. 'description': 'One-Zero-One',
  44. 'timestamp': 1371261608,
  45. 'upload_date': '20130615',
  46. 'uploader': 'redvsblue',
  47. 'uploader_id': '792887',
  48. 'duration': 279,
  49. }
  50. },
  51. {
  52. # https://bugzilla.redhat.com/show_bug.cgi?id=967465
  53. 'url': 'http://a.blip.tv/api.swf#h6Uag5KbVwI',
  54. 'md5': '314e87b1ebe7a48fcbfdd51b791ce5a6',
  55. 'info_dict': {
  56. 'id': '6573122',
  57. 'ext': 'mov',
  58. 'upload_date': '20130520',
  59. 'description': 'Two hapless space marines argue over what to do when they realize they have an astronomically huge problem on their hands.',
  60. 'title': 'Red vs. Blue Season 11 Trailer',
  61. 'timestamp': 1369029609,
  62. 'uploader': 'redvsblue',
  63. 'uploader_id': '792887',
  64. }
  65. },
  66. {
  67. 'url': 'http://blip.tv/play/gbk766dkj4Yn',
  68. 'md5': 'fe0a33f022d49399a241e84a8ea8b8e3',
  69. 'info_dict': {
  70. 'id': '1749452',
  71. 'ext': 'mp4',
  72. 'upload_date': '20090208',
  73. 'description': 'Witness the first appearance of the Nostalgia Critic character, as Doug reviews the movie Transformers.',
  74. 'title': 'Nostalgia Critic: Transformers',
  75. 'timestamp': 1234068723,
  76. 'uploader': 'NostalgiaCritic',
  77. 'uploader_id': '246467',
  78. }
  79. },
  80. {
  81. # https://github.com/rg3/youtube-dl/pull/4404
  82. 'note': 'Audio only',
  83. 'url': 'http://blip.tv/hilarios-productions/weekly-manga-recap-kingdom-7119982',
  84. 'md5': '76c0a56f24e769ceaab21fbb6416a351',
  85. 'info_dict': {
  86. 'id': '7103299',
  87. 'ext': 'flv',
  88. 'title': 'Weekly Manga Recap: Kingdom',
  89. 'description': 'And then Shin breaks the enemy line, and he&apos;s all like HWAH! And then he slices a guy and it&apos;s all like FWASHING! And... it&apos;s really hard to describe the best parts of this series without breaking down into sound effects, okay?',
  90. 'timestamp': 1417660321,
  91. 'upload_date': '20141204',
  92. 'uploader': 'The Rollo T',
  93. 'uploader_id': '407429',
  94. 'duration': 7251,
  95. 'vcodec': 'none',
  96. }
  97. },
  98. ]
  99. def _real_extract(self, url):
  100. mobj = re.match(self._VALID_URL, url)
  101. lookup_id = mobj.group('lookup_id')
  102. # See https://github.com/rg3/youtube-dl/issues/857 and
  103. # https://github.com/rg3/youtube-dl/issues/4197
  104. if lookup_id:
  105. urlh = self._request_webpage(
  106. 'http://blip.tv/play/%s' % lookup_id, lookup_id, 'Resolving lookup id')
  107. url = compat_urlparse.urlparse(urlh.geturl())
  108. qs = compat_urlparse.parse_qs(url.query)
  109. mobj = re.match(self._VALID_URL, qs['file'][0])
  110. video_id = mobj.group('id')
  111. rss = self._download_xml('http://blip.tv/rss/flash/%s' % video_id, video_id, 'Downloading video RSS')
  112. def blip(s):
  113. return '{http://blip.tv/dtd/blip/1.0}%s' % s
  114. def media(s):
  115. return '{http://search.yahoo.com/mrss/}%s' % s
  116. def itunes(s):
  117. return '{http://www.itunes.com/dtds/podcast-1.0.dtd}%s' % s
  118. item = rss.find('channel/item')
  119. video_id = item.find(blip('item_id')).text
  120. title = item.find('./title').text
  121. description = clean_html(compat_str(item.find(blip('puredescription')).text))
  122. timestamp = parse_iso8601(item.find(blip('datestamp')).text)
  123. uploader = item.find(blip('user')).text
  124. uploader_id = item.find(blip('userid')).text
  125. duration = int(item.find(blip('runtime')).text)
  126. media_thumbnail = item.find(media('thumbnail'))
  127. thumbnail = media_thumbnail.get('url') if media_thumbnail is not None else item.find(itunes('image')).text
  128. categories = [category.text for category in item.findall('category')]
  129. formats = []
  130. subtitles = {}
  131. media_group = item.find(media('group'))
  132. for media_content in media_group.findall(media('content')):
  133. url = media_content.get('url')
  134. role = media_content.get(blip('role'))
  135. msg = self._download_webpage(
  136. url + '?showplayer=20140425131715&referrer=http://blip.tv&mask=7&skin=flashvars&view=url',
  137. video_id, 'Resolving URL for %s' % role)
  138. real_url = compat_urlparse.parse_qs(msg.strip())['message'][0]
  139. media_type = media_content.get('type')
  140. if media_type == 'text/srt' or url.endswith('.srt'):
  141. LANGS = {
  142. 'english': 'en',
  143. }
  144. lang = role.rpartition('-')[-1].strip().lower()
  145. langcode = LANGS.get(lang, lang)
  146. subtitles[langcode] = url
  147. elif media_type.startswith('video/'):
  148. formats.append({
  149. 'url': real_url,
  150. 'format_id': role,
  151. 'format_note': media_type,
  152. 'vcodec': media_content.get(blip('vcodec')) or 'none',
  153. 'acodec': media_content.get(blip('acodec')),
  154. 'filesize': media_content.get('filesize'),
  155. 'width': int_or_none(media_content.get('width')),
  156. 'height': int_or_none(media_content.get('height')),
  157. })
  158. self._sort_formats(formats)
  159. # subtitles
  160. video_subtitles = self.extract_subtitles(video_id, subtitles)
  161. if self._downloader.params.get('listsubtitles', False):
  162. self._list_available_subtitles(video_id, subtitles)
  163. return
  164. return {
  165. 'id': video_id,
  166. 'title': title,
  167. 'description': description,
  168. 'timestamp': timestamp,
  169. 'uploader': uploader,
  170. 'uploader_id': uploader_id,
  171. 'duration': duration,
  172. 'thumbnail': thumbnail,
  173. 'categories': categories,
  174. 'formats': formats,
  175. 'subtitles': video_subtitles,
  176. }
  177. def _download_subtitle_url(self, sub_lang, url):
  178. # For some weird reason, blip.tv serves a video instead of subtitles
  179. # when we request with a common UA
  180. req = compat_urllib_request.Request(url)
  181. req.add_header('Youtubedl-user-agent', 'youtube-dl')
  182. return self._download_webpage(req, None, note=False)
  183. class BlipTVUserIE(InfoExtractor):
  184. _VALID_URL = r'(?:(?:https?://(?:\w+\.)?blip\.tv/)|bliptvuser:)(?!api\.swf)([^/]+)/*$'
  185. _PAGE_SIZE = 12
  186. IE_NAME = 'blip.tv:user'
  187. _TEST = {
  188. 'url': 'http://blip.tv/actone',
  189. 'info_dict': {
  190. 'id': 'actone',
  191. 'title': 'Act One: The Series',
  192. },
  193. 'playlist_count': 5,
  194. }
  195. def _real_extract(self, url):
  196. mobj = re.match(self._VALID_URL, url)
  197. username = mobj.group(1)
  198. page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
  199. page = self._download_webpage(url, username, 'Downloading user page')
  200. mobj = re.search(r'data-users-id="([^"]+)"', page)
  201. page_base = page_base % mobj.group(1)
  202. title = self._og_search_title(page)
  203. # Download video ids using BlipTV Ajax calls. Result size per
  204. # query is limited (currently to 12 videos) so we need to query
  205. # page by page until there are no video ids - it means we got
  206. # all of them.
  207. video_ids = []
  208. pagenum = 1
  209. while True:
  210. url = page_base + "&page=" + str(pagenum)
  211. page = self._download_webpage(
  212. url, username, 'Downloading video ids from page %d' % pagenum)
  213. # Extract video identifiers
  214. ids_in_page = []
  215. for mobj in re.finditer(r'href="/([^"]+)"', page):
  216. if mobj.group(1) not in ids_in_page:
  217. ids_in_page.append(unescapeHTML(mobj.group(1)))
  218. video_ids.extend(ids_in_page)
  219. # A little optimization - if current page is not
  220. # "full", ie. does not contain PAGE_SIZE video ids then
  221. # we can assume that this page is the last one - there
  222. # are no more ids on further pages - no need to query
  223. # again.
  224. if len(ids_in_page) < self._PAGE_SIZE:
  225. break
  226. pagenum += 1
  227. urls = ['http://blip.tv/%s' % video_id for video_id in video_ids]
  228. url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
  229. return self.playlist_result(
  230. url_entries, playlist_title=title, playlist_id=username)