2 from __future__ import unicode_literals
6 from .common import InfoExtractor
17 class FunimationIE(InfoExtractor):
18 _VALID_URL = r'https?://(?:www\.)?funimation\.com/shows/[^/]+/videos/official/(?P<id>[^/?#&]+)'
21 'url': 'http://www.funimation.com/shows/air/videos/official/breeze',
24 'display_id': 'breeze',
26 'title': 'Air - 1 - Breeze',
27 'description': 'md5:1769f43cd5fc130ace8fd87232207892',
28 'thumbnail': 're:https?://.*\.jpg',
31 'url': 'http://www.funimation.com/shows/hacksign/videos/official/role-play',
34 'display_id': 'role-play',
36 'title': '.hack//SIGN - 1 - Role Play',
37 'description': 'md5:b602bdc15eef4c9bbb201bb6e6a4a2dd',
38 'thumbnail': 're:https?://.*\.jpg',
43 (username, password) = self._get_login_info()
46 data = urlencode_postdata(encode_dict({
47 'email_field': username,
48 'password_field': password,
50 login_request = sanitized_Request('http://www.funimation.com/login', data, headers={
51 'User-Agent': 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0',
52 'Content-Type': 'application/x-www-form-urlencoded'
54 login = self._download_webpage(
55 login_request, None, 'Logging in as %s' % username)
56 if re.search(r'<meta property="og:url" content="http://www.funimation.com/login"/>', login) is not None:
57 raise ExtractorError('Unable to login, wrong username or password.', expected=True)
59 def _real_initialize(self):
62 def _real_extract(self, url):
63 display_id = self._match_id(url)
69 'ERROR_MATURE_CONTENT_LOGGED_IN': 'matureContentLoggedIn',
70 'ERROR_MATURE_CONTENT_LOGGED_OUT': 'matureContentLoggedOut',
71 'ERROR_SUBSCRIPTION_LOGGED_OUT': 'subscriptionLoggedOut',
72 'ERROR_VIDEO_EXPIRED': 'videoExpired',
73 'ERROR_TERRITORY_UNAVAILABLE': 'territoryUnavailable',
74 'SVODBASIC_SUBSCRIPTION_IN_PLAYER': 'basicSubscription',
75 'SVODNON_SUBSCRIPTION_IN_PLAYER': 'nonSubscription',
76 'ERROR_PLAYER_NOT_RESPONDING': 'playerNotResponding',
77 'ERROR_UNABLE_TO_CONNECT_TO_CDN': 'unableToConnectToCDN',
78 'ERROR_STREAM_NOT_FOUND': 'streamNotFound',
82 # PC UA is served with m3u8 that provides some bonus lower quality formats
83 ('pc', 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0'),
84 # Mobile UA allows to extract direct links and also does not fail when
85 # PC UA fails with hulu error (e.g.
86 # http://www.funimation.com/shows/hacksign/videos/official/role-play)
87 ('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'),
90 for kind, user_agent in USER_AGENTS:
91 request = sanitized_Request(url)
92 request.add_header('User-Agent', user_agent)
93 webpage = self._download_webpage(
94 request, display_id, 'Downloading %s webpage' % kind)
96 items = self._parse_json(
98 r'var\s+playersData\s*=\s*(\[.+?\]);\n',
99 webpage, 'players data'),
100 display_id)[0]['playlist'][0]['items']
102 item = next(item for item in items if item.get('itemAK') == display_id)
105 video_error_messages = self._search_regex(
106 r'var\s+videoErrorMessages\s*=\s*({.+?});\n',
107 webpage, 'error messages', default=None)
108 if video_error_messages:
109 error_messages_json = self._parse_json(video_error_messages, display_id, fatal=False)
110 if error_messages_json:
111 for _, error in error_messages_json.items():
112 type_ = error.get('type')
113 description = error.get('description')
114 content = error.get('content')
115 if type_ == 'text' and description and content:
116 error_message = ERRORS_MAP.get(description)
118 error_messages[error_message] = content
120 for video in item.get('videoSet', []):
121 auth_token = video.get('authToken')
124 funimation_id = video.get('FUNImationID') or video.get('videoId')
125 preference = 1 if video.get('languageMode') == 'dub' else 0
126 if not auth_token.startswith('?'):
127 auth_token = '?%s' % auth_token
128 for quality in ('sd', 'hd', 'hd1080'):
129 format_url = video.get('%sUrl' % quality)
132 if not format_url.startswith(('http', '//')):
133 errors.append(format_url)
135 if determine_ext(format_url) == 'm3u8':
136 m3u8_formats = self._extract_m3u8_formats(
137 format_url + auth_token, display_id, 'mp4', entry_protocol='m3u8_native',
138 preference=preference, m3u8_id=funimation_id or 'hls', fatal=False)
140 formats.extend(m3u8_formats)
143 'url': format_url + auth_token,
144 'format_id': funimation_id,
145 'preference': preference,
147 mobj = re.search(r'(?P<height>\d+)-(?P<tbr>\d+)[Kk]', format_url)
150 'height': int(mobj.group('height')),
151 'tbr': int(mobj.group('tbr')),
155 if not formats and errors:
156 raise ExtractorError(
157 '%s returned error: %s'
158 % (self.IE_NAME, clean_html(error_messages.get(errors[0], errors[0]))),
161 self._sort_formats(formats)
163 title = item['title']
164 artist = item.get('artist')
166 title = '%s - %s' % (artist, title)
167 description = self._og_search_description(webpage) or item.get('description')
168 thumbnail = self._og_search_thumbnail(webpage) or item.get('posterUrl')
169 video_id = item.get('itemId') or display_id
173 'display_id': display_id,
175 'description': description,
176 'thumbnail': thumbnail,