animeondemand.py 5.7 KB

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