bliptv.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import datetime
  2. import json
  3. import os
  4. import re
  5. import socket
  6. from .common import InfoExtractor
  7. from ..utils import (
  8. compat_http_client,
  9. compat_parse_qs,
  10. compat_str,
  11. compat_urllib_error,
  12. compat_urllib_parse_urlparse,
  13. compat_urllib_request,
  14. ExtractorError,
  15. unescapeHTML,
  16. )
  17. class BlipTVIE(InfoExtractor):
  18. """Information extractor for blip.tv"""
  19. _VALID_URL = r'^(?:https?://)?(?:\w+\.)?blip\.tv/((.+/)|(play/)|(api\.swf#))(.+)$'
  20. _URL_EXT = r'^.*\.([a-z0-9]+)$'
  21. IE_NAME = u'blip.tv'
  22. _TEST = {
  23. u'url': u'http://blip.tv/cbr/cbr-exclusive-gotham-city-imposters-bats-vs-jokerz-short-3-5796352',
  24. u'file': u'5779306.m4v',
  25. u'md5': u'80baf1ec5c3d2019037c1c707d676b9f',
  26. u'info_dict': {
  27. u"upload_date": u"20111205",
  28. u"description": u"md5:9bc31f227219cde65e47eeec8d2dc596",
  29. u"uploader": u"Comic Book Resources - CBR TV",
  30. u"title": u"CBR EXCLUSIVE: \"Gotham City Imposters\" Bats VS Jokerz Short 3"
  31. }
  32. }
  33. def report_direct_download(self, title):
  34. """Report information extraction."""
  35. self.to_screen(u'%s: Direct download detected' % title)
  36. def _real_extract(self, url):
  37. mobj = re.match(self._VALID_URL, url)
  38. if mobj is None:
  39. raise ExtractorError(u'Invalid URL: %s' % url)
  40. # See https://github.com/rg3/youtube-dl/issues/857
  41. api_mobj = re.match(r'http://a\.blip\.tv/api\.swf#(?P<video_id>[\d\w]+)', url)
  42. if api_mobj is not None:
  43. url = 'http://blip.tv/play/g_%s' % api_mobj.group('video_id')
  44. urlp = compat_urllib_parse_urlparse(url)
  45. if urlp.path.startswith('/play/'):
  46. response = self._request_webpage(url, None, False)
  47. redirecturl = response.geturl()
  48. rurlp = compat_urllib_parse_urlparse(redirecturl)
  49. file_id = compat_parse_qs(rurlp.fragment)['file'][0].rpartition('/')[2]
  50. url = 'http://blip.tv/a/a-' + file_id
  51. return self._real_extract(url)
  52. if '?' in url:
  53. cchar = '&'
  54. else:
  55. cchar = '?'
  56. json_url = url + cchar + 'skin=json&version=2&no_wrap=1'
  57. request = compat_urllib_request.Request(json_url)
  58. request.add_header('User-Agent', 'iTunes/10.6.1')
  59. self.report_extraction(mobj.group(1))
  60. info = None
  61. urlh = self._request_webpage(request, None, False,
  62. u'unable to download video info webpage')
  63. if urlh.headers.get('Content-Type', '').startswith('video/'): # Direct download
  64. basename = url.split('/')[-1]
  65. title,ext = os.path.splitext(basename)
  66. title = title.decode('UTF-8')
  67. ext = ext.replace('.', '')
  68. self.report_direct_download(title)
  69. return {
  70. 'id': title,
  71. 'url': url,
  72. 'uploader': None,
  73. 'upload_date': None,
  74. 'title': title,
  75. 'ext': ext,
  76. 'urlhandle': urlh
  77. }
  78. try:
  79. json_code_bytes = urlh.read()
  80. json_code = json_code_bytes.decode('utf-8')
  81. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  82. raise ExtractorError(u'Unable to read video info webpage: %s' % compat_str(err))
  83. try:
  84. json_data = json.loads(json_code)
  85. if 'Post' in json_data:
  86. data = json_data['Post']
  87. else:
  88. data = json_data
  89. upload_date = datetime.datetime.strptime(data['datestamp'], '%m-%d-%y %H:%M%p').strftime('%Y%m%d')
  90. if 'additionalMedia' in data:
  91. formats = sorted(data['additionalMedia'], key=lambda f: int(f['media_height']))
  92. best_format = formats[-1]
  93. video_url = best_format['url']
  94. else:
  95. video_url = data['media']['url']
  96. umobj = re.match(self._URL_EXT, video_url)
  97. if umobj is None:
  98. raise ValueError('Can not determine filename extension')
  99. ext = umobj.group(1)
  100. return {
  101. 'id': compat_str(data['item_id']),
  102. 'url': video_url,
  103. 'uploader': data['display_name'],
  104. 'upload_date': upload_date,
  105. 'title': data['title'],
  106. 'ext': ext,
  107. 'format': data['media']['mimeType'],
  108. 'thumbnail': data['thumbnailUrl'],
  109. 'description': data['description'],
  110. 'player_url': data['embedUrl'],
  111. 'user_agent': 'iTunes/10.6.1',
  112. }
  113. except (ValueError, KeyError) as err:
  114. raise ExtractorError(u'Unable to parse video information: %s' % repr(err))
  115. class BlipTVUserIE(InfoExtractor):
  116. """Information Extractor for blip.tv users."""
  117. _VALID_URL = r'(?:(?:(?:https?://)?(?:\w+\.)?blip\.tv/)|bliptvuser:)([^/]+)/*$'
  118. _PAGE_SIZE = 12
  119. IE_NAME = u'blip.tv:user'
  120. def _real_extract(self, url):
  121. # Extract username
  122. mobj = re.match(self._VALID_URL, url)
  123. if mobj is None:
  124. raise ExtractorError(u'Invalid URL: %s' % url)
  125. username = mobj.group(1)
  126. page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
  127. page = self._download_webpage(url, username, u'Downloading user page')
  128. mobj = re.search(r'data-users-id="([^"]+)"', page)
  129. page_base = page_base % mobj.group(1)
  130. # Download video ids using BlipTV Ajax calls. Result size per
  131. # query is limited (currently to 12 videos) so we need to query
  132. # page by page until there are no video ids - it means we got
  133. # all of them.
  134. video_ids = []
  135. pagenum = 1
  136. while True:
  137. url = page_base + "&page=" + str(pagenum)
  138. page = self._download_webpage(url, username,
  139. u'Downloading video ids from page %d' % pagenum)
  140. # Extract video identifiers
  141. ids_in_page = []
  142. for mobj in re.finditer(r'href="/([^"]+)"', page):
  143. if mobj.group(1) not in ids_in_page:
  144. ids_in_page.append(unescapeHTML(mobj.group(1)))
  145. video_ids.extend(ids_in_page)
  146. # A little optimization - if current page is not
  147. # "full", ie. does not contain PAGE_SIZE video ids then
  148. # we can assume that this page is the last one - there
  149. # are no more ids on further pages - no need to query
  150. # again.
  151. if len(ids_in_page) < self._PAGE_SIZE:
  152. break
  153. pagenum += 1
  154. urls = [u'http://blip.tv/%s' % video_id for video_id in video_ids]
  155. url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
  156. return [self.playlist_result(url_entries, playlist_title = username)]