You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
11 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_urlparse,
  6. compat_str,
  7. )
  8. from ..utils import (
  9. determine_ext,
  10. extract_attributes,
  11. ExtractorError,
  12. sanitized_Request,
  13. urlencode_postdata,
  14. )
  15. class AnimeOnDemandIE(InfoExtractor):
  16. _VALID_URL = r'https?://(?:www\.)?anime-on-demand\.de/anime/(?P<id>\d+)'
  17. _LOGIN_URL = 'https://www.anime-on-demand.de/users/sign_in'
  18. _APPLY_HTML5_URL = 'https://www.anime-on-demand.de/html5apply'
  19. _NETRC_MACHINE = 'animeondemand'
  20. _TESTS = [{
  21. # jap, OmU
  22. 'url': 'https://www.anime-on-demand.de/anime/161',
  23. 'info_dict': {
  24. 'id': '161',
  25. 'title': 'Grimgar, Ashes and Illusions (OmU)',
  26. 'description': 'md5:6681ce3c07c7189d255ac6ab23812d31',
  27. },
  28. 'playlist_mincount': 4,
  29. }, {
  30. # Film wording is used instead of Episode, ger/jap, Dub/OmU
  31. 'url': 'https://www.anime-on-demand.de/anime/39',
  32. 'only_matching': True,
  33. }, {
  34. # Episodes without titles, jap, OmU
  35. 'url': 'https://www.anime-on-demand.de/anime/162',
  36. 'only_matching': True,
  37. }, {
  38. # ger/jap, Dub/OmU, account required
  39. 'url': 'https://www.anime-on-demand.de/anime/169',
  40. 'only_matching': True,
  41. }, {
  42. # Full length film, non-series, ger/jap, Dub/OmU, account required
  43. 'url': 'https://www.anime-on-demand.de/anime/185',
  44. 'only_matching': True,
  45. }]
  46. def _login(self):
  47. (username, password) = self._get_login_info()
  48. if username is None:
  49. return
  50. login_page = self._download_webpage(
  51. self._LOGIN_URL, None, 'Downloading login page')
  52. if '>Our licensing terms allow the distribution of animes only to German-speaking countries of Europe' in login_page:
  53. self.raise_geo_restricted(
  54. '%s is only available in German-speaking countries of Europe' % self.IE_NAME)
  55. login_form = self._form_hidden_inputs('new_user', login_page)
  56. login_form.update({
  57. 'user[login]': username,
  58. 'user[password]': password,
  59. })
  60. post_url = self._search_regex(
  61. r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page,
  62. 'post url', default=self._LOGIN_URL, group='url')
  63. if not post_url.startswith('http'):
  64. post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url)
  65. request = sanitized_Request(
  66. post_url, urlencode_postdata(login_form))
  67. request.add_header('Referer', self._LOGIN_URL)
  68. response = self._download_webpage(
  69. request, None, 'Logging in as %s' % username)
  70. if all(p not in response for p in ('>Logout<', 'href="/users/sign_out"')):
  71. error = self._search_regex(
  72. r'<p class="alert alert-danger">(.+?)</p>',
  73. response, 'error', default=None)
  74. if error:
  75. raise ExtractorError('Unable to login: %s' % error, expected=True)
  76. raise ExtractorError('Unable to log in')
  77. def _real_initialize(self):
  78. self._login()
  79. def _real_extract(self, url):
  80. anime_id = self._match_id(url)
  81. webpage = self._download_webpage(url, anime_id)
  82. if 'data-playlist=' not in webpage:
  83. self._download_webpage(
  84. self._APPLY_HTML5_URL, anime_id,
  85. 'Activating HTML5 beta', 'Unable to apply HTML5 beta')
  86. webpage = self._download_webpage(url, anime_id)
  87. csrf_token = self._html_search_meta(
  88. 'csrf-token', webpage, 'csrf token', fatal=True)
  89. anime_title = self._html_search_regex(
  90. r'(?s)<h1[^>]+itemprop="name"[^>]*>(.+?)</h1>',
  91. webpage, 'anime name')
  92. anime_description = self._html_search_regex(
  93. r'(?s)<div[^>]+itemprop="description"[^>]*>(.+?)</div>',
  94. webpage, 'anime description', default=None)
  95. entries = []
  96. def extract_info(html, video_id, num=None):
  97. title, description = [None] * 2
  98. formats = []
  99. for input_ in re.findall(
  100. r'<input[^>]+class=["\'].*?streamstarter_html5[^>]+>', html):
  101. attributes = extract_attributes(input_)
  102. playlist_urls = []
  103. for playlist_key in ('data-playlist', 'data-otherplaylist'):
  104. playlist_url = attributes.get(playlist_key)
  105. if isinstance(playlist_url, compat_str) and re.match(
  106. r'/?[\da-zA-Z]+', playlist_url):
  107. playlist_urls.append(attributes[playlist_key])
  108. if not playlist_urls:
  109. continue
  110. lang = attributes.get('data-lang')
  111. lang_note = attributes.get('value')
  112. for playlist_url in playlist_urls:
  113. kind = self._search_regex(
  114. r'videomaterialurl/\d+/([^/]+)/',
  115. playlist_url, 'media kind', default=None)
  116. format_id_list = []
  117. if lang:
  118. format_id_list.append(lang)
  119. if kind:
  120. format_id_list.append(kind)
  121. if not format_id_list and num is not None:
  122. format_id_list.append(compat_str(num))
  123. format_id = '-'.join(format_id_list)
  124. format_note = ', '.join(filter(None, (kind, lang_note)))
  125. request = sanitized_Request(
  126. compat_urlparse.urljoin(url, playlist_url),
  127. headers={
  128. 'X-Requested-With': 'XMLHttpRequest',
  129. 'X-CSRF-Token': csrf_token,
  130. 'Referer': url,
  131. 'Accept': 'application/json, text/javascript, */*; q=0.01',
  132. })
  133. playlist = self._download_json(
  134. request, video_id, 'Downloading %s playlist JSON' % format_id,
  135. fatal=False)
  136. if not playlist:
  137. continue
  138. start_video = playlist.get('startvideo', 0)
  139. playlist = playlist.get('playlist')
  140. if not playlist or not isinstance(playlist, list):
  141. continue
  142. playlist = playlist[start_video]
  143. title = playlist.get('title')
  144. if not title:
  145. continue
  146. description = playlist.get('description')
  147. for source in playlist.get('sources', []):
  148. file_ = source.get('file')
  149. if not file_:
  150. continue
  151. ext = determine_ext(file_)
  152. format_id_list = [lang, kind]
  153. if ext == 'm3u8':
  154. format_id_list.append('hls')
  155. elif source.get('type') == 'video/dash' or ext == 'mpd':
  156. format_id_list.append('dash')
  157. format_id = '-'.join(filter(None, format_id_list))
  158. if ext == 'm3u8':
  159. file_formats = self._extract_m3u8_formats(
  160. file_, video_id, 'mp4',
  161. entry_protocol='m3u8_native', m3u8_id=format_id, fatal=False)
  162. elif source.get('type') == 'video/dash' or ext == 'mpd':
  163. continue
  164. file_formats = self._extract_mpd_formats(
  165. file_, video_id, mpd_id=format_id, fatal=False)
  166. else:
  167. continue
  168. for f in file_formats:
  169. f.update({
  170. 'language': lang,
  171. 'format_note': format_note,
  172. })
  173. formats.extend(file_formats)
  174. return {
  175. 'title': title,
  176. 'description': description,
  177. 'formats': formats,
  178. }
  179. def extract_entries(html, video_id, common_info, num=None):
  180. info = extract_info(html, video_id, num)
  181. if info['formats']:
  182. self._sort_formats(info['formats'])
  183. f = common_info.copy()
  184. f.update(info)
  185. entries.append(f)
  186. # Extract teaser/trailer only when full episode is not available
  187. if not info['formats']:
  188. m = re.search(
  189. r'data-dialog-header=(["\'])(?P<title>.+?)\1[^>]+href=(["\'])(?P<href>.+?)\3[^>]*>(?P<kind>Teaser|Trailer)<',
  190. html)
  191. if m:
  192. f = common_info.copy()
  193. f.update({
  194. 'id': '%s-%s' % (f['id'], m.group('kind').lower()),
  195. 'title': m.group('title'),
  196. 'url': compat_urlparse.urljoin(url, m.group('href')),
  197. })
  198. entries.append(f)
  199. def extract_episodes(html):
  200. for num, episode_html in enumerate(re.findall(
  201. r'(?s)<h3[^>]+class="episodebox-title".+?>Episodeninhalt<', html), 1):
  202. episodebox_title = self._search_regex(
  203. (r'class="episodebox-title"[^>]+title=(["\'])(?P<title>.+?)\1',
  204. r'class="episodebox-title"[^>]+>(?P<title>.+?)<'),
  205. episode_html, 'episodebox title', default=None, group='title')
  206. if not episodebox_title:
  207. continue
  208. episode_number = int(self._search_regex(
  209. r'(?:Episode|Film)\s*(\d+)',
  210. episodebox_title, 'episode number', default=num))
  211. episode_title = self._search_regex(
  212. r'(?:Episode|Film)\s*\d+\s*-\s*(.+)',
  213. episodebox_title, 'episode title', default=None)
  214. video_id = 'episode-%d' % episode_number
  215. common_info = {
  216. 'id': video_id,
  217. 'series': anime_title,
  218. 'episode': episode_title,
  219. 'episode_number': episode_number,
  220. }
  221. extract_entries(episode_html, video_id, common_info)
  222. def extract_film(html, video_id):
  223. common_info = {
  224. 'id': anime_id,
  225. 'title': anime_title,
  226. 'description': anime_description,
  227. }
  228. extract_entries(html, video_id, common_info)
  229. extract_episodes(webpage)
  230. if not entries:
  231. extract_film(webpage, anime_id)
  232. return self.playlist_result(entries, anime_id, anime_title, anime_description)