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.

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