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