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.

160 lines
5.8 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. _TEST = {
  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. def _login(self):
  27. (username, password) = self._get_login_info()
  28. if username is None:
  29. return
  30. login_page = self._download_webpage(
  31. self._LOGIN_URL, None, 'Downloading login page')
  32. login_form = self._form_hidden_inputs('new_user', login_page)
  33. login_form.update({
  34. 'user[login]': username,
  35. 'user[password]': password,
  36. })
  37. post_url = self._search_regex(
  38. r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page,
  39. 'post url', default=self._LOGIN_URL, group='url')
  40. if not post_url.startswith('http'):
  41. post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url)
  42. request = sanitized_Request(
  43. post_url, urlencode_postdata(encode_dict(login_form)))
  44. request.add_header('Referer', self._LOGIN_URL)
  45. response = self._download_webpage(
  46. request, None, 'Logging in as %s' % username)
  47. if all(p not in response for p in ('>Logout<', 'href="/users/sign_out"')):
  48. error = self._search_regex(
  49. r'<p class="alert alert-danger">(.+?)</p>',
  50. response, 'error', default=None)
  51. if error:
  52. raise ExtractorError('Unable to login: %s' % error, expected=True)
  53. raise ExtractorError('Unable to log in')
  54. def _real_initialize(self):
  55. self._login()
  56. def _real_extract(self, url):
  57. anime_id = self._match_id(url)
  58. webpage = self._download_webpage(url, anime_id)
  59. if 'data-playlist=' not in webpage:
  60. self._download_webpage(
  61. self._APPLY_HTML5_URL, anime_id,
  62. 'Activating HTML5 beta', 'Unable to apply HTML5 beta')
  63. webpage = self._download_webpage(url, anime_id)
  64. csrf_token = self._html_search_meta(
  65. 'csrf-token', webpage, 'csrf token', fatal=True)
  66. anime_title = self._html_search_regex(
  67. r'(?s)<h1[^>]+itemprop="name"[^>]*>(.+?)</h1>',
  68. webpage, 'anime name')
  69. anime_description = self._html_search_regex(
  70. r'(?s)<div[^>]+itemprop="description"[^>]*>(.+?)</div>',
  71. webpage, 'anime description', default=None)
  72. entries = []
  73. for episode_html in re.findall(r'(?s)<h3[^>]+class="episodebox-title".+?>Episodeninhalt<', webpage):
  74. m = re.search(
  75. r'class="episodebox-title"[^>]+title="Episode (?P<number>\d+) - (?P<title>.+?)"', episode_html)
  76. if not m:
  77. continue
  78. episode_number = int(m.group('number'))
  79. episode_title = m.group('title')
  80. video_id = 'episode-%d' % episode_number
  81. common_info = {
  82. 'id': video_id,
  83. 'series': anime_title,
  84. 'episode': episode_title,
  85. 'episode_number': episode_number,
  86. }
  87. formats = []
  88. playlist_url = self._search_regex(
  89. r'data-playlist=(["\'])(?P<url>.+?)\1',
  90. episode_html, 'data playlist', default=None, group='url')
  91. if playlist_url:
  92. request = sanitized_Request(
  93. compat_urlparse.urljoin(url, playlist_url),
  94. headers={
  95. 'X-Requested-With': 'XMLHttpRequest',
  96. 'X-CSRF-Token': csrf_token,
  97. 'Referer': url,
  98. 'Accept': 'application/json, text/javascript, */*; q=0.01',
  99. })
  100. playlist = self._download_json(
  101. request, video_id, 'Downloading playlist JSON', fatal=False)
  102. if playlist:
  103. playlist = playlist['playlist'][0]
  104. title = playlist['title']
  105. description = playlist.get('description')
  106. for source in playlist.get('sources', []):
  107. file_ = source.get('file')
  108. if file_ and determine_ext(file_) == 'm3u8':
  109. formats = self._extract_m3u8_formats(
  110. file_, video_id, 'mp4',
  111. entry_protocol='m3u8_native', m3u8_id='hls')
  112. if formats:
  113. f = common_info.copy()
  114. f.update({
  115. 'title': title,
  116. 'description': description,
  117. 'formats': formats,
  118. })
  119. entries.append(f)
  120. m = re.search(
  121. r'data-dialog-header=(["\'])(?P<title>.+?)\1[^>]+href=(["\'])(?P<href>.+?)\3[^>]*>Teaser<',
  122. episode_html)
  123. if m:
  124. f = common_info.copy()
  125. f.update({
  126. 'id': '%s-teaser' % f['id'],
  127. 'title': m.group('title'),
  128. 'url': compat_urlparse.urljoin(url, m.group('href')),
  129. })
  130. entries.append(f)
  131. return self.playlist_result(entries, anime_id, anime_title, anime_description)