animeondemand.py 5.9 KB

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