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.

176 lines
6.5 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_urlparse
  5. from ..utils import (
  6. determine_ext,
  7. encode_dict,
  8. ExtractorError,
  9. sanitized_Request,
  10. urlencode_postdata,
  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. _TESTS = [{
  18. 'url': 'https://www.anime-on-demand.de/anime/161',
  19. 'info_dict': {
  20. 'id': '161',
  21. 'title': 'Grimgar, Ashes and Illusions (OmU)',
  22. 'description': 'md5:6681ce3c07c7189d255ac6ab23812d31',
  23. },
  24. 'playlist_mincount': 4,
  25. }, {
  26. # Film wording is used instead of Episode
  27. 'url': 'https://www.anime-on-demand.de/anime/39',
  28. 'only_matching': True,
  29. }, {
  30. # Episodes without titles
  31. 'url': 'https://www.anime-on-demand.de/anime/162',
  32. 'only_matching': True,
  33. }]
  34. def _login(self):
  35. (username, password) = self._get_login_info()
  36. if username is None:
  37. return
  38. login_page = self._download_webpage(
  39. self._LOGIN_URL, None, 'Downloading login page')
  40. login_form = self._form_hidden_inputs('new_user', login_page)
  41. login_form.update({
  42. 'user[login]': username,
  43. 'user[password]': password,
  44. })
  45. post_url = self._search_regex(
  46. r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page,
  47. 'post url', default=self._LOGIN_URL, group='url')
  48. if not post_url.startswith('http'):
  49. post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url)
  50. request = sanitized_Request(
  51. post_url, urlencode_postdata(encode_dict(login_form)))
  52. request.add_header('Referer', self._LOGIN_URL)
  53. response = self._download_webpage(
  54. request, None, 'Logging in as %s' % username)
  55. if all(p not in response for p in ('>Logout<', 'href="/users/sign_out"')):
  56. error = self._search_regex(
  57. r'<p class="alert alert-danger">(.+?)</p>',
  58. response, 'error', default=None)
  59. if error:
  60. raise ExtractorError('Unable to login: %s' % error, expected=True)
  61. raise ExtractorError('Unable to log in')
  62. def _real_initialize(self):
  63. self._login()
  64. def _real_extract(self, url):
  65. anime_id = self._match_id(url)
  66. webpage = self._download_webpage(url, anime_id)
  67. if 'data-playlist=' not in webpage:
  68. self._download_webpage(
  69. self._APPLY_HTML5_URL, anime_id,
  70. 'Activating HTML5 beta', 'Unable to apply HTML5 beta')
  71. webpage = self._download_webpage(url, anime_id)
  72. csrf_token = self._html_search_meta(
  73. 'csrf-token', webpage, 'csrf token', fatal=True)
  74. anime_title = self._html_search_regex(
  75. r'(?s)<h1[^>]+itemprop="name"[^>]*>(.+?)</h1>',
  76. webpage, 'anime name')
  77. anime_description = self._html_search_regex(
  78. r'(?s)<div[^>]+itemprop="description"[^>]*>(.+?)</div>',
  79. webpage, 'anime description', default=None)
  80. entries = []
  81. for num, episode_html in enumerate(re.findall(
  82. r'(?s)<h3[^>]+class="episodebox-title".+?>Episodeninhalt<', webpage), 1):
  83. episodebox_title = self._search_regex(
  84. (r'class="episodebox-title"[^>]+title=(["\'])(?P<title>.+?)\1',
  85. r'class="episodebox-title"[^>]+>(?P<title>.+?)<'),
  86. episode_html, 'episodebox title', default=None, group='title')
  87. if not episodebox_title:
  88. continue
  89. episode_number = int(self._search_regex(
  90. r'(?:Episode|Film)\s*(\d+)',
  91. episodebox_title, 'episode number', default=num))
  92. episode_title = self._search_regex(
  93. r'(?:Episode|Film)\s*\d+\s*-\s*(.+)',
  94. episodebox_title, 'episode title', default=None)
  95. video_id = 'episode-%d' % episode_number
  96. common_info = {
  97. 'id': video_id,
  98. 'series': anime_title,
  99. 'episode': episode_title,
  100. 'episode_number': episode_number,
  101. }
  102. formats = []
  103. playlist_url = self._search_regex(
  104. r'data-playlist=(["\'])(?P<url>.+?)\1',
  105. episode_html, 'data playlist', default=None, group='url')
  106. if playlist_url:
  107. request = sanitized_Request(
  108. compat_urlparse.urljoin(url, playlist_url),
  109. headers={
  110. 'X-Requested-With': 'XMLHttpRequest',
  111. 'X-CSRF-Token': csrf_token,
  112. 'Referer': url,
  113. 'Accept': 'application/json, text/javascript, */*; q=0.01',
  114. })
  115. playlist = self._download_json(
  116. request, video_id, 'Downloading playlist JSON', fatal=False)
  117. if playlist:
  118. playlist = playlist['playlist'][0]
  119. title = playlist['title']
  120. description = playlist.get('description')
  121. for source in playlist.get('sources', []):
  122. file_ = source.get('file')
  123. if file_ and determine_ext(file_) == 'm3u8':
  124. formats = self._extract_m3u8_formats(
  125. file_, video_id, 'mp4',
  126. entry_protocol='m3u8_native', m3u8_id='hls')
  127. if formats:
  128. f = common_info.copy()
  129. f.update({
  130. 'title': title,
  131. 'description': description,
  132. 'formats': formats,
  133. })
  134. entries.append(f)
  135. m = re.search(
  136. r'data-dialog-header=(["\'])(?P<title>.+?)\1[^>]+href=(["\'])(?P<href>.+?)\3[^>]*>Teaser<',
  137. episode_html)
  138. if m:
  139. f = common_info.copy()
  140. f.update({
  141. 'id': '%s-teaser' % f['id'],
  142. 'title': m.group('title'),
  143. 'url': compat_urlparse.urljoin(url, m.group('href')),
  144. })
  145. entries.append(f)
  146. return self.playlist_result(entries, anime_id, anime_title, anime_description)