# coding: utf-8
from __future__ import unicode_literals
+
+import random
+import string
+
from .common import InfoExtractor
from ..compat import compat_HTTPError
from ..utils import (
- encode_dict,
- sanitized_Request,
+ determine_ext,
+ int_or_none,
+ js_to_json,
ExtractorError,
urlencode_postdata
)
-import re
+
class FunimationIE(InfoExtractor):
- _VALID_URL = r'https?://(?:www\.)?funimation\.com/shows/.+[^ ]/videos/official/(?P<id>[^?]+)'
+ _VALID_URL = r'https?://(?:www\.)?funimation(?:\.com|now\.uk)/shows/[^/]+/(?P<id>[^/?#&]+)'
+
+ _NETRC_MACHINE = 'funimation'
+ _TOKEN = None
- _TEST = {
- 'url': 'http://www.funimation.com/shows/air/videos/official/breeze',
+ _TESTS = [{
+ 'url': 'https://www.funimation.com/shows/hacksign/role-play/',
'info_dict': {
- 'id': 'AIRENG0001',
- 'title': 'Air - 1 - Breeze ',
+ 'id': '91144',
+ 'display_id': 'role-play',
'ext': 'mp4',
- 'thumbnail': 'http://www.funimation.com/admin/uploads/default/recap_thumbnails/7555590/home_spotlight/AIR0001.jpg',
- 'description': 'Travelling puppeteer Yukito arrives in a small town where he hopes to earn money through the magic of his puppets. When a young girl named Misuzu lures him to her home with the promise of food, his life changes forever. ',
- }
- }
+ 'title': '.hack//SIGN - Role Play',
+ 'description': 'md5:b602bdc15eef4c9bbb201bb6e6a4a2dd',
+ 'thumbnail': r're:https?://.*\.jpg',
+ },
+ 'params': {
+ # m3u8 download
+ 'skip_download': True,
+ },
+ }, {
+ 'url': 'https://www.funimation.com/shows/attack-on-titan-junior-high/broadcast-dub-preview/',
+ 'info_dict': {
+ 'id': '210051',
+ 'display_id': 'broadcast-dub-preview',
+ 'ext': 'mp4',
+ 'title': 'Attack on Titan: Junior High - Broadcast Dub Preview',
+ 'thumbnail': r're:https?://.*\.(?:jpg|png)',
+ },
+ 'params': {
+ # m3u8 download
+ 'skip_download': True,
+ },
+ }, {
+ 'url': 'https://www.funimationnow.uk/shows/puzzle-dragons-x/drop-impact/simulcast/',
+ 'only_matching': True,
+ }]
def _login(self):
- (username, password) = self._get_login_info()
+ username, password = self._get_login_info()
if username is None:
return
- login_url = 'http://www.funimation.com/login'
- data = urlencode_postdata(encode_dict({
- 'loginForm2': 'loginform',
- 'email_field': username,
- 'password_field': password,
- }))
- login_request = sanitized_Request(login_url, data)
- login_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
try:
- login = self._download_webpage(login_request, login_url)
+ data = self._download_json(
+ 'https://prod-api-funimationnow.dadcdigital.com/api/auth/login/',
+ None, 'Logging in', data=urlencode_postdata({
+ 'username': username,
+ 'password': password,
+ }))
+ self._TOKEN = data['token']
except ExtractorError as e:
- if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
- raise ExtractorError('Funimation is not available in your region.', expected=True)
+ if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
+ error = self._parse_json(e.cause.read().decode(), None)['error']
+ raise ExtractorError(error, expected=True)
raise
- if re.search(r'<meta property="og:url" content="http://www.funimation.com/login"/>', login) is not None:
- raise ExtractorError('Unable to login, wrong username or password.', expected=True)
def _real_initialize(self):
self._login()
def _real_extract(self, url):
- mobj = re.match(self._VALID_URL, url)
- video_id = mobj.group('id')
+ display_id = self._match_id(url)
+ webpage = self._download_webpage(url, display_id)
+
+ def _search_kane(name):
+ return self._search_regex(
+ r"KANE_customdimensions\.%s\s*=\s*'([^']+)';" % name,
+ webpage, name, default=None)
+
+ title_data = self._parse_json(self._search_regex(
+ r'TITLE_DATA\s*=\s*({[^}]+})',
+ webpage, 'title data', default=''),
+ display_id, js_to_json, fatal=False) or {}
+
+ video_id = title_data.get('id') or self._search_regex([
+ r"KANE_customdimensions.videoID\s*=\s*'(\d+)';",
+ r'<iframe[^>]+src="/player/(\d+)',
+ ], webpage, 'video_id', default=None)
+ if not video_id:
+ player_url = self._html_search_meta([
+ 'al:web:url',
+ 'og:video:url',
+ 'og:video:secure_url',
+ ], webpage, fatal=True)
+ video_id = self._search_regex(r'/player/(\d+)', player_url, 'video id')
+
+ title = episode = title_data.get('title') or _search_kane('videoTitle') or self._og_search_title(webpage)
+ series = _search_kane('showName')
+ if series:
+ title = '%s - %s' % (series, title)
+ description = self._html_search_meta(['description', 'og:description'], webpage, fatal=True)
+
try:
- webpage = self._download_webpage(url, video_id)
+ 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:
- raise ExtractorError('Funimation is not available in your region.', expected=True)
+ 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
- if re.search(r'"sdUrl":"http', webpage) is None:
- raise ExtractorError('You are not logged-in or the stream requires subscription.', expected=True)
-
- m3u8 = self._search_regex(r'".+Url":"(.+?m3u8)"', webpage, 'm3u8') + self._search_regex(r'"authToken":"(.+?)"', webpage, 'm3u8')
- formats = self._extract_m3u8_formats(m3u8.replace('\\', ''), video_id, ext='mp4', entry_protocol='m3u8_native')
- video_show = self._search_regex(r'"artist":"(.+?)"', webpage, 'video_show')
- video_track = self._search_regex(r'"videoNumber":"(\d+).0"', webpage, 'video_track')
- video_title = self._search_regex(r'"title":"({0}.+?)"'.format(video_track), webpage, 'video_title')
- video_id = self._search_regex(r'"FUNImationID":"(.+?)"', webpage, 'video_id')
+ formats = []
+ for source in sources:
+ source_url = source.get('src')
+ if not source_url:
+ continue
+ source_type = source.get('videoType') or determine_ext(source_url)
+ if source_type == 'm3u8':
+ formats.extend(self._extract_m3u8_formats(
+ source_url, video_id, 'mp4',
+ m3u8_id='hls', fatal=False))
+ else:
+ formats.append({
+ 'format_id': source_type,
+ 'url': source_url,
+ })
+ self._sort_formats(formats)
return {
'id': video_id,
- 'title': video_show + ' - ' + video_title + ' ',
- 'formats': formats,
+ 'display_id': display_id,
+ 'title': title,
+ 'description': description,
'thumbnail': self._og_search_thumbnail(webpage),
- 'description': self._og_search_description(webpage)
+ 'series': series,
+ 'season_number': int_or_none(title_data.get('seasonNum') or _search_kane('season')),
+ 'episode_number': int_or_none(title_data.get('episodeNum')),
+ 'episode': episode,
+ 'season_id': title_data.get('seriesId'),
+ 'formats': formats,
}