viki.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. )
  13. from .common import InfoExtractor
  14. class VikiIE(InfoExtractor):
  15. IE_NAME = 'viki'
  16. # iPad2
  17. _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'
  18. _VALID_URL = r'^https?://(?:www\.)?viki\.com/videos/(?P<id>[0-9]+v)'
  19. _TESTS = [{
  20. 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
  21. 'info_dict': {
  22. 'id': '1023585v',
  23. 'ext': 'mp4',
  24. 'title': 'Heirs Episode 14',
  25. 'uploader': 'SBS',
  26. 'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e',
  27. 'upload_date': '20131121',
  28. 'age_limit': 13,
  29. },
  30. 'skip': 'Blocked in the US',
  31. }, {
  32. 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
  33. 'md5': 'ca6493e6f0a6ec07da9aa8d6304b4b2c',
  34. 'info_dict': {
  35. 'id': '1067139v',
  36. 'ext': 'mp4',
  37. 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
  38. 'upload_date': '20150430',
  39. 'title': '\'The Avengers: Age of Ultron\' Press Conference',
  40. }
  41. }]
  42. def _real_extract(self, url):
  43. video_id = self._match_id(url)
  44. webpage = self._download_webpage(url, video_id)
  45. title = self._og_search_title(webpage)
  46. description = self._og_search_description(webpage)
  47. thumbnail = self._og_search_thumbnail(webpage)
  48. uploader_m = re.search(
  49. r'<strong>Broadcast Network: </strong>\s*([^<]*)<', webpage)
  50. if uploader_m is None:
  51. uploader = None
  52. else:
  53. uploader = uploader_m.group(1).strip()
  54. rating_str = self._html_search_regex(
  55. r'<strong>Rating: </strong>\s*([^<]*)<', webpage,
  56. 'rating information', default='').strip()
  57. age_limit = US_RATINGS.get(rating_str)
  58. req = compat_urllib_request.Request(
  59. 'http://www.viki.com/player5_fragment/%s?action=show&controller=videos' % video_id)
  60. req.add_header('User-Agent', self._USER_AGENT)
  61. info_webpage = self._download_webpage(
  62. req, video_id, note='Downloading info page')
  63. if re.match(r'\s*<div\s+class="video-error', info_webpage):
  64. raise ExtractorError(
  65. 'Video %s is blocked from your location.' % video_id,
  66. expected=True)
  67. video_url = self._html_search_regex(
  68. r'<source[^>]+src="([^"]+)"', info_webpage, 'video URL')
  69. upload_date_str = self._html_search_regex(
  70. r'"created_at":"([^"]+)"', info_webpage, 'upload date')
  71. upload_date = (
  72. unified_strdate(upload_date_str)
  73. if upload_date_str is not None
  74. else None
  75. )
  76. # subtitles
  77. video_subtitles = self.extract_subtitles(video_id, info_webpage)
  78. return {
  79. 'id': video_id,
  80. 'title': title,
  81. 'url': video_url,
  82. 'description': description,
  83. 'thumbnail': thumbnail,
  84. 'age_limit': age_limit,
  85. 'uploader': uploader,
  86. 'subtitles': video_subtitles,
  87. 'upload_date': upload_date,
  88. }
  89. def _get_subtitles(self, video_id, info_webpage):
  90. res = {}
  91. for sturl_html in re.findall(r'<track src="([^"]+)"', info_webpage):
  92. sturl = unescapeHTML(sturl_html)
  93. m = re.search(r'/(?P<lang>[a-z]+)\.vtt', sturl)
  94. if not m:
  95. continue
  96. res[m.group('lang')] = [{
  97. 'url': compat_urlparse.urljoin('http://www.viki.com', sturl),
  98. 'ext': 'vtt',
  99. }]
  100. return res