- USER_AGENTS = (
- # PC UA is served with m3u8 that provides some bonus lower quality formats
- ('pc', 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0'),
- # Mobile UA allows to extract direct links and also does not fail when
- # PC UA fails with hulu error (e.g.
- # http://www.funimation.com/shows/hacksign/videos/official/role-play)
- ('mobile', 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36'),
- )
-
- user_agent = self._extract_cloudflare_session_ua(url)
- if user_agent:
- USER_AGENTS = ((None, user_agent),)
-
- for kind, user_agent in USER_AGENTS:
- request = sanitized_Request(url)
- request.add_header('User-Agent', user_agent)
- webpage = self._download_webpage(
- request, display_id,
- 'Downloading %s webpage' % kind if kind else 'Downloading webpage')
-
- playlist = self._parse_json(
- self._search_regex(
- r'var\s+playersData\s*=\s*(\[.+?\]);\n',
- webpage, 'players data'),
- display_id)[0]['playlist']
-
- items = next(item['items'] for item in playlist if item.get('items'))
- item = next(item for item in items if item.get('itemAK') == display_id)
-
- error_messages = {}
- video_error_messages = self._search_regex(
- r'var\s+videoErrorMessages\s*=\s*({.+?});\n',
- webpage, 'error messages', default=None)
- if video_error_messages:
- error_messages_json = self._parse_json(video_error_messages, display_id, fatal=False)
- if error_messages_json:
- for _, error in error_messages_json.items():
- type_ = error.get('type')
- description = error.get('description')
- content = error.get('content')
- if type_ == 'text' and description and content:
- error_message = ERRORS_MAP.get(description)
- if error_message:
- error_messages[error_message] = content
-
- for video in item.get('videoSet', []):
- auth_token = video.get('authToken')
- if not auth_token:
- continue
- funimation_id = video.get('FUNImationID') or video.get('videoId')
- preference = 1 if video.get('languageMode') == 'dub' else 0
- if not auth_token.startswith('?'):
- auth_token = '?%s' % auth_token
- for quality, height in (('sd', 480), ('hd', 720), ('hd1080', 1080)):
- format_url = video.get('%sUrl' % quality)
- if not format_url:
- continue
- if not format_url.startswith(('http', '//')):
- errors.append(format_url)
- continue
- if determine_ext(format_url) == 'm3u8':
- formats.extend(self._extract_m3u8_formats(
- format_url + auth_token, display_id, 'mp4', entry_protocol='m3u8_native',
- preference=preference, m3u8_id='%s-hls' % funimation_id, fatal=False))
- else:
- tbr = int_or_none(self._search_regex(
- r'-(\d+)[Kk]', format_url, 'tbr', default=None))
- formats.append({
- 'url': format_url + auth_token,
- 'format_id': '%s-http-%dp' % (funimation_id, height),
- 'height': height,
- 'tbr': tbr,
- 'preference': preference,
- })
-
- if not formats and errors:
- raise ExtractorError(
- '%s returned error: %s'
- % (self.IE_NAME, clean_html(error_messages.get(errors[0], errors[0]))),
- expected=True)
+ try:
+ headers = {}
+ if self._TOKEN:
+ headers['Authorization'] = 'Token %s' % self._TOKEN
+ sources = self._download_json(
+ 'https://www.funimation.com/api/showexperience/%s/' % video_id,
+ video_id, headers=headers, query={
+ 'pinst_id': ''.join([random.choice(string.digits + string.ascii_letters) for _ in range(8)]),
+ })['items']
+ except ExtractorError as e:
+ if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
+ error = self._parse_json(e.cause.read(), video_id)['errors'][0]
+ raise ExtractorError('%s said: %s' % (
+ self.IE_NAME, error.get('detail') or error.get('title')), expected=True)
+ raise