]>
Commit | Line | Data |
---|---|---|
1 | import base64 | |
2 | import urllib.parse | |
3 | ||
4 | from .common import InfoExtractor | |
5 | from ..utils import ( | |
6 | ExtractorError, | |
7 | clean_html, | |
8 | time_seconds, | |
9 | try_call, | |
10 | unified_timestamp, | |
11 | update_url_query, | |
12 | ) | |
13 | ||
14 | ||
15 | class RadikoBaseIE(InfoExtractor): | |
16 | _FULL_KEY = None | |
17 | _HOSTS_FOR_TIME_FREE_FFMPEG_UNSUPPORTED = ( | |
18 | 'https://c-rpaa.smartstream.ne.jp', | |
19 | 'https://si-c-radiko.smartstream.ne.jp', | |
20 | 'https://tf-f-rpaa-radiko.smartstream.ne.jp', | |
21 | 'https://tf-c-rpaa-radiko.smartstream.ne.jp', | |
22 | 'https://si-f-radiko.smartstream.ne.jp', | |
23 | 'https://rpaa.smartstream.ne.jp', | |
24 | ) | |
25 | _HOSTS_FOR_TIME_FREE_FFMPEG_SUPPORTED = ( | |
26 | 'https://rd-wowza-radiko.radiko-cf.com', | |
27 | 'https://radiko.jp', | |
28 | 'https://f-radiko.smartstream.ne.jp', | |
29 | ) | |
30 | # Following URL forcibly connects not Time Free but Live | |
31 | _HOSTS_FOR_LIVE = ( | |
32 | 'https://c-radiko.smartstream.ne.jp', | |
33 | ) | |
34 | ||
35 | def _auth_client(self): | |
36 | _, auth1_handle = self._download_webpage_handle( | |
37 | 'https://radiko.jp/v2/api/auth1', None, 'Downloading authentication page', | |
38 | headers={ | |
39 | 'x-radiko-app': 'pc_html5', | |
40 | 'x-radiko-app-version': '0.0.1', | |
41 | 'x-radiko-device': 'pc', | |
42 | 'x-radiko-user': 'dummy_user', | |
43 | }) | |
44 | auth1_header = auth1_handle.info() | |
45 | ||
46 | auth_token = auth1_header['X-Radiko-AuthToken'] | |
47 | kl = int(auth1_header['X-Radiko-KeyLength']) | |
48 | ko = int(auth1_header['X-Radiko-KeyOffset']) | |
49 | raw_partial_key = self._extract_full_key()[ko:ko + kl] | |
50 | partial_key = base64.b64encode(raw_partial_key).decode() | |
51 | ||
52 | area_id = self._download_webpage( | |
53 | 'https://radiko.jp/v2/api/auth2', None, 'Authenticating', | |
54 | headers={ | |
55 | 'x-radiko-device': 'pc', | |
56 | 'x-radiko-user': 'dummy_user', | |
57 | 'x-radiko-authtoken': auth_token, | |
58 | 'x-radiko-partialkey': partial_key, | |
59 | }).split(',')[0] | |
60 | ||
61 | auth_data = (auth_token, area_id) | |
62 | self.cache.store('radiko', 'auth_data', auth_data) | |
63 | return auth_data | |
64 | ||
65 | def _extract_full_key(self): | |
66 | if self._FULL_KEY: | |
67 | return self._FULL_KEY | |
68 | ||
69 | jscode = self._download_webpage( | |
70 | 'https://radiko.jp/apps/js/playerCommon.js', None, | |
71 | note='Downloading player js code') | |
72 | full_key = self._search_regex( | |
73 | (r"RadikoJSPlayer\([^,]*,\s*(['\"])pc_html5\1,\s*(['\"])(?P<fullkey>[0-9a-f]+)\2,\s*{"), | |
74 | jscode, 'full key', fatal=False, group='fullkey') | |
75 | ||
76 | if full_key: | |
77 | full_key = full_key.encode() | |
78 | else: # use full key ever known | |
79 | full_key = b'bcd151073c03b352e1ef2fd66c32209da9ca0afa' | |
80 | ||
81 | self._FULL_KEY = full_key | |
82 | return full_key | |
83 | ||
84 | def _find_program(self, video_id, station, cursor): | |
85 | station_program = self._download_xml( | |
86 | 'https://radiko.jp/v3/program/station/weekly/%s.xml' % station, video_id, | |
87 | note='Downloading radio program for %s station' % station) | |
88 | ||
89 | prog = None | |
90 | for p in station_program.findall('.//prog'): | |
91 | ft_str, to_str = p.attrib['ft'], p.attrib['to'] | |
92 | ft = unified_timestamp(ft_str, False) | |
93 | to = unified_timestamp(to_str, False) | |
94 | if ft <= cursor and cursor < to: | |
95 | prog = p | |
96 | break | |
97 | if not prog: | |
98 | raise ExtractorError('Cannot identify radio program to download!') | |
99 | assert ft, to | |
100 | return prog, station_program, ft, ft_str, to_str | |
101 | ||
102 | def _extract_formats(self, video_id, station, is_onair, ft, cursor, auth_token, area_id, query): | |
103 | m3u8_playlist_data = self._download_xml( | |
104 | f'https://radiko.jp/v3/station/stream/pc_html5/{station}.xml', video_id, | |
105 | note='Downloading stream information') | |
106 | m3u8_urls = m3u8_playlist_data.findall('.//url') | |
107 | ||
108 | formats = [] | |
109 | found = set() | |
110 | for url_tag in m3u8_urls: | |
111 | pcu = url_tag.find('playlist_create_url').text | |
112 | url_attrib = url_tag.attrib | |
113 | playlist_url = update_url_query(pcu, { | |
114 | 'station_id': station, | |
115 | **query, | |
116 | 'l': '15', | |
117 | 'lsid': '88ecea37e968c1f17d5413312d9f8003', | |
118 | 'type': 'b', | |
119 | }) | |
120 | if playlist_url in found: | |
121 | continue | |
122 | else: | |
123 | found.add(playlist_url) | |
124 | ||
125 | time_to_skip = None if is_onair else cursor - ft | |
126 | ||
127 | domain = urllib.parse.urlparse(playlist_url).netloc | |
128 | subformats = self._extract_m3u8_formats( | |
129 | playlist_url, video_id, ext='m4a', | |
130 | live=True, fatal=False, m3u8_id=domain, | |
131 | note=f'Downloading m3u8 information from {domain}', | |
132 | headers={ | |
133 | 'X-Radiko-AreaId': area_id, | |
134 | 'X-Radiko-AuthToken': auth_token, | |
135 | }) | |
136 | for sf in subformats: | |
137 | if (is_onair ^ pcu.startswith(self._HOSTS_FOR_LIVE)) or ( | |
138 | not is_onair and pcu.startswith(self._HOSTS_FOR_TIME_FREE_FFMPEG_UNSUPPORTED)): | |
139 | sf['preference'] = -100 | |
140 | sf['format_note'] = 'not preferred' | |
141 | if not is_onair and url_attrib['timefree'] == '1' and time_to_skip: | |
142 | sf['downloader_options'] = {'ffmpeg_args': ['-ss', time_to_skip]} | |
143 | formats.extend(subformats) | |
144 | ||
145 | return formats | |
146 | ||
147 | ||
148 | class RadikoIE(RadikoBaseIE): | |
149 | _VALID_URL = r'https?://(?:www\.)?radiko\.jp/#!/ts/(?P<station>[A-Z0-9-]+)/(?P<id>\d+)' | |
150 | ||
151 | _TESTS = [{ | |
152 | # QRR (文化放送) station provides <desc> | |
153 | 'url': 'https://radiko.jp/#!/ts/QRR/20210425101300', | |
154 | 'only_matching': True, | |
155 | }, { | |
156 | # FMT (TOKYO FM) station does not provide <desc> | |
157 | 'url': 'https://radiko.jp/#!/ts/FMT/20210810150000', | |
158 | 'only_matching': True, | |
159 | }, { | |
160 | 'url': 'https://radiko.jp/#!/ts/JOAK-FM/20210509090000', | |
161 | 'only_matching': True, | |
162 | }] | |
163 | ||
164 | def _real_extract(self, url): | |
165 | station, video_id = self._match_valid_url(url).groups() | |
166 | vid_int = unified_timestamp(video_id, False) | |
167 | prog, station_program, ft, radio_begin, radio_end = self._find_program(video_id, station, vid_int) | |
168 | ||
169 | auth_cache = self.cache.load('radiko', 'auth_data') | |
170 | for attempt in range(2): | |
171 | auth_token, area_id = (not attempt and auth_cache) or self._auth_client() | |
172 | formats = self._extract_formats( | |
173 | video_id=video_id, station=station, is_onair=False, | |
174 | ft=ft, cursor=vid_int, auth_token=auth_token, area_id=area_id, | |
175 | query={ | |
176 | 'start_at': radio_begin, | |
177 | 'ft': radio_begin, | |
178 | 'end_at': radio_end, | |
179 | 'to': radio_end, | |
180 | 'seek': video_id, | |
181 | }) | |
182 | if formats: | |
183 | break | |
184 | ||
185 | return { | |
186 | 'id': video_id, | |
187 | 'title': try_call(lambda: prog.find('title').text), | |
188 | 'description': clean_html(try_call(lambda: prog.find('info').text)), | |
189 | 'uploader': try_call(lambda: station_program.find('.//name').text), | |
190 | 'uploader_id': station, | |
191 | 'timestamp': vid_int, | |
192 | 'formats': formats, | |
193 | 'is_live': True, | |
194 | } | |
195 | ||
196 | ||
197 | class RadikoRadioIE(RadikoBaseIE): | |
198 | _VALID_URL = r'https?://(?:www\.)?radiko\.jp/#!/live/(?P<id>[A-Z0-9-]+)' | |
199 | ||
200 | _TESTS = [{ | |
201 | # QRR (文化放送) station provides <desc> | |
202 | 'url': 'https://radiko.jp/#!/live/QRR', | |
203 | 'only_matching': True, | |
204 | }, { | |
205 | # FMT (TOKYO FM) station does not provide <desc> | |
206 | 'url': 'https://radiko.jp/#!/live/FMT', | |
207 | 'only_matching': True, | |
208 | }, { | |
209 | 'url': 'https://radiko.jp/#!/live/JOAK-FM', | |
210 | 'only_matching': True, | |
211 | }] | |
212 | ||
213 | def _real_extract(self, url): | |
214 | station = self._match_id(url) | |
215 | self.report_warning('Downloader will not stop at the end of the program! Press Ctrl+C to stop') | |
216 | ||
217 | auth_token, area_id = self._auth_client() | |
218 | # get current time in JST (GMT+9:00 w/o DST) | |
219 | vid_now = time_seconds(hours=9) | |
220 | ||
221 | prog, station_program, ft, _, _ = self._find_program(station, station, vid_now) | |
222 | ||
223 | title = prog.find('title').text | |
224 | description = clean_html(prog.find('info').text) | |
225 | station_name = station_program.find('.//name').text | |
226 | ||
227 | formats = self._extract_formats( | |
228 | video_id=station, station=station, is_onair=True, | |
229 | ft=ft, cursor=vid_now, auth_token=auth_token, area_id=area_id, | |
230 | query={}) | |
231 | ||
232 | return { | |
233 | 'id': station, | |
234 | 'title': title, | |
235 | 'description': description, | |
236 | 'uploader': station_name, | |
237 | 'uploader_id': station, | |
238 | 'timestamp': ft, | |
239 | 'formats': formats, | |
240 | 'is_live': True, | |
241 | } |