1 from __future__ import unicode_literals
5 from .common import InfoExtractor
20 class AnimeOnDemandIE(InfoExtractor):
21 _VALID_URL = r'https?://(?:www\.)?anime-on-demand\.de/anime/(?P<id>\d+)'
22 _LOGIN_URL = 'https://www.anime-on-demand.de/users/sign_in'
23 _APPLY_HTML5_URL = 'https://www.anime-on-demand.de/html5apply'
24 _NETRC_MACHINE = 'animeondemand'
26 'url': 'https://www.anime-on-demand.de/anime/161',
29 'title': 'Grimgar, Ashes and Illusions (OmU)',
30 'description': 'md5:6681ce3c07c7189d255ac6ab23812d31',
32 'playlist_mincount': 4,
34 # Film wording is used instead of Episode
35 'url': 'https://www.anime-on-demand.de/anime/39',
36 'only_matching': True,
38 # Episodes without titles
39 'url': 'https://www.anime-on-demand.de/anime/162',
40 'only_matching': True,
42 # ger/jap, Dub/OmU, account required
43 'url': 'https://www.anime-on-demand.de/anime/169',
44 'only_matching': True,
48 (username, password) = self._get_login_info()
52 login_page = self._download_webpage(
53 self._LOGIN_URL, None, 'Downloading login page')
55 if '>Our licensing terms allow the distribution of animes only to German-speaking countries of Europe' in login_page:
56 self.raise_geo_restricted(
57 '%s is only available in German-speaking countries of Europe' % self.IE_NAME)
59 login_form = self._form_hidden_inputs('new_user', login_page)
62 'user[login]': username,
63 'user[password]': password,
66 post_url = self._search_regex(
67 r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page,
68 'post url', default=self._LOGIN_URL, group='url')
70 if not post_url.startswith('http'):
71 post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url)
73 request = sanitized_Request(
74 post_url, urlencode_postdata(encode_dict(login_form)))
75 request.add_header('Referer', self._LOGIN_URL)
77 response = self._download_webpage(
78 request, None, 'Logging in as %s' % username)
80 if all(p not in response for p in ('>Logout<', 'href="/users/sign_out"')):
81 error = self._search_regex(
82 r'<p class="alert alert-danger">(.+?)</p>',
83 response, 'error', default=None)
85 raise ExtractorError('Unable to login: %s' % error, expected=True)
86 raise ExtractorError('Unable to log in')
88 def _real_initialize(self):
91 def _real_extract(self, url):
92 anime_id = self._match_id(url)
94 webpage = self._download_webpage(url, anime_id)
96 if 'data-playlist=' not in webpage:
97 self._download_webpage(
98 self._APPLY_HTML5_URL, anime_id,
99 'Activating HTML5 beta', 'Unable to apply HTML5 beta')
100 webpage = self._download_webpage(url, anime_id)
102 csrf_token = self._html_search_meta(
103 'csrf-token', webpage, 'csrf token', fatal=True)
105 anime_title = self._html_search_regex(
106 r'(?s)<h1[^>]+itemprop="name"[^>]*>(.+?)</h1>',
107 webpage, 'anime name')
108 anime_description = self._html_search_regex(
109 r'(?s)<div[^>]+itemprop="description"[^>]*>(.+?)</div>',
110 webpage, 'anime description', default=None)
114 for num, episode_html in enumerate(re.findall(
115 r'(?s)<h3[^>]+class="episodebox-title".+?>Episodeninhalt<', webpage), 1):
116 episodebox_title = self._search_regex(
117 (r'class="episodebox-title"[^>]+title=(["\'])(?P<title>.+?)\1',
118 r'class="episodebox-title"[^>]+>(?P<title>.+?)<'),
119 episode_html, 'episodebox title', default=None, group='title')
120 if not episodebox_title:
123 episode_number = int(self._search_regex(
124 r'(?:Episode|Film)\s*(\d+)',
125 episodebox_title, 'episode number', default=num))
126 episode_title = self._search_regex(
127 r'(?:Episode|Film)\s*\d+\s*-\s*(.+)',
128 episodebox_title, 'episode title', default=None)
130 video_id = 'episode-%d' % episode_number
134 'series': anime_title,
135 'episode': episode_title,
136 'episode_number': episode_number,
141 for input_ in re.findall(
142 r'<input[^>]+class=["\'].*?streamstarter_html5[^>]+>', episode_html):
143 attributes = extract_attributes(input_)
145 for playlist_key in ('data-playlist', 'data-otherplaylist'):
146 playlist_url = attributes.get(playlist_key)
147 if isinstance(playlist_url, compat_str) and re.match(
148 r'/?[\da-zA-Z]+', playlist_url):
149 playlist_urls.append(attributes[playlist_key])
150 if not playlist_urls:
153 lang = attributes.get('data-lang')
154 lang_note = attributes.get('value')
156 for playlist_url in playlist_urls:
157 kind = self._search_regex(
158 r'videomaterialurl/\d+/([^/]+)/',
159 playlist_url, 'media kind', default=None)
162 format_id_list.append(lang)
164 format_id_list.append(kind)
165 if not format_id_list:
166 format_id_list.append(compat_str(num))
167 format_id = '-'.join(format_id_list)
168 format_note = ', '.join(filter(None, (kind, lang_note)))
169 request = sanitized_Request(
170 compat_urlparse.urljoin(url, playlist_url),
172 'X-Requested-With': 'XMLHttpRequest',
173 'X-CSRF-Token': csrf_token,
175 'Accept': 'application/json, text/javascript, */*; q=0.01',
177 playlist = self._download_json(
178 request, video_id, 'Downloading %s playlist JSON' % format_id,
182 start_video = playlist.get('startvideo', 0)
183 playlist = playlist.get('playlist')
184 if not playlist or not isinstance(playlist, list):
186 playlist = playlist[start_video]
187 title = playlist.get('title')
190 description = playlist.get('description')
191 for source in playlist.get('sources', []):
192 file_ = source.get('file')
195 ext = determine_ext(file_)
196 format_id_list = [lang, kind]
198 format_id_list.append('hls')
199 elif source.get('type') == 'video/dash' or ext == 'mpd':
200 format_id_list.append('dash')
201 format_id = '-'.join(filter(None, format_id_list))
203 file_formats = self._extract_m3u8_formats(
204 file_, video_id, 'mp4',
205 entry_protocol='m3u8_native', m3u8_id=format_id, fatal=False)
206 elif source.get('type') == 'video/dash' or ext == 'mpd':
207 file_formats = self._extract_mpd_formats(
208 file_, video_id, mpd_id=format_id, fatal=False)
211 for f in file_formats:
214 'format_note': format_note,
216 formats.extend(file_formats)
219 self._sort_formats(formats)
220 f = common_info.copy()
223 'description': description,
229 r'data-dialog-header=(["\'])(?P<title>.+?)\1[^>]+href=(["\'])(?P<href>.+?)\3[^>]*>Teaser<',
232 f = common_info.copy()
234 'id': '%s-teaser' % f['id'],
235 'title': m.group('title'),
236 'url': compat_urlparse.urljoin(url, m.group('href')),
240 return self.playlist_result(entries, anime_id, anime_title, anime_description)