2 from __future__
import unicode_literals
7 from .common
import InfoExtractor
8 from ..compat
import compat_HTTPError
22 class FunimationPageIE(InfoExtractor
):
23 IE_NAME
= 'funimation:page'
24 _VALID_URL
= r
'(?P<origin>https?://(?:www\.)?funimation(?:\.com|now\.uk))/(?P<lang>[^/]+/)?(?P<path>shows/(?P<id>[^/]+/[^/?#&]+).*$)'
27 'url': 'https://www.funimation.com/shows/attack-on-titan-junior-high/broadcast-dub-preview/',
31 'title': 'Broadcast Dub Preview',
32 # Other metadata is tested in FunimationIE
35 'skip_download': 'm3u8',
37 'add_ie': ['Funimation'],
40 'url': 'https://www.funimation.com/shows/hacksign/role-play/',
41 'only_matching': True,
44 'url': 'https://www.funimation.com/en/shows/hacksign/role-play/',
45 'only_matching': True,
47 'url': 'https://www.funimationnow.uk/shows/puzzle-dragons-x/drop-impact/simulcast/',
48 'only_matching': True,
51 def _real_extract(self
, url
):
52 mobj
= self
._match
_valid
_url
(url
)
53 display_id
= mobj
.group('id').replace('/', '_')
54 if not mobj
.group('lang'):
55 url
= '%s/en/%s' % (mobj
.group('origin'), mobj
.group('path'))
57 webpage
= self
._download
_webpage
(url
, display_id
)
58 title_data
= self
._parse
_json
(self
._search
_regex
(
59 r
'TITLE_DATA\s*=\s*({[^}]+})',
60 webpage
, 'title data', default
=''),
61 display_id
, js_to_json
, fatal
=False) or {}
65 or self
._search
_regex
(
66 (r
"KANE_customdimensions.videoID\s*=\s*'(\d+)';", r
'<iframe[^>]+src="/player/(\d+)'),
67 webpage
, 'video_id', default
=None)
68 or self
._search
_regex
(
70 self
._html
_search
_meta
(['al:web:url', 'og:video:url', 'og:video:secure_url'], webpage
, fatal
=True),
72 return self
.url_result(f
'https://www.funimation.com/player/{video_id}', FunimationIE
.ie_key(), video_id
)
75 class FunimationIE(InfoExtractor
):
76 _VALID_URL
= r
'https?://(?:www\.)?funimation\.com/player/(?P<id>\d+)'
78 _NETRC_MACHINE
= 'funimation'
82 'url': 'https://www.funimation.com/player/210051',
85 'display_id': 'broadcast-dub-preview',
87 'title': 'Broadcast Dub Preview',
88 'thumbnail': r
're:https?://.*\.(?:jpg|png)',
89 'episode': 'Broadcast Dub Preview',
90 'episode_id': '210050',
92 'season_id': '166038',
94 'series': 'Attack on Titan: Junior High',
99 'skip_download': 'm3u8',
102 'note': 'player_id should be extracted with the relevent compat-opt',
103 'url': 'https://www.funimation.com/player/210051',
106 'display_id': 'broadcast-dub-preview',
108 'title': 'Broadcast Dub Preview',
109 'thumbnail': r
're:https?://.*\.(?:jpg|png)',
110 'episode': 'Broadcast Dub Preview',
111 'episode_id': '210050',
113 'season_id': '166038',
115 'series': 'Attack on Titan: Junior High',
120 'skip_download': 'm3u8',
121 'compat_opts': ['seperate-video-versions'],
126 username
, password
= self
._get
_login
_info
()
130 data
= self
._download
_json
(
131 'https://prod-api-funimationnow.dadcdigital.com/api/auth/login/',
132 None, 'Logging in', data
=urlencode_postdata({
133 'username': username
,
134 'password': password
,
136 self
._TOKEN
= data
['token']
137 except ExtractorError
as e
:
138 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
== 401:
139 error
= self
._parse
_json
(e
.cause
.read().decode(), None)['error']
140 raise ExtractorError(error
, expected
=True)
143 def _real_initialize(self
):
147 def _get_experiences(episode
):
148 for lang
, lang_data
in episode
.get('languages', {}).items():
149 for video_data
in lang_data
.values():
150 for version
, f
in video_data
.items():
151 yield lang
, version
.title(), f
153 def _get_episode(self
, webpage
, experience_id
=None, episode_id
=None, fatal
=True):
154 ''' Extract the episode, season and show objects given either episode/experience id '''
155 show
= self
._parse
_json
(
157 r
'show\s*=\s*({.+?})\s*;', webpage
, 'show data', fatal
=fatal
),
158 experience_id
, transform_source
=js_to_json
, fatal
=fatal
) or []
159 for season
in show
.get('seasons', []):
160 for episode
in season
.get('episodes', []):
161 if episode_id
is not None:
162 if str(episode
.get('episodePk')) == episode_id
:
163 return episode
, season
, show
165 for _
, _
, f
in self
._get
_experiences
(episode
):
166 if f
.get('experienceId') == experience_id
:
167 return episode
, season
, show
169 raise ExtractorError('Unable to find episode information')
171 self
.report_warning('Unable to find episode information')
174 def _real_extract(self
, url
):
175 initial_experience_id
= self
._match
_id
(url
)
176 webpage
= self
._download
_webpage
(
177 url
, initial_experience_id
, note
=f
'Downloading player webpage for {initial_experience_id}')
178 episode
, season
, show
= self
._get
_episode
(webpage
, experience_id
=int(initial_experience_id
))
179 episode_id
= str(episode
['episodePk'])
180 display_id
= episode
.get('slug') or episode_id
182 formats
, subtitles
, thumbnails
, duration
= [], {}, [], 0
183 requested_languages
, requested_versions
= self
._configuration
_arg
('language'), self
._configuration
_arg
('version')
184 language_preference
= qualities((requested_languages
or [''])[::-1])
185 source_preference
= qualities((requested_versions
or ['uncut', 'simulcast'])[::-1])
186 only_initial_experience
= 'seperate-video-versions' in self
.get_param('compat_opts', [])
188 for lang
, version
, fmt
in self
._get
_experiences
(episode
):
189 experience_id
= str(fmt
['experienceId'])
190 if (only_initial_experience
and experience_id
!= initial_experience_id
191 or requested_languages
and lang
.lower() not in requested_languages
192 or requested_versions
and version
.lower() not in requested_versions
):
194 thumbnails
.append({'url': fmt.get('poster')}
)
195 duration
= max(duration
, fmt
.get('duration', 0))
196 format_name
= '%s %s (%s)' % (version
, lang
, experience_id
)
197 self
.extract_subtitles(
198 subtitles
, experience_id
, display_id
=display_id
, format_name
=format_name
,
199 episode
=episode
if experience_id
== initial_experience_id
else episode_id
)
203 headers
['Authorization'] = 'Token %s' % self
._TOKEN
204 page
= self
._download
_json
(
205 'https://www.funimation.com/api/showexperience/%s/' % experience_id
,
206 display_id
, headers
=headers
, expected_status
=403, query
={
207 'pinst_id': ''.join([random
.choice(string
.digits
+ string
.ascii_letters
) for _
in range(8)]),
208 }, note
=f
'Downloading {format_name} JSON')
209 sources
= page
.get('items') or []
211 error
= try_get(page
, lambda x
: x
['errors'][0], dict)
213 self
.report_warning('%s said: Error %s - %s' % (
214 self
.IE_NAME
, error
.get('code'), error
.get('detail') or error
.get('title')))
216 self
.report_warning('No sources found for format')
219 for source
in sources
:
220 source_url
= source
.get('src')
221 source_type
= source
.get('videoType') or determine_ext(source_url
)
222 if source_type
== 'm3u8':
223 current_formats
.extend(self
._extract
_m
3u8_formats
(
224 source_url
, display_id
, 'mp4', m3u8_id
='%s-%s' % (experience_id
, 'hls'), fatal
=False,
225 note
=f
'Downloading {format_name} m3u8 information'))
227 current_formats
.append({
228 'format_id': '%s-%s' % (experience_id
, source_type
),
231 for f
in current_formats
:
232 # TODO: Convert language to code
235 'format_note': version
,
236 'source_preference': source_preference(version
.lower()),
237 'language_preference': language_preference(lang
.lower()),
239 formats
.extend(current_formats
)
240 self
._remove
_duplicate
_formats
(formats
)
241 self
._sort
_formats
(formats
, ('lang', 'source'))
244 'id': initial_experience_id
if only_initial_experience
else episode_id
,
245 'display_id': display_id
,
246 'duration': duration
,
247 'title': episode
['episodeTitle'],
248 'description': episode
.get('episodeSummary'),
249 'episode': episode
.get('episodeTitle'),
250 'episode_number': int_or_none(episode
.get('episodeId')),
251 'episode_id': episode_id
,
252 'season': season
.get('seasonTitle'),
253 'season_number': int_or_none(season
.get('seasonId')),
254 'season_id': str_or_none(season
.get('seasonPk')),
255 'series': show
.get('showTitle'),
257 'thumbnails': thumbnails
,
258 'subtitles': subtitles
,
261 def _get_subtitles(self
, subtitles
, experience_id
, episode
, display_id
, format_name
):
262 if isinstance(episode
, str):
263 webpage
= self
._download
_webpage
(
264 f
'https://www.funimation.com/player/{experience_id}', display_id
,
265 fatal
=False, note
=f
'Downloading player webpage for {format_name}')
266 episode
, _
, _
= self
._get
_episode
(webpage
, episode_id
=episode
, fatal
=False)
268 for _
, version
, f
in self
._get
_experiences
(episode
):
269 for source
in f
.get('sources'):
270 for text_track
in source
.get('textTracks'):
271 if not text_track
.get('src'):
273 sub_type
= text_track
.get('type').upper()
274 sub_type
= sub_type
if sub_type
!= 'FULL' else None
276 'url': text_track
['src'],
277 'name': ' '.join(filter(None, (version
, text_track
.get('label'), sub_type
)))
279 lang
= '_'.join(filter(None, (
280 text_track
.get('language', 'und'), version
if version
!= 'Simulcast' else None, sub_type
)))
281 if current_sub
not in subtitles
.get(lang
, []):
282 subtitles
.setdefault(lang
, []).append(current_sub
)
286 class FunimationShowIE(FunimationIE
):
287 IE_NAME
= 'funimation:show'
288 _VALID_URL
= r
'(?P<url>https?://(?:www\.)?funimation(?:\.com|now\.uk)/(?P<locale>[^/]+)?/?shows/(?P<id>[^/?#&]+))/?(?:[?#]|$)'
291 'url': 'https://www.funimation.com/en/shows/sk8-the-infinity',
294 'title': 'SK8 the Infinity'
296 'playlist_count': 13,
298 'skip_download': True,
302 'url': 'https://www.funimation.com/shows/ouran-high-school-host-club/',
305 'title': 'Ouran High School Host Club'
307 'playlist_count': 26,
309 'skip_download': True,
313 def _real_initialize(self
):
314 region
= self
._get
_cookies
('https://www.funimation.com').get('region')
315 self
._region
= region
.value
if region
else try_get(
317 'https://geo-service.prd.funimationsvc.com/geo/v1/region/check', None, fatal
=False,
318 note
='Checking geo-location', errnote
='Unable to fetch geo-location information'),
319 lambda x
: x
['region']) or 'US'
321 def _real_extract(self
, url
):
322 base_url
, locale
, display_id
= self
._match
_valid
_url
(url
).groups()
324 show_info
= self
._download
_json
(
325 'https://title-api.prd.funimationsvc.com/v2/shows/%s?region=%s&deviceType=web&locale=%s'
326 % (display_id
, self
._region
, locale
or 'en'), display_id
)
327 items
= self
._download
_json
(
328 'https://prod-api-funimationnow.dadcdigital.com/api/funimation/episodes/?limit=99999&title_id=%s'
329 % show_info
.get('id'), display_id
).get('items')
330 vod_items
= map(lambda k
: dict_get(k
, ('mostRecentSvod', 'mostRecentAvod')).get('item'), items
)
334 'id': show_info
['id'],
335 'title': show_info
['name'],
338 '%s/%s' % (base_url
, vod_item
.get('episodeSlug')), FunimationPageIE
.ie_key(),
339 vod_item
.get('episodeId'), vod_item
.get('episodeName'))
340 for vod_item
in sorted(vod_items
, key
=lambda x
: x
.get('episodeOrder'))],