bliptv.py 11 KB

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