[daisuki] Add subtitles (#4738)
[youtube-dl] / youtube_dl / extractor / daisuki.py
1 from __future__ import unicode_literals
2
3 import base64
4 import json
5 import random
6 import re
7
8 from .common import InfoExtractor
9 from ..aes import (
10     aes_cbc_decrypt,
11     aes_cbc_encrypt,
12 )
13 from ..utils import (
14     bytes_to_intlist,
15     bytes_to_long,
16     clean_html,
17     ExtractorError,
18     intlist_to_bytes,
19     get_element_by_id,
20     js_to_json,
21     int_or_none,
22     long_to_bytes,
23     pkcs1pad,
24     remove_end,
25 )
26
27
28 class DaisukiIE(InfoExtractor):
29     _VALID_URL = r'https?://(?:www\.)?daisuki\.net/[^/]+/[^/]+/[^/]+/watch\.[^.]+\.(?P<id>\d+)\.html'
30
31     _TEST = {
32         'url': 'http://www.daisuki.net/tw/en/anime/watch.TheIdolMasterCG.11213.html',
33         'info_dict': {
34             'id': '11213',
35             'ext': 'mp4',
36             'title': '#01 Who is in the pumpkin carriage? - THE IDOLM@STER CINDERELLA GIRLS',
37             'subtitles': {
38                 'mul': [{
39                     'ext': 'ttml',
40                 }],
41             },
42             'creator': 'BANDAI NAMCO Entertainment',
43         },
44         'params': {
45             'skip_download': True,  # AES-encrypted HLS stream
46         },
47     }
48
49     # The public key in PEM format can be found in clientlibs_anime_watch.min.js
50     _RSA_KEY = (0xc5524c25e8e14b366b3754940beeb6f96cb7e2feef0b932c7659a0c5c3bf173d602464c2df73d693b513ae06ff1be8f367529ab30bf969c5640522181f2a0c51ea546ae120d3d8d908595e4eff765b389cde080a1ef7f1bbfb07411cc568db73b7f521cedf270cbfbe0ddbc29b1ac9d0f2d8f4359098caffee6d07915020077d, 65537)
51
52     def _real_extract(self, url):
53         video_id = self._match_id(url)
54
55         webpage = self._download_webpage(url, video_id)
56
57         flashvars = self._parse_json(self._search_regex(
58             r'(?s)var\s+flashvars\s*=\s*({.+?});', webpage, 'flashvars'),
59             video_id, transform_source=js_to_json)
60
61         iv = [0] * 16
62
63         data = {}
64         for key in ('device_cd', 'mv_id', 'ss1_prm', 'ss2_prm', 'ss3_prm', 'ss_id'):
65             data[key] = flashvars.get(key, '')
66
67         encrypted_rtn = None
68
69         # Some AES keys are rejected. Try it with different AES keys
70         for idx in range(5):
71             aes_key = [random.randint(0, 254) for _ in range(32)]
72             padded_aeskey = intlist_to_bytes(pkcs1pad(aes_key, 128))
73
74             n, e = self._RSA_KEY
75             encrypted_aeskey = long_to_bytes(pow(bytes_to_long(padded_aeskey), e, n))
76             init_data = self._download_json('http://www.daisuki.net/bin/bgn/init', video_id, query={
77                 's': flashvars.get('s', ''),
78                 'c': flashvars.get('ss3_prm', ''),
79                 'e': url,
80                 'd': base64.b64encode(intlist_to_bytes(aes_cbc_encrypt(
81                     bytes_to_intlist(json.dumps(data)),
82                     aes_key, iv))).decode('ascii'),
83                 'a': base64.b64encode(encrypted_aeskey).decode('ascii'),
84             }, note='Downloading JSON metadata' + (' (try #%d)' % (idx + 1) if idx > 0 else ''))
85
86             if 'rtn' in init_data:
87                 encrypted_rtn = init_data['rtn']
88                 break
89
90             self._sleep(5, video_id)
91
92         if encrypted_rtn is None:
93             raise ExtractorError('Failed to fetch init data')
94
95         rtn = self._parse_json(
96             intlist_to_bytes(aes_cbc_decrypt(bytes_to_intlist(
97                 base64.b64decode(encrypted_rtn)),
98                 aes_key, iv)).decode('utf-8').rstrip('\0'),
99             video_id)
100
101         formats = self._extract_m3u8_formats(
102             rtn['play_url'], video_id, ext='mp4', entry_protocol='m3u8_native')
103
104         title = remove_end(self._og_search_title(webpage), ' - DAISUKI')
105
106         creator = self._html_search_regex(
107             r'Creator\s*:\s*([^<]+)', webpage, 'creator', fatal=False)
108
109         subtitles = {}
110         caption_url = rtn.get('caption_url')
111         if caption_url:
112             # mul: multiple languages
113             subtitles['mul'] = [{
114                 'url': caption_url,
115                 'ext': 'ttml',
116             }]
117
118         return {
119             'id': video_id,
120             'title': title,
121             'formats': formats,
122             'subtitles': subtitles,
123             'creator': creator,
124         }
125
126
127 class DaisukiPlaylistIE(InfoExtractor):
128     _VALID_URL = r'https?://(?:www\.)daisuki\.net/[^/]+/[^/]+/[^/]+/detail\.(?P<id>[a-zA-Z0-9]+)\.html'
129
130     _TEST = {
131         'url': 'http://www.daisuki.net/tw/en/anime/detail.TheIdolMasterCG.html',
132         'info_dict': {
133             'id': 'TheIdolMasterCG',
134             'title': 'THE IDOLM@STER CINDERELLA GIRLS',
135             'description': 'md5:0f2c028a9339f7a2c7fbf839edc5c5d8',
136         },
137         'playlist_count': 26,
138     }
139
140     def _real_extract(self, url):
141         playlist_id = self._match_id(url)
142
143         webpage = self._download_webpage(url, playlist_id)
144
145         episode_pattern = r'''(?sx)
146             <img[^>]+delay="[^"]+/(\d+)/movie\.jpg".+?
147             <p[^>]+class=".*?\bepisodeNumber\b.*?">(?:<a[^>]+>)?([^<]+)'''
148         entries = [{
149             '_type': 'url_transparent',
150             'url': url.replace('detail', 'watch').replace('.html', '.' + movie_id + '.html'),
151             'episode_id': episode_id,
152             'episode_number': int_or_none(episode_id),
153         } for movie_id, episode_id in re.findall(episode_pattern, webpage)]
154
155         playlist_title = remove_end(
156             self._og_search_title(webpage, fatal=False), ' - Anime - DAISUKI')
157         playlist_description = clean_html(get_element_by_id('synopsisTxt', webpage))
158
159         return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)