viki.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 = self._html_search_regex(
  31. r'<strong>Broadcast Network: </strong>\s*([^<]*)<', webpage,
  32. u'uploader')
  33. if uploader is not None:
  34. uploader = uploader.strip()
  35. rating_str = self._html_search_regex(
  36. r'<strong>Rating: </strong>\s*([^<]*)<', webpage,
  37. u'rating information', default='').strip()
  38. RATINGS = {
  39. 'G': 0,
  40. 'PG': 10,
  41. 'PG-13': 13,
  42. 'R': 16,
  43. 'NC': 18,
  44. }
  45. age_limit = RATINGS.get(rating_str)
  46. info_url = 'http://www.viki.com/player5_fragment/%s?action=show&controller=videos' % video_id
  47. info_webpage = self._download_webpage(
  48. info_url, video_id, note=u'Downloading info page')
  49. if re.match(r'\s*<div\s+class="video-error', info_webpage):
  50. raise ExtractorError(
  51. u'Video %s is blocked from your location.' % video_id,
  52. expected=True)
  53. video_url = self._html_search_regex(
  54. r'<source[^>]+src="([^"]+)"', info_webpage, u'video URL')
  55. upload_date_str = self._html_search_regex(
  56. r'"created_at":"([^"]+)"', info_webpage, u'upload date')
  57. upload_date = (
  58. unified_strdate(upload_date_str)
  59. if upload_date_str is not None
  60. else None
  61. )
  62. # subtitles
  63. video_subtitles = self.extract_subtitles(video_id, info_webpage)
  64. if self._downloader.params.get('listsubtitles', False):
  65. self._list_available_subtitles(video_id, info_webpage)
  66. return
  67. return {
  68. 'id': video_id,
  69. 'title': title,
  70. 'url': video_url,
  71. 'description': description,
  72. 'thumbnail': thumbnail,
  73. 'age_limit': age_limit,
  74. 'uploader': uploader,
  75. 'subtitles': video_subtitles,
  76. 'upload_date': upload_date,
  77. }
  78. def _get_available_subtitles(self, video_id, info_webpage):
  79. res = {}
  80. for sturl in re.findall(r'<track src="([^"]+)"/>'):
  81. m = re.search(r'/(?P<lang>[a-z]+)\.vtt', sturl)
  82. if not m:
  83. continue
  84. res[m.group('lang')] = sturl
  85. return res