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.

292 lines
12 KiB

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