[funimation] fix extraction(closes #10696)(#11773)
[youtube-dl] / youtube_dl / extractor / funimation.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_HTTPError,
7     compat_urllib_parse_unquote_plus,
8 )
9 from ..utils import (
10     determine_ext,
11     int_or_none,
12     js_to_json,
13     sanitized_Request,
14     ExtractorError,
15     urlencode_postdata
16 )
17
18
19 class FunimationIE(InfoExtractor):
20     _VALID_URL = r'https?://(?:www\.)?funimation(?:\.com|now\.uk)/shows/[^/]+/(?P<id>[^/?#&]+)'
21
22     _NETRC_MACHINE = 'funimation'
23
24     _TESTS = [{
25         'url': 'https://www.funimation.com/shows/hacksign/role-play/',
26         'info_dict': {
27             'id': '91144',
28             'display_id': 'role-play',
29             'ext': 'mp4',
30             'title': '.hack//SIGN - Role Play',
31             'description': 'md5:b602bdc15eef4c9bbb201bb6e6a4a2dd',
32             'thumbnail': r're:https?://.*\.jpg',
33         },
34         'params': {
35             # m3u8 download
36             'skip_download': True,
37         },
38     }, {
39         'url': 'https://www.funimation.com/shows/attack-on-titan-junior-high/broadcast-dub-preview/',
40         'info_dict': {
41             'id': '9635',
42             'display_id': 'broadcast-dub-preview',
43             'ext': 'mp4',
44             'title': 'Attack on Titan: Junior High - Broadcast Dub Preview',
45             'description': 'md5:f8ec49c0aff702a7832cd81b8a44f803',
46             'thumbnail': r're:https?://.*\.(?:jpg|png)',
47         },
48         'skip': 'Access without user interaction is forbidden by CloudFlare',
49     }, {
50         'url': 'https://www.funimationnow.uk/shows/puzzle-dragons-x/drop-impact/simulcast/',
51         'only_matching': True,
52     }]
53
54     _LOGIN_URL = 'http://www.funimation.com/login'
55
56     def _extract_cloudflare_session_ua(self, url):
57         ci_session_cookie = self._get_cookies(url).get('ci_session')
58         if ci_session_cookie:
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',
64                 default=None)
65
66     def _login(self):
67         (username, password) = self._get_login_info()
68         if username is None:
69             return
70         data = urlencode_postdata({
71             'email_field': username,
72             'password_field': password,
73         })
74         user_agent = self._extract_cloudflare_session_ua(self._LOGIN_URL)
75         if not user_agent:
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'
80         })
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<')):
84             return
85         error = self._html_search_regex(
86             r'(?s)<div[^>]+id=["\']errorMessages["\'][^>]*>(.+?)</div>',
87             login_page, 'error messages', default=None)
88         if error:
89             raise ExtractorError('Unable to login: %s' % error, expected=True)
90         raise ExtractorError('Unable to log in')
91
92     def _real_initialize(self):
93         self._login()
94
95     def _real_extract(self, url):
96         display_id = self._match_id(url)
97         webpage = self._download_webpage(url, display_id)
98
99         def _search_kane(name):
100             return self._search_regex(
101                 r"KANE_customdimensions\.%s\s*=\s*'([^']+)';" % name,
102                 webpage, name, default=None)
103
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 {}
108
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)
113         if not video_id:
114             player_url = self._html_search_meta([
115                 'al:web:url',
116                 'og:video:url',
117                 'og:video:secure_url',
118             ], webpage, fatal=True)
119             video_id = self._search_regex(r'/player/(\d+)', player_url, 'video id')
120
121         title = episode = title_data.get('title') or _search_kane('videoTitle') or self._og_search_title(webpage)
122         series = _search_kane('showName')
123         if series:
124             title = '%s - %s' % (series, title)
125         description = self._html_search_meta(['description', 'og:description'], webpage, fatal=True)
126
127         try:
128             sources = self._download_json(
129                 'https://prod-api-funimationnow.dadcdigital.com/api/source/catalog/video/%s/signed/' % video_id,
130                 video_id)['items']
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)
136             raise
137
138         formats = []
139         for source in sources:
140             source_url = source.get('src')
141             if not source_url:
142                 continue
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))
148             else:
149                 formats.append({
150                     'format_id': source_type,
151                     'url': source_url,
152                 })
153         self._sort_formats(formats)
154
155         return {
156             'id': video_id,
157             'display_id': display_id,
158             'title': title,
159             'description': description,
160             'thumbnail': self._og_search_thumbnail(webpage),
161             'series': series,
162             'season_number': int_or_none(title_data.get('seasonNum') or _search_kane('season')),
163             'episode_number': int_or_none(title_data.get('episodeNum')),
164             'episode': episode,
165             'season_id': title_data.get('seriesId'),
166             'formats': formats,
167         }