2 from __future__
import unicode_literals
8 from .common
import InfoExtractor
9 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
= re
.match(self
._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 only_initial_experience
= 'seperate-video-versions' in self
.get_param('compat_opts', [])
186 for lang
, version
, fmt
in self
._get
_experiences
(episode
):
187 experience_id
= str(fmt
['experienceId'])
188 if (only_initial_experience
and experience_id
!= initial_experience_id
189 or requested_languages
and lang
not in requested_languages
190 or requested_versions
and version
not in requested_versions
):
192 thumbnails
.append({'url': fmt.get('poster')}
)
193 duration
= max(duration
, fmt
.get('duration', 0))
194 format_name
= '%s %s (%s)' % (version
, lang
, experience_id
)
195 self
.extract_subtitles(
196 subtitles
, experience_id
, display_id
=display_id
, format_name
=format_name
,
197 episode
=episode
if experience_id
== initial_experience_id
else episode_id
)
201 headers
['Authorization'] = 'Token %s' % self
._TOKEN
202 page
= self
._download
_json
(
203 'https://www.funimation.com/api/showexperience/%s/' % experience_id
,
204 display_id
, headers
=headers
, expected_status
=403, query
={
205 'pinst_id': ''.join([random
.choice(string
.digits
+ string
.ascii_letters
) for _
in range(8)]),
206 }, note
=f
'Downloading {format_name} JSON')
207 sources
= page
.get('items') or []
209 error
= try_get(page
, lambda x
: x
['errors'][0], dict)
211 self
.report_warning('%s said: Error %s - %s' % (
212 self
.IE_NAME
, error
.get('code'), error
.get('detail') or error
.get('title')))
214 self
.report_warning('No sources found for format')
217 for source
in sources
:
218 source_url
= source
.get('src')
219 source_type
= source
.get('videoType') or determine_ext(source_url
)
220 if source_type
== 'm3u8':
221 current_formats
.extend(self
._extract
_m
3u8_formats
(
222 source_url
, display_id
, 'mp4', m3u8_id
='%s-%s' % (experience_id
, 'hls'), fatal
=False,
223 note
=f
'Downloading {format_name} m3u8 information'))
225 current_formats
.append({
226 'format_id': '%s-%s' % (experience_id
, source_type
),
229 for f
in current_formats
:
230 # TODO: Convert language to code
231 f
.update({'language': lang, 'format_note': version}
)
232 formats
.extend(current_formats
)
233 self
._remove
_duplicate
_formats
(formats
)
234 self
._sort
_formats
(formats
)
237 'id': initial_experience_id
if only_initial_experience
else episode_id
,
238 'display_id': display_id
,
239 'duration': duration
,
240 'title': episode
['episodeTitle'],
241 'description': episode
.get('episodeSummary'),
242 'episode': episode
.get('episodeTitle'),
243 'episode_number': int_or_none(episode
.get('episodeId')),
244 'episode_id': episode_id
,
245 'season': season
.get('seasonTitle'),
246 'season_number': int_or_none(season
.get('seasonId')),
247 'season_id': str_or_none(season
.get('seasonPk')),
248 'series': show
.get('showTitle'),
250 'thumbnails': thumbnails
,
251 'subtitles': subtitles
,
254 def _get_subtitles(self
, subtitles
, experience_id
, episode
, display_id
, format_name
):
255 if isinstance(episode
, str):
256 webpage
= self
._download
_webpage
(
257 f
'https://www.funimation.com/player/{experience_id}', display_id
,
258 fatal
=False, note
=f
'Downloading player webpage for {format_name}')
259 episode
, _
, _
= self
._get
_episode
(webpage
, episode_id
=episode
, fatal
=False)
261 for _
, version
, f
in self
._get
_experiences
(episode
):
262 for source
in f
.get('sources'):
263 for text_track
in source
.get('textTracks'):
264 if not text_track
.get('src'):
266 sub_type
= text_track
.get('type').upper()
267 sub_type
= sub_type
if sub_type
!= 'FULL' else None
269 'url': text_track
['src'],
270 'name': ' '.join(filter(None, (version
, text_track
.get('label'), sub_type
)))
272 lang
= '_'.join(filter(None, (
273 text_track
.get('language', 'und'), version
if version
!= 'Simulcast' else None, sub_type
)))
274 if current_sub
not in subtitles
.get(lang
, []):
275 subtitles
.setdefault(lang
, []).append(current_sub
)
279 class FunimationShowIE(FunimationIE
):
280 IE_NAME
= 'funimation:show'
281 _VALID_URL
= r
'(?P<url>https?://(?:www\.)?funimation(?:\.com|now\.uk)/(?P<locale>[^/]+)?/?shows/(?P<id>[^/?#&]+))/?(?:[?#]|$)'
284 'url': 'https://www.funimation.com/en/shows/sk8-the-infinity',
287 'title': 'SK8 the Infinity'
289 'playlist_count': 13,
291 'skip_download': True,
295 'url': 'https://www.funimation.com/shows/ouran-high-school-host-club/',
298 'title': 'Ouran High School Host Club'
300 'playlist_count': 26,
302 'skip_download': True,
306 def _real_extract(self
, url
):
307 base_url
, locale
, display_id
= re
.match(self
._VALID
_URL
, url
).groups()
309 show_info
= self
._download
_json
(
310 'https://title-api.prd.funimationsvc.com/v2/shows/%s?region=US&deviceType=web&locale=%s'
311 % (display_id
, locale
or 'en'), display_id
)
312 items
= self
._download
_json
(
313 'https://prod-api-funimationnow.dadcdigital.com/api/funimation/episodes/?limit=99999&title_id=%s'
314 % show_info
.get('id'), display_id
).get('items')
315 vod_items
= map(lambda k
: dict_get(k
, ('mostRecentSvod', 'mostRecentAvod')).get('item'), items
)
319 'id': show_info
['id'],
320 'title': show_info
['name'],
323 '%s/%s' % (base_url
, vod_item
.get('episodeSlug')), FunimationPageIE
.ie_key(),
324 vod_item
.get('episodeId'), vod_item
.get('episodeName'))
325 for vod_item
in sorted(vod_items
, key
=lambda x
: x
.get('episodeOrder'))],