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.

292 lines
11 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 ..compat import (
  5. compat_urllib_request,
  6. compat_urlparse,
  7. )
  8. from ..utils import (
  9. clean_html,
  10. int_or_none,
  11. parse_iso8601,
  12. unescapeHTML,
  13. xpath_text,
  14. xpath_with_ns,
  15. )
  16. class BlipTVIE(InfoExtractor):
  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': '80baf1ec5c3d2019037c1c707d676b9f',
  22. 'info_dict': {
  23. 'id': '5779306',
  24. 'ext': 'm4v',
  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. # missing duration
  100. 'url': 'http://blip.tv/rss/flash/6700880',
  101. 'info_dict': {
  102. 'id': '6684191',
  103. 'ext': 'm4v',
  104. 'title': 'Cowboy Bebop: Gateway Shuffle Review',
  105. 'description': 'md5:3acc480c0f9ae157f5fe88547ecaf3f8',
  106. 'timestamp': 1386639757,
  107. 'upload_date': '20131210',
  108. 'uploader': 'sfdebris',
  109. 'uploader_id': '706520',
  110. }
  111. }
  112. ]
  113. @staticmethod
  114. def _extract_url(webpage):
  115. mobj = re.search(r'<meta\s[^>]*https?://api\.blip\.tv/\w+/redirect/\w+/(\d+)', webpage)
  116. if mobj:
  117. return 'http://blip.tv/a/a-' + mobj.group(1)
  118. mobj = re.search(r'<(?:iframe|embed|object)\s[^>]*(https?://(?:\w+\.)?blip\.tv/(?:play/|api\.swf#)[a-zA-Z0-9_]+)', webpage)
  119. if mobj:
  120. return mobj.group(1)
  121. def _real_extract(self, url):
  122. mobj = re.match(self._VALID_URL, url)
  123. lookup_id = mobj.group('lookup_id')
  124. # See https://github.com/rg3/youtube-dl/issues/857 and
  125. # https://github.com/rg3/youtube-dl/issues/4197
  126. if lookup_id:
  127. urlh = self._request_webpage(
  128. 'http://blip.tv/play/%s' % lookup_id, lookup_id, 'Resolving lookup id')
  129. url = compat_urlparse.urlparse(urlh.geturl())
  130. qs = compat_urlparse.parse_qs(url.query)
  131. mobj = re.match(self._VALID_URL, qs['file'][0])
  132. video_id = mobj.group('id')
  133. rss = self._download_xml('http://blip.tv/rss/flash/%s' % video_id, video_id, 'Downloading video RSS')
  134. def _x(p):
  135. return xpath_with_ns(p, {
  136. 'blip': 'http://blip.tv/dtd/blip/1.0',
  137. 'media': 'http://search.yahoo.com/mrss/',
  138. 'itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd',
  139. })
  140. item = rss.find('channel/item')
  141. video_id = xpath_text(item, _x('blip:item_id'), 'video id') or lookup_id
  142. title = xpath_text(item, 'title', 'title', fatal=True)
  143. description = clean_html(xpath_text(item, _x('blip:puredescription'), 'description'))
  144. timestamp = parse_iso8601(xpath_text(item, _x('blip:datestamp'), 'timestamp'))
  145. uploader = xpath_text(item, _x('blip:user'), 'uploader')
  146. uploader_id = xpath_text(item, _x('blip:userid'), 'uploader id')
  147. duration = int_or_none(xpath_text(item, _x('blip:runtime'), 'duration'))
  148. media_thumbnail = item.find(_x('media:thumbnail'))
  149. thumbnail = (media_thumbnail.get('url') if media_thumbnail is not None
  150. else xpath_text(item, 'image', 'thumbnail'))
  151. categories = [category.text for category in item.findall('category') if category is not None]
  152. formats = []
  153. subtitles_urls = {}
  154. media_group = item.find(_x('media:group'))
  155. for media_content in media_group.findall(_x('media:content')):
  156. url = media_content.get('url')
  157. role = media_content.get(_x('blip:role'))
  158. msg = self._download_webpage(
  159. url + '?showplayer=20140425131715&referrer=http://blip.tv&mask=7&skin=flashvars&view=url',
  160. video_id, 'Resolving URL for %s' % role)
  161. real_url = compat_urlparse.parse_qs(msg.strip())['message'][0]
  162. media_type = media_content.get('type')
  163. if media_type == 'text/srt' or url.endswith('.srt'):
  164. LANGS = {
  165. 'english': 'en',
  166. }
  167. lang = role.rpartition('-')[-1].strip().lower()
  168. langcode = LANGS.get(lang, lang)
  169. subtitles_urls[langcode] = url
  170. elif media_type.startswith('video/'):
  171. formats.append({
  172. 'url': real_url,
  173. 'format_id': role,
  174. 'format_note': media_type,
  175. 'vcodec': media_content.get(_x('blip:vcodec')) or 'none',
  176. 'acodec': media_content.get(_x('blip:acodec')),
  177. 'filesize': media_content.get('filesize'),
  178. 'width': int_or_none(media_content.get('width')),
  179. 'height': int_or_none(media_content.get('height')),
  180. })
  181. self._check_formats(formats, video_id)
  182. self._sort_formats(formats)
  183. subtitles = self.extract_subtitles(video_id, subtitles_urls)
  184. return {
  185. 'id': video_id,
  186. 'title': title,
  187. 'description': description,
  188. 'timestamp': timestamp,
  189. 'uploader': uploader,
  190. 'uploader_id': uploader_id,
  191. 'duration': duration,
  192. 'thumbnail': thumbnail,
  193. 'categories': categories,
  194. 'formats': formats,
  195. 'subtitles': subtitles,
  196. }
  197. def _get_subtitles(self, video_id, subtitles_urls):
  198. subtitles = {}
  199. for lang, url in subtitles_urls.items():
  200. # For some weird reason, blip.tv serves a video instead of subtitles
  201. # when we request with a common UA
  202. req = compat_urllib_request.Request(url)
  203. req.add_header('User-Agent', 'youtube-dl')
  204. subtitles[lang] = [{
  205. # The extension is 'srt' but it's actually an 'ass' file
  206. 'ext': 'ass',
  207. 'data': self._download_webpage(req, None, note=False),
  208. }]
  209. return subtitles
  210. class BlipTVUserIE(InfoExtractor):
  211. _VALID_URL = r'(?:(?:https?://(?:\w+\.)?blip\.tv/)|bliptvuser:)(?!api\.swf)([^/]+)/*$'
  212. _PAGE_SIZE = 12
  213. IE_NAME = 'blip.tv:user'
  214. _TEST = {
  215. 'url': 'http://blip.tv/actone',
  216. 'info_dict': {
  217. 'id': 'actone',
  218. 'title': 'Act One: The Series',
  219. },
  220. 'playlist_count': 5,
  221. }
  222. def _real_extract(self, url):
  223. mobj = re.match(self._VALID_URL, url)
  224. username = mobj.group(1)
  225. page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
  226. page = self._download_webpage(url, username, 'Downloading user page')
  227. mobj = re.search(r'data-users-id="([^"]+)"', page)
  228. page_base = page_base % mobj.group(1)
  229. title = self._og_search_title(page)
  230. # Download video ids using BlipTV Ajax calls. Result size per
  231. # query is limited (currently to 12 videos) so we need to query
  232. # page by page until there are no video ids - it means we got
  233. # all of them.
  234. video_ids = []
  235. pagenum = 1
  236. while True:
  237. url = page_base + "&page=" + str(pagenum)
  238. page = self._download_webpage(
  239. url, username, 'Downloading video ids from page %d' % pagenum)
  240. # Extract video identifiers
  241. ids_in_page = []
  242. for mobj in re.finditer(r'href="/([^"]+)"', page):
  243. if mobj.group(1) not in ids_in_page:
  244. ids_in_page.append(unescapeHTML(mobj.group(1)))
  245. video_ids.extend(ids_in_page)
  246. # A little optimization - if current page is not
  247. # "full", ie. does not contain PAGE_SIZE video ids then
  248. # we can assume that this page is the last one - there
  249. # are no more ids on further pages - no need to query
  250. # again.
  251. if len(ids_in_page) < self._PAGE_SIZE:
  252. break
  253. pagenum += 1
  254. urls = ['http://blip.tv/%s' % video_id for video_id in video_ids]
  255. url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
  256. return self.playlist_result(
  257. url_entries, playlist_title=title, playlist_id=username)