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