viki.py 3.2 KB

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