viki.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from __future__ import unicode_literals
  2. import re
  3. from ..utils import (
  4. ExtractorError,
  5. unescapeHTML,
  6. unified_strdate,
  7. )
  8. from .subtitles import SubtitlesInfoExtractor
  9. class VikiIE(SubtitlesInfoExtractor):
  10. IE_NAME = 'viki'
  11. _VALID_URL = r'^https?://(?:www\.)?viki\.com/videos/(?P<id>[0-9]+v)'
  12. _TEST = {
  13. 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
  14. 'md5': 'a21454021c2646f5433514177e2caa5f',
  15. 'info_dict': {
  16. 'id': '1023585v',
  17. 'ext': 'mp4',
  18. 'title': 'Heirs Episode 14',
  19. 'uploader': 'SBS',
  20. 'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e',
  21. 'upload_date': '20131121',
  22. 'age_limit': 13,
  23. },
  24. 'skip': 'Blocked in the US',
  25. }
  26. def _real_extract(self, url):
  27. mobj = re.match(self._VALID_URL, url)
  28. video_id = mobj.group(1)
  29. webpage = self._download_webpage(url, video_id)
  30. title = self._og_search_title(webpage)
  31. description = self._og_search_description(webpage)
  32. thumbnail = self._og_search_thumbnail(webpage)
  33. uploader_m = re.search(
  34. r'<strong>Broadcast Network: </strong>\s*([^<]*)<', webpage)
  35. if uploader_m is None:
  36. uploader = None
  37. else:
  38. uploader = uploader_m.group(1).strip()
  39. rating_str = self._html_search_regex(
  40. r'<strong>Rating: </strong>\s*([^<]*)<', webpage,
  41. 'rating information', default='').strip()
  42. RATINGS = {
  43. 'G': 0,
  44. 'PG': 10,
  45. 'PG-13': 13,
  46. 'R': 16,
  47. 'NC': 18,
  48. }
  49. age_limit = RATINGS.get(rating_str)
  50. info_url = 'http://www.viki.com/player5_fragment/%s?action=show&controller=videos' % video_id
  51. info_webpage = self._download_webpage(
  52. info_url, video_id, note='Downloading info page')
  53. if re.match(r'\s*<div\s+class="video-error', info_webpage):
  54. raise ExtractorError(
  55. 'Video %s is blocked from your location.' % video_id,
  56. expected=True)
  57. video_url = self._html_search_regex(
  58. r'<source[^>]+src="([^"]+)"', info_webpage, 'video URL')
  59. upload_date_str = self._html_search_regex(
  60. r'"created_at":"([^"]+)"', info_webpage, 'upload date')
  61. upload_date = (
  62. unified_strdate(upload_date_str)
  63. if upload_date_str is not None
  64. else None
  65. )
  66. # subtitles
  67. video_subtitles = self.extract_subtitles(video_id, info_webpage)
  68. if self._downloader.params.get('listsubtitles', False):
  69. self._list_available_subtitles(video_id, info_webpage)
  70. return
  71. return {
  72. 'id': video_id,
  73. 'title': title,
  74. 'url': video_url,
  75. 'description': description,
  76. 'thumbnail': thumbnail,
  77. 'age_limit': age_limit,
  78. 'uploader': uploader,
  79. 'subtitles': video_subtitles,
  80. 'upload_date': upload_date,
  81. }
  82. def _get_available_subtitles(self, video_id, info_webpage):
  83. res = {}
  84. for sturl_html in re.findall(r'<track src="([^"]+)"/>', info_webpage):
  85. sturl = unescapeHTML(sturl_html)
  86. m = re.search(r'/(?P<lang>[a-z]+)\.vtt', sturl)
  87. if not m:
  88. continue
  89. res[m.group('lang')] = sturl
  90. return res