]>
Commit | Line | Data |
---|---|---|
929ba399 | 1 | import random |
8cd69fc4 | 2 | import re |
929ba399 RA |
3 | import string |
4 | ||
ab4bdc91 | 5 | from .common import InfoExtractor |
804181dd | 6 | from ..compat import compat_HTTPError |
ab4bdc91 | 7 | from ..utils import ( |
f542a3d2 | 8 | determine_ext, |
f377f44d | 9 | int_or_none, |
34921b43 | 10 | join_nonempty, |
91399b2f | 11 | js_to_json, |
8cd69fc4 J |
12 | orderedSet, |
13 | qualities, | |
3acf6d38 | 14 | str_or_none, |
8cd69fc4 | 15 | traverse_obj, |
3acf6d38 | 16 | try_get, |
29f63c96 | 17 | urlencode_postdata, |
ab4bdc91 | 18 | ExtractorError, |
ab4bdc91 | 19 | ) |
ab4bdc91 | 20 | |
b4c299ba | 21 | |
8cd69fc4 J |
22 | class FunimationBaseIE(InfoExtractor): |
23 | _NETRC_MACHINE = 'funimation' | |
24 | _REGION = None | |
25 | _TOKEN = None | |
26 | ||
27 | def _get_region(self): | |
28 | region_cookie = self._get_cookies('https://www.funimation.com').get('region') | |
29 | region = region_cookie.value if region_cookie else self.get_param('geo_bypass_country') | |
30 | return region or traverse_obj( | |
31 | self._download_json( | |
32 | 'https://geo-service.prd.funimationsvc.com/geo/v1/region/check', None, fatal=False, | |
33 | note='Checking geo-location', errnote='Unable to fetch geo-location information'), | |
34 | 'region') or 'US' | |
35 | ||
52efa4b3 | 36 | def _perform_login(self, username, password): |
37 | if self._TOKEN: | |
8cd69fc4 J |
38 | return |
39 | try: | |
40 | data = self._download_json( | |
41 | 'https://prod-api-funimationnow.dadcdigital.com/api/auth/login/', | |
42 | None, 'Logging in', data=urlencode_postdata({ | |
43 | 'username': username, | |
44 | 'password': password, | |
45 | })) | |
52efa4b3 | 46 | FunimationBaseIE._TOKEN = data['token'] |
8cd69fc4 J |
47 | except ExtractorError as e: |
48 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401: | |
49 | error = self._parse_json(e.cause.read().decode(), None)['error'] | |
50 | raise ExtractorError(error, expected=True) | |
51 | raise | |
52 | ||
53 | ||
54 | class FunimationPageIE(FunimationBaseIE): | |
3acf6d38 | 55 | IE_NAME = 'funimation:page' |
8cd69fc4 | 56 | _VALID_URL = r'https?://(?:www\.)?funimation(?:\.com|now\.uk)/(?:(?P<lang>[^/]+)/)?(?:shows|v)/(?P<show>[^/]+)/(?P<episode>[^/?#&]+)' |
3acf6d38 | 57 | |
58 | _TESTS = [{ | |
59 | 'url': 'https://www.funimation.com/shows/attack-on-titan-junior-high/broadcast-dub-preview/', | |
60 | 'info_dict': { | |
61 | 'id': '210050', | |
62 | 'ext': 'mp4', | |
63 | 'title': 'Broadcast Dub Preview', | |
64 | # Other metadata is tested in FunimationIE | |
65 | }, | |
66 | 'params': { | |
67 | 'skip_download': 'm3u8', | |
68 | }, | |
69 | 'add_ie': ['Funimation'], | |
70 | }, { | |
71 | # Not available in US | |
72 | 'url': 'https://www.funimation.com/shows/hacksign/role-play/', | |
73 | 'only_matching': True, | |
74 | }, { | |
75 | # with lang code | |
76 | 'url': 'https://www.funimation.com/en/shows/hacksign/role-play/', | |
77 | 'only_matching': True, | |
78 | }, { | |
79 | 'url': 'https://www.funimationnow.uk/shows/puzzle-dragons-x/drop-impact/simulcast/', | |
80 | 'only_matching': True, | |
8cd69fc4 J |
81 | }, { |
82 | 'url': 'https://www.funimation.com/v/a-certain-scientific-railgun/super-powered-level-5', | |
83 | 'only_matching': True, | |
3acf6d38 | 84 | }] |
85 | ||
8cd69fc4 J |
86 | def _real_initialize(self): |
87 | if not self._REGION: | |
88 | FunimationBaseIE._REGION = self._get_region() | |
8cd69fc4 | 89 | |
3acf6d38 | 90 | def _real_extract(self, url): |
8cd69fc4 J |
91 | locale, show, episode = self._match_valid_url(url).group('lang', 'show', 'episode') |
92 | ||
93 | video_id = traverse_obj(self._download_json( | |
94 | f'https://title-api.prd.funimationsvc.com/v1/shows/{show}/episodes/{episode}', | |
95 | f'{show}_{episode}', query={ | |
96 | 'deviceType': 'web', | |
97 | 'region': self._REGION, | |
98 | 'locale': locale or 'en' | |
99 | }), ('videoList', ..., 'id'), get_all=False) | |
100 | ||
3acf6d38 | 101 | return self.url_result(f'https://www.funimation.com/player/{video_id}', FunimationIE.ie_key(), video_id) |
102 | ||
103 | ||
8cd69fc4 | 104 | class FunimationIE(FunimationBaseIE): |
3acf6d38 | 105 | _VALID_URL = r'https?://(?:www\.)?funimation\.com/player/(?P<id>\d+)' |
ab4bdc91 | 106 | |
b59623ef | 107 | _TESTS = [{ |
3acf6d38 | 108 | 'url': 'https://www.funimation.com/player/210051', |
ab4bdc91 | 109 | 'info_dict': { |
3acf6d38 | 110 | 'id': '210050', |
111 | 'display_id': 'broadcast-dub-preview', | |
b59623ef | 112 | 'ext': 'mp4', |
3acf6d38 | 113 | 'title': 'Broadcast Dub Preview', |
114 | 'thumbnail': r're:https?://.*\.(?:jpg|png)', | |
115 | 'episode': 'Broadcast Dub Preview', | |
116 | 'episode_id': '210050', | |
117 | 'season': 'Extras', | |
118 | 'season_id': '166038', | |
119 | 'season_number': 99, | |
120 | 'series': 'Attack on Titan: Junior High', | |
121 | 'description': '', | |
8cd69fc4 | 122 | 'duration': 155, |
b59623ef | 123 | }, |
91399b2f | 124 | 'params': { |
3acf6d38 | 125 | 'skip_download': 'm3u8', |
91399b2f | 126 | }, |
b091529a | 127 | }, { |
3acf6d38 | 128 | 'note': 'player_id should be extracted with the relevent compat-opt', |
129 | 'url': 'https://www.funimation.com/player/210051', | |
0b1bb1ac | 130 | 'info_dict': { |
804181dd | 131 | 'id': '210051', |
0b1bb1ac S |
132 | 'display_id': 'broadcast-dub-preview', |
133 | 'ext': 'mp4', | |
3acf6d38 | 134 | 'title': 'Broadcast Dub Preview', |
ec85ded8 | 135 | 'thumbnail': r're:https?://.*\.(?:jpg|png)', |
3acf6d38 | 136 | 'episode': 'Broadcast Dub Preview', |
137 | 'episode_id': '210050', | |
138 | 'season': 'Extras', | |
139 | 'season_id': '166038', | |
140 | 'season_number': 99, | |
141 | 'series': 'Attack on Titan: Junior High', | |
142 | 'description': '', | |
8cd69fc4 | 143 | 'duration': 155, |
0b1bb1ac | 144 | }, |
804181dd | 145 | 'params': { |
3acf6d38 | 146 | 'skip_download': 'm3u8', |
147 | 'compat_opts': ['seperate-video-versions'], | |
804181dd | 148 | }, |
b59623ef | 149 | }] |
f542a3d2 | 150 | |
3acf6d38 | 151 | @staticmethod |
152 | def _get_experiences(episode): | |
153 | for lang, lang_data in episode.get('languages', {}).items(): | |
154 | for video_data in lang_data.values(): | |
155 | for version, f in video_data.items(): | |
156 | yield lang, version.title(), f | |
f542a3d2 | 157 | |
3acf6d38 | 158 | def _get_episode(self, webpage, experience_id=None, episode_id=None, fatal=True): |
159 | ''' Extract the episode, season and show objects given either episode/experience id ''' | |
160 | show = self._parse_json( | |
161 | self._search_regex( | |
162 | r'show\s*=\s*({.+?})\s*;', webpage, 'show data', fatal=fatal), | |
163 | experience_id, transform_source=js_to_json, fatal=fatal) or [] | |
164 | for season in show.get('seasons', []): | |
165 | for episode in season.get('episodes', []): | |
166 | if episode_id is not None: | |
167 | if str(episode.get('episodePk')) == episode_id: | |
168 | return episode, season, show | |
169 | continue | |
170 | for _, _, f in self._get_experiences(episode): | |
171 | if f.get('experienceId') == experience_id: | |
172 | return episode, season, show | |
173 | if fatal: | |
174 | raise ExtractorError('Unable to find episode information') | |
175 | else: | |
176 | self.report_warning('Unable to find episode information') | |
177 | return {}, {}, {} | |
91399b2f | 178 | |
3acf6d38 | 179 | def _real_extract(self, url): |
180 | initial_experience_id = self._match_id(url) | |
181 | webpage = self._download_webpage( | |
182 | url, initial_experience_id, note=f'Downloading player webpage for {initial_experience_id}') | |
183 | episode, season, show = self._get_episode(webpage, experience_id=int(initial_experience_id)) | |
184 | episode_id = str(episode['episodePk']) | |
185 | display_id = episode.get('slug') or episode_id | |
91399b2f | 186 | |
3acf6d38 | 187 | formats, subtitles, thumbnails, duration = [], {}, [], 0 |
188 | requested_languages, requested_versions = self._configuration_arg('language'), self._configuration_arg('version') | |
e919569e | 189 | language_preference = qualities((requested_languages or [''])[::-1]) |
190 | source_preference = qualities((requested_versions or ['uncut', 'simulcast'])[::-1]) | |
3acf6d38 | 191 | only_initial_experience = 'seperate-video-versions' in self.get_param('compat_opts', []) |
192 | ||
193 | for lang, version, fmt in self._get_experiences(episode): | |
194 | experience_id = str(fmt['experienceId']) | |
195 | if (only_initial_experience and experience_id != initial_experience_id | |
4bb6b02f | 196 | or requested_languages and lang.lower() not in requested_languages |
197 | or requested_versions and version.lower() not in requested_versions): | |
3acf6d38 | 198 | continue |
199 | thumbnails.append({'url': fmt.get('poster')}) | |
200 | duration = max(duration, fmt.get('duration', 0)) | |
201 | format_name = '%s %s (%s)' % (version, lang, experience_id) | |
202 | self.extract_subtitles( | |
203 | subtitles, experience_id, display_id=display_id, format_name=format_name, | |
204 | episode=episode if experience_id == initial_experience_id else episode_id) | |
f542a3d2 | 205 | |
8fa17117 RA |
206 | headers = {} |
207 | if self._TOKEN: | |
208 | headers['Authorization'] = 'Token %s' % self._TOKEN | |
3acf6d38 | 209 | page = self._download_json( |
210 | 'https://www.funimation.com/api/showexperience/%s/' % experience_id, | |
211 | display_id, headers=headers, expected_status=403, query={ | |
929ba399 | 212 | 'pinst_id': ''.join([random.choice(string.digits + string.ascii_letters) for _ in range(8)]), |
3acf6d38 | 213 | }, note=f'Downloading {format_name} JSON') |
214 | sources = page.get('items') or [] | |
215 | if not sources: | |
216 | error = try_get(page, lambda x: x['errors'][0], dict) | |
217 | if error: | |
218 | self.report_warning('%s said: Error %s - %s' % ( | |
219 | self.IE_NAME, error.get('code'), error.get('detail') or error.get('title'))) | |
220 | else: | |
221 | self.report_warning('No sources found for format') | |
f542a3d2 | 222 | |
3acf6d38 | 223 | current_formats = [] |
224 | for source in sources: | |
225 | source_url = source.get('src') | |
226 | source_type = source.get('videoType') or determine_ext(source_url) | |
227 | if source_type == 'm3u8': | |
228 | current_formats.extend(self._extract_m3u8_formats( | |
229 | source_url, display_id, 'mp4', m3u8_id='%s-%s' % (experience_id, 'hls'), fatal=False, | |
230 | note=f'Downloading {format_name} m3u8 information')) | |
231 | else: | |
232 | current_formats.append({ | |
233 | 'format_id': '%s-%s' % (experience_id, source_type), | |
234 | 'url': source_url, | |
235 | }) | |
236 | for f in current_formats: | |
237 | # TODO: Convert language to code | |
e919569e | 238 | f.update({ |
239 | 'language': lang, | |
240 | 'format_note': version, | |
241 | 'source_preference': source_preference(version.lower()), | |
242 | 'language_preference': language_preference(lang.lower()), | |
243 | }) | |
3acf6d38 | 244 | formats.extend(current_formats) |
1d485a1a | 245 | if not formats and (requested_languages or requested_versions): |
246 | self.raise_no_formats( | |
247 | 'There are no video formats matching the requested languages/versions', expected=True, video_id=display_id) | |
3acf6d38 | 248 | self._remove_duplicate_formats(formats) |
e919569e | 249 | self._sort_formats(formats, ('lang', 'source')) |
b59623ef | 250 | |
ab4bdc91 | 251 | return { |
3acf6d38 | 252 | 'id': initial_experience_id if only_initial_experience else episode_id, |
f542a3d2 | 253 | 'display_id': display_id, |
3acf6d38 | 254 | 'duration': duration, |
255 | 'title': episode['episodeTitle'], | |
256 | 'description': episode.get('episodeSummary'), | |
257 | 'episode': episode.get('episodeTitle'), | |
258 | 'episode_number': int_or_none(episode.get('episodeId')), | |
259 | 'episode_id': episode_id, | |
260 | 'season': season.get('seasonTitle'), | |
261 | 'season_number': int_or_none(season.get('seasonId')), | |
262 | 'season_id': str_or_none(season.get('seasonPk')), | |
263 | 'series': show.get('showTitle'), | |
ab4bdc91 | 264 | 'formats': formats, |
3acf6d38 | 265 | 'thumbnails': thumbnails, |
266 | 'subtitles': subtitles, | |
ab4bdc91 | 267 | } |
29f63c96 | 268 | |
3acf6d38 | 269 | def _get_subtitles(self, subtitles, experience_id, episode, display_id, format_name): |
270 | if isinstance(episode, str): | |
271 | webpage = self._download_webpage( | |
9222c381 | 272 | f'https://www.funimation.com/player/{experience_id}/', display_id, |
3acf6d38 | 273 | fatal=False, note=f'Downloading player webpage for {format_name}') |
274 | episode, _, _ = self._get_episode(webpage, episode_id=episode, fatal=False) | |
275 | ||
276 | for _, version, f in self._get_experiences(episode): | |
277 | for source in f.get('sources'): | |
278 | for text_track in source.get('textTracks'): | |
279 | if not text_track.get('src'): | |
280 | continue | |
281 | sub_type = text_track.get('type').upper() | |
282 | sub_type = sub_type if sub_type != 'FULL' else None | |
283 | current_sub = { | |
284 | 'url': text_track['src'], | |
34921b43 | 285 | 'name': join_nonempty(version, text_track.get('label'), sub_type, delim=' ') |
3acf6d38 | 286 | } |
34921b43 | 287 | lang = join_nonempty(text_track.get('language', 'und'), |
288 | version if version != 'Simulcast' else None, | |
289 | sub_type, delim='_') | |
3acf6d38 | 290 | if current_sub not in subtitles.get(lang, []): |
291 | subtitles.setdefault(lang, []).append(current_sub) | |
29f63c96 | 292 | return subtitles |
125728b0 M |
293 | |
294 | ||
8cd69fc4 | 295 | class FunimationShowIE(FunimationBaseIE): |
125728b0 M |
296 | IE_NAME = 'funimation:show' |
297 | _VALID_URL = r'(?P<url>https?://(?:www\.)?funimation(?:\.com|now\.uk)/(?P<locale>[^/]+)?/?shows/(?P<id>[^/?#&]+))/?(?:[?#]|$)' | |
298 | ||
299 | _TESTS = [{ | |
300 | 'url': 'https://www.funimation.com/en/shows/sk8-the-infinity', | |
301 | 'info_dict': { | |
302 | 'id': 1315000, | |
303 | 'title': 'SK8 the Infinity' | |
304 | }, | |
305 | 'playlist_count': 13, | |
306 | 'params': { | |
307 | 'skip_download': True, | |
308 | }, | |
309 | }, { | |
310 | # without lang code | |
311 | 'url': 'https://www.funimation.com/shows/ouran-high-school-host-club/', | |
312 | 'info_dict': { | |
313 | 'id': 39643, | |
314 | 'title': 'Ouran High School Host Club' | |
315 | }, | |
316 | 'playlist_count': 26, | |
317 | 'params': { | |
318 | 'skip_download': True, | |
319 | }, | |
320 | }] | |
321 | ||
ad226b1d | 322 | def _real_initialize(self): |
8cd69fc4 J |
323 | if not self._REGION: |
324 | FunimationBaseIE._REGION = self._get_region() | |
ad226b1d | 325 | |
125728b0 | 326 | def _real_extract(self, url): |
5ad28e7f | 327 | base_url, locale, display_id = self._match_valid_url(url).groups() |
125728b0 M |
328 | |
329 | show_info = self._download_json( | |
ad226b1d | 330 | 'https://title-api.prd.funimationsvc.com/v2/shows/%s?region=%s&deviceType=web&locale=%s' |
8cd69fc4 J |
331 | % (display_id, self._REGION, locale or 'en'), display_id) |
332 | items_info = self._download_json( | |
125728b0 | 333 | 'https://prod-api-funimationnow.dadcdigital.com/api/funimation/episodes/?limit=99999&title_id=%s' |
8cd69fc4 J |
334 | % show_info.get('id'), display_id) |
335 | ||
e6f868a6 | 336 | vod_items = traverse_obj(items_info, ('items', ..., lambda k, _: re.match(r'(?i)mostRecent[AS]vod', k), 'item')) |
125728b0 M |
337 | |
338 | return { | |
339 | '_type': 'playlist', | |
340 | 'id': show_info['id'], | |
341 | 'title': show_info['name'], | |
8cd69fc4 | 342 | 'entries': orderedSet( |
125728b0 | 343 | self.url_result( |
3acf6d38 | 344 | '%s/%s' % (base_url, vod_item.get('episodeSlug')), FunimationPageIE.ie_key(), |
125728b0 | 345 | vod_item.get('episodeId'), vod_item.get('episodeName')) |
8cd69fc4 | 346 | for vod_item in sorted(vod_items, key=lambda x: x.get('episodeOrder', -1))), |
125728b0 | 347 | } |