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.

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