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.

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