viki.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from __future__ import unicode_literals
  2. import re
  3. from ..compat import (
  4. compat_urlparse,
  5. compat_urllib_request,
  6. )
  7. from ..utils import (
  8. ExtractorError,
  9. unescapeHTML,
  10. unified_strdate,
  11. US_RATINGS,
  12. clean_html,
  13. determine_ext,
  14. mimetype2ext,
  15. )
  16. from .common import InfoExtractor
  17. class VikiIE(InfoExtractor):
  18. IE_NAME = 'viki'
  19. # iPad2
  20. _USER_AGENT = 'Mozilla/5.0(iPad; U; CPU OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F191 Safari/6533.18.5'
  21. _VALID_URL = r'^https?://(?:www\.)?viki\.com/videos/(?P<id>[0-9]+v)'
  22. _TESTS = [{
  23. 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
  24. 'info_dict': {
  25. 'id': '1023585v',
  26. 'ext': 'mp4',
  27. 'title': 'Heirs Episode 14',
  28. 'uploader': 'SBS',
  29. 'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e',
  30. 'upload_date': '20131121',
  31. 'age_limit': 13,
  32. },
  33. 'skip': 'Blocked in the US',
  34. }, {
  35. 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
  36. 'md5': 'ca6493e6f0a6ec07da9aa8d6304b4b2c',
  37. 'info_dict': {
  38. 'id': '1067139v',
  39. 'ext': 'mp4',
  40. 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
  41. 'upload_date': '20150430',
  42. 'title': '\'The Avengers: Age of Ultron\' Press Conference',
  43. }
  44. }, {
  45. 'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi',
  46. 'info_dict': {
  47. 'id': '1048879v',
  48. 'ext': 'mp4',
  49. 'upload_date': '20140820',
  50. 'description': 'md5:54ff56d51bdfc7a30441ec967394e91c',
  51. 'title': 'Ankhon Dekhi',
  52. },
  53. 'params': {
  54. # requires ffmpeg
  55. 'skip_download': True,
  56. }
  57. }]
  58. def _real_extract(self, url):
  59. video_id = self._match_id(url)
  60. webpage = self._download_webpage(url, video_id)
  61. title = self._og_search_title(webpage)
  62. description = self._og_search_description(webpage)
  63. thumbnail = self._og_search_thumbnail(webpage)
  64. uploader_m = re.search(
  65. r'<strong>Broadcast Network: </strong>\s*([^<]*)<', webpage)
  66. if uploader_m is None:
  67. uploader = None
  68. else:
  69. uploader = uploader_m.group(1).strip()
  70. rating_str = self._html_search_regex(
  71. r'<strong>Rating: </strong>\s*([^<]*)<', webpage,
  72. 'rating information', default='').strip()
  73. age_limit = US_RATINGS.get(rating_str)
  74. req = compat_urllib_request.Request(
  75. 'http://www.viki.com/player5_fragment/%s?action=show&controller=videos' % video_id)
  76. req.add_header('User-Agent', self._USER_AGENT)
  77. info_webpage = self._download_webpage(
  78. req, video_id, note='Downloading info page')
  79. err_msg = self._html_search_regex(r'<div[^>]+class="video-error[^>]+>(.+)</div>', info_webpage, 'error message', default=None)
  80. if err_msg:
  81. err_msg = clean_html(err_msg)
  82. if 'not available in your region' in err_msg:
  83. raise ExtractorError(
  84. 'Video %s is blocked from your location.' % video_id,
  85. expected=True)
  86. else:
  87. raise ExtractorError('Viki said: ' + err_msg)
  88. mobj = re.search(
  89. r'<source[^>]+type="(?P<mime_type>[^"]+)"[^>]+src="(?P<url>[^"]+)"', info_webpage)
  90. if not mobj:
  91. raise ExtractorError('Unable to find video URL')
  92. video_url = unescapeHTML(mobj.group('url'))
  93. video_ext = mimetype2ext(mobj.group('mime_type'))
  94. if determine_ext(video_url) == 'm3u8':
  95. formats = self._extract_m3u8_formats(
  96. video_url, video_id, ext=video_ext)
  97. else:
  98. formats = [{
  99. 'url': video_url,
  100. 'ext': video_ext,
  101. }]
  102. upload_date_str = self._html_search_regex(
  103. r'"created_at":"([^"]+)"', info_webpage, 'upload date')
  104. upload_date = (
  105. unified_strdate(upload_date_str)
  106. if upload_date_str is not None
  107. else None
  108. )
  109. # subtitles
  110. video_subtitles = self.extract_subtitles(video_id, info_webpage)
  111. return {
  112. 'id': video_id,
  113. 'title': title,
  114. 'formats': formats,
  115. 'description': description,
  116. 'thumbnail': thumbnail,
  117. 'age_limit': age_limit,
  118. 'uploader': uploader,
  119. 'subtitles': video_subtitles,
  120. 'upload_date': upload_date,
  121. }
  122. def _get_subtitles(self, video_id, info_webpage):
  123. res = {}
  124. for sturl_html in re.findall(r'<track src="([^"]+)"', info_webpage):
  125. sturl = unescapeHTML(sturl_html)
  126. m = re.search(r'/(?P<lang>[a-z]+)\.vtt', sturl)
  127. if not m:
  128. continue
  129. res[m.group('lang')] = [{
  130. 'url': compat_urlparse.urljoin('http://www.viki.com', sturl),
  131. 'ext': 'vtt',
  132. }]
  133. return res