2 from __future__ import unicode_literals
4 from .common import InfoExtractor
7 compat_urllib_parse_unquote_plus,
19 class FunimationIE(InfoExtractor):
20 _VALID_URL = r'https?://(?:www\.)?funimation(?:\.com|now\.uk)/shows/[^/]+/(?P<id>[^/?#&]+)'
22 _NETRC_MACHINE = 'funimation'
25 'url': 'https://www.funimation.com/shows/hacksign/role-play/',
28 'display_id': 'role-play',
30 'title': '.hack//SIGN - Role Play',
31 'description': 'md5:b602bdc15eef4c9bbb201bb6e6a4a2dd',
32 'thumbnail': r're:https?://.*\.jpg',
36 'skip_download': True,
39 'url': 'https://www.funimation.com/shows/attack-on-titan-junior-high/broadcast-dub-preview/',
42 'display_id': 'broadcast-dub-preview',
44 'title': 'Attack on Titan: Junior High - Broadcast Dub Preview',
45 'description': 'md5:f8ec49c0aff702a7832cd81b8a44f803',
46 'thumbnail': r're:https?://.*\.(?:jpg|png)',
48 'skip': 'Access without user interaction is forbidden by CloudFlare',
50 'url': 'https://www.funimationnow.uk/shows/puzzle-dragons-x/drop-impact/simulcast/',
51 'only_matching': True,
54 _LOGIN_URL = 'http://www.funimation.com/login'
56 def _extract_cloudflare_session_ua(self, url):
57 ci_session_cookie = self._get_cookies(url).get('ci_session')
59 ci_session = compat_urllib_parse_unquote_plus(ci_session_cookie.value)
60 # ci_session is a string serialized by PHP function serialize()
61 # This case is simple enough to use regular expressions only
62 return self._search_regex(
63 r'"user_agent";s:\d+:"([^"]+)"', ci_session, 'user agent',
67 (username, password) = self._get_login_info()
70 data = urlencode_postdata({
71 'email_field': username,
72 'password_field': password,
74 user_agent = self._extract_cloudflare_session_ua(self._LOGIN_URL)
76 user_agent = 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0'
77 login_request = sanitized_Request(self._LOGIN_URL, data, headers={
78 'User-Agent': user_agent,
79 'Content-Type': 'application/x-www-form-urlencoded'
81 login_page = self._download_webpage(
82 login_request, None, 'Logging in as %s' % username)
83 if any(p in login_page for p in ('funimation.com/logout', '>Log Out<')):
85 error = self._html_search_regex(
86 r'(?s)<div[^>]+id=["\']errorMessages["\'][^>]*>(.+?)</div>',
87 login_page, 'error messages', default=None)
89 raise ExtractorError('Unable to login: %s' % error, expected=True)
90 raise ExtractorError('Unable to log in')
92 def _real_initialize(self):
95 def _real_extract(self, url):
96 display_id = self._match_id(url)
97 webpage = self._download_webpage(url, display_id)
99 def _search_kane(name):
100 return self._search_regex(
101 r"KANE_customdimensions\.%s\s*=\s*'([^']+)';" % name,
102 webpage, name, default=None)
104 title_data = self._parse_json(self._search_regex(
105 r'TITLE_DATA\s*=\s*({[^}]+})',
106 webpage, 'title data', default=''),
107 display_id, js_to_json, fatal=False) or {}
109 video_id = title_data.get('id') or self._search_regex([
110 r"KANE_customdimensions.videoID\s*=\s*'(\d+)';",
111 r'<iframe[^>]+src="/player/(\d+)"',
112 ], webpage, 'video_id', default=None)
114 player_url = self._html_search_meta([
117 'og:video:secure_url',
118 ], webpage, fatal=True)
119 video_id = self._search_regex(r'/player/(\d+)', player_url, 'video id')
121 title = episode = title_data.get('title') or _search_kane('videoTitle') or self._og_search_title(webpage)
122 series = _search_kane('showName')
124 title = '%s - %s' % (series, title)
125 description = self._html_search_meta(['description', 'og:description'], webpage, fatal=True)
128 sources = self._download_json(
129 'https://prod-api-funimationnow.dadcdigital.com/api/source/catalog/video/%s/signed/' % video_id,
131 except ExtractorError as e:
132 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
133 error = self._parse_json(e.cause.read(), video_id)['errors'][0]
134 raise ExtractorError('%s said: %s' % (
135 self.IE_NAME, error.get('detail') or error.get('title')), expected=True)
139 for source in sources:
140 source_url = source.get('src')
143 source_type = source.get('videoType') or determine_ext(source_url)
144 if source_type == 'm3u8':
145 formats.extend(self._extract_m3u8_formats(
146 source_url, video_id, 'mp4',
147 m3u8_id='hls', fatal=False))
150 'format_id': source_type,
153 self._sort_formats(formats)
157 'display_id': display_id,
159 'description': description,
160 'thumbnail': self._og_search_thumbnail(webpage),
162 'season_number': int_or_none(title_data.get('seasonNum') or _search_kane('season')),
163 'episode_number': int_or_none(title_data.get('episodeNum')),
165 'season_id': title_data.get('seriesId'),