You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.8 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .crunchyroll import CrunchyrollIE
  5. from .common import InfoExtractor
  6. from ..compat import compat_HTTPError
  7. from ..utils import (
  8. ExtractorError,
  9. int_or_none,
  10. remove_start,
  11. xpath_text,
  12. )
  13. class SoompiBaseIE(InfoExtractor):
  14. def _get_episodes(self, webpage, episode_filter=None):
  15. episodes = self._parse_json(
  16. self._search_regex(
  17. r'VIDEOS\s*=\s*(\[.+?\]);', webpage, 'episodes JSON'),
  18. None)
  19. return list(filter(episode_filter, episodes))
  20. class SoompiIE(SoompiBaseIE, CrunchyrollIE):
  21. IE_NAME = 'soompi'
  22. _VALID_URL = r'https?://tv\.soompi\.com/(?:en/)?watch/(?P<id>[0-9]+)'
  23. _TESTS = [{
  24. 'url': 'http://tv.soompi.com/en/watch/29235',
  25. 'info_dict': {
  26. 'id': '29235',
  27. 'ext': 'mp4',
  28. 'title': 'Episode 1096',
  29. 'description': '2015-05-20'
  30. },
  31. 'params': {
  32. 'skip_download': True,
  33. },
  34. }]
  35. def _get_episode(self, webpage, video_id):
  36. return self._get_episodes(webpage, lambda x: x['id'] == video_id)[0]
  37. def _get_subtitles(self, config, video_id):
  38. sub_langs = {}
  39. for subtitle in config.findall('./{default}preload/subtitles/subtitle'):
  40. sub_langs[subtitle.attrib['id']] = subtitle.attrib['title']
  41. subtitles = {}
  42. for s in config.findall('./{default}preload/subtitle'):
  43. lang_code = sub_langs.get(s.attrib['id'])
  44. if not lang_code:
  45. continue
  46. sub_id = s.get('id')
  47. data = xpath_text(s, './data', 'data')
  48. iv = xpath_text(s, './iv', 'iv')
  49. if not id or not iv or not data:
  50. continue
  51. subtitle = self._decrypt_subtitles(data, iv, sub_id).decode('utf-8')
  52. subtitles[lang_code] = self._extract_subtitles(subtitle)
  53. return subtitles
  54. def _real_extract(self, url):
  55. video_id = self._match_id(url)
  56. try:
  57. webpage = self._download_webpage(
  58. url, video_id, 'Downloading episode page')
  59. except ExtractorError as ee:
  60. if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
  61. webpage = ee.cause.read()
  62. block_message = self._html_search_regex(
  63. r'(?s)<div class="block-message">(.+?)</div>', webpage,
  64. 'block message', default=None)
  65. if block_message:
  66. raise ExtractorError(block_message, expected=True)
  67. raise
  68. formats = []
  69. config = None
  70. for format_id in re.findall(r'\?quality=([0-9a-zA-Z]+)', webpage):
  71. config = self._download_xml(
  72. 'http://tv.soompi.com/en/show/_/%s-config.xml?mode=hls&quality=%s' % (video_id, format_id),
  73. video_id, 'Downloading %s XML' % format_id)
  74. m3u8_url = xpath_text(
  75. config, './{default}preload/stream_info/file',
  76. '%s m3u8 URL' % format_id)
  77. if not m3u8_url:
  78. continue
  79. formats.extend(self._extract_m3u8_formats(
  80. m3u8_url, video_id, 'mp4', m3u8_id=format_id))
  81. self._sort_formats(formats)
  82. episode = self._get_episode(webpage, video_id)
  83. title = episode['name']
  84. description = episode.get('description')
  85. duration = int_or_none(episode.get('duration'))
  86. thumbnails = [{
  87. 'id': thumbnail_id,
  88. 'url': thumbnail_url,
  89. } for thumbnail_id, thumbnail_url in episode.get('img_url', {}).items()]
  90. subtitles = self.extract_subtitles(config, video_id)
  91. return {
  92. 'id': video_id,
  93. 'title': title,
  94. 'description': description,
  95. 'thumbnails': thumbnails,
  96. 'duration': duration,
  97. 'formats': formats,
  98. 'subtitles': subtitles
  99. }
  100. class SoompiShowIE(SoompiBaseIE):
  101. IE_NAME = 'soompi:show'
  102. _VALID_URL = r'https?://tv\.soompi\.com/en/shows/(?P<id>[0-9a-zA-Z\-_]+)'
  103. _TESTS = [{
  104. 'url': 'http://tv.soompi.com/en/shows/liar-game',
  105. 'info_dict': {
  106. 'id': 'liar-game',
  107. 'title': 'Liar Game',
  108. 'description': 'md5:52c02bce0c1a622a95823591d0589b66',
  109. },
  110. 'playlist_count': 14,
  111. }]
  112. def _real_extract(self, url):
  113. show_id = self._match_id(url)
  114. webpage = self._download_webpage(
  115. url, show_id, 'Downloading show page')
  116. title = remove_start(self._og_search_title(webpage), 'SoompiTV | ')
  117. description = self._og_search_description(webpage)
  118. entries = [
  119. self.url_result('http://tv.soompi.com/en/watch/%s' % episode['id'], 'Soompi')
  120. for episode in self._get_episodes(webpage)]
  121. return self.playlist_result(entries, show_id, title, description)