soompi.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. import base64
  6. import xml.etree.ElementTree
  7. # Soompi uses the same subtitle encryption as crunchyroll
  8. from .crunchyroll import CrunchyrollIE
  9. class SoompiIE(CrunchyrollIE):
  10. IE_NAME = 'soompi'
  11. _VALID_URL = r'^https?://tv\.soompi\.com/en/watch/(?P<id>[0-9]+)'
  12. _TESTS = [{
  13. 'url': 'http://tv.soompi.com/en/watch/29235',
  14. 'info_dict': {
  15. 'id': '29235',
  16. 'ext': 'mp4',
  17. 'title': 'Episode 1096',
  18. 'description': '2015-05-20'
  19. },
  20. 'params': {
  21. 'skip_download': True,
  22. },
  23. }]
  24. def _get_episodes(self, webpage, episode_filter=None):
  25. episodes = json.loads(
  26. self._search_regex(r'\s+VIDEOS\s+= (\[.+?\]);', webpage, "episodes meta"))
  27. return [ep for ep in episodes if episode_filter is None or episode_filter(ep)]
  28. def _get_subtitles(self, video_id, show_format_xml):
  29. subtitles = {}
  30. subtitle_info_nodes = show_format_xml.findall('./{default}preload/subtitles/subtitle')
  31. subtitle_nodes = show_format_xml.findall('./{default}preload/subtitle')
  32. sub_langs = {}
  33. for i in subtitle_info_nodes:
  34. sub_langs[i.attrib["id"]] = i.attrib["title"]
  35. for s in subtitle_nodes:
  36. lang_code = sub_langs.get(s.attrib["id"], None)
  37. if lang_code is None:
  38. continue
  39. sub_id = int(s.attrib["id"])
  40. iv = base64.b64decode(s.find("iv").text)
  41. data = base64.b64decode(s.find("data").text)
  42. subtitle = self._decrypt_subtitles(data, iv, sub_id).decode('utf-8')
  43. sub_root = xml.etree.ElementTree.fromstring(subtitle)
  44. subtitles[lang_code] = [{
  45. 'ext': 'srt', 'data': self._convert_subtitles_to_srt(sub_root)
  46. }, {
  47. 'ext': 'ass', 'data': self._convert_subtitles_to_ass(sub_root)
  48. }]
  49. return subtitles
  50. def _real_extract(self, url):
  51. video_id = self._match_id(url)
  52. webpage = self._download_webpage(
  53. url, video_id, note="Downloading episode page",
  54. errnote="Video may not be available for your location")
  55. vid_formats = re.findall(r"\?quality=q([0-9]+)", webpage)
  56. show_meta = json.loads(
  57. self._search_regex(r'\s+var show = (\{.+?\});', webpage, "show meta"))
  58. episodes = self._get_episodes(
  59. webpage, episode_filter=lambda x: x['id'] == video_id)
  60. title = episodes[0]["name"]
  61. description = episodes[0]["description"]
  62. duration = int(episodes[0]["duration"])
  63. slug = show_meta["slug"]
  64. formats = []
  65. show_format_xml = None
  66. for vf in vid_formats:
  67. show_format_url = "http://tv.soompi.com/en/show/%s/%s-config.xml?mode=hls&quality=q%s" \
  68. % (slug, video_id, vf)
  69. show_format_xml = self._download_xml(
  70. show_format_url, video_id, note="Downloading q%s show xml" % vf)
  71. avail_formats = self._extract_m3u8_formats(
  72. show_format_xml.find('./{default}preload/stream_info/file').text,
  73. video_id, ext="mp4", m3u8_id=vf, preference=int(vf))
  74. formats.extend(avail_formats)
  75. self._sort_formats(formats)
  76. subtitles = self.extract_subtitles(video_id, show_format_xml)
  77. return {
  78. 'id': video_id,
  79. 'title': title,
  80. 'description': description,
  81. 'duration': duration,
  82. 'formats': formats,
  83. 'subtitles': subtitles
  84. }
  85. class SoompiShowIE(SoompiIE):
  86. IE_NAME = 'soompi:show'
  87. _VALID_URL = r'^https?://tv\.soompi\.com/en/shows/(?P<id>[0-9a-zA-Z\-_]+)'
  88. _TESTS = [{
  89. 'url': 'http://tv.soompi.com/en/shows/liar-game',
  90. 'info_dict': {
  91. 'id': 'liar-game',
  92. 'title': 'Liar Game',
  93. 'description': 'md5:52c02bce0c1a622a95823591d0589b66',
  94. },
  95. 'playlist_count': 14,
  96. }]
  97. def _real_extract(self, url):
  98. show_id = self._match_id(url)
  99. webpage = self._download_webpage(url, show_id, note="Downloading show page")
  100. title = self._og_search_title(webpage).replace("SoompiTV | ", "")
  101. description = self._og_search_description(webpage)
  102. episodes = self._get_episodes(webpage)
  103. entries = []
  104. for ep in episodes:
  105. entries.append(self.url_result(
  106. 'http://tv.soompi.com/en/watch/%s' % ep['id'], 'Soompi', ep['id']))
  107. return self.playlist_result(entries, show_id, title, description)