viki.py 3.0 KB

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