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.

242 lines
9.7 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_urlparse,
  6. compat_str,
  7. )
  8. from ..utils import (
  9. determine_ext,
  10. extract_attributes,
  11. ExtractorError,
  12. sanitized_Request,
  13. urlencode_postdata,
  14. )
  15. class AnimeOnDemandIE(InfoExtractor):
  16. _VALID_URL = r'https?://(?:www\.)?anime-on-demand\.de/anime/(?P<id>\d+)'
  17. _LOGIN_URL = 'https://www.anime-on-demand.de/users/sign_in'
  18. _APPLY_HTML5_URL = 'https://www.anime-on-demand.de/html5apply'
  19. _NETRC_MACHINE = 'animeondemand'
  20. _TESTS = [{
  21. 'url': 'https://www.anime-on-demand.de/anime/161',
  22. 'info_dict': {
  23. 'id': '161',
  24. 'title': 'Grimgar, Ashes and Illusions (OmU)',
  25. 'description': 'md5:6681ce3c07c7189d255ac6ab23812d31',
  26. },
  27. 'playlist_mincount': 4,
  28. }, {
  29. # Film wording is used instead of Episode
  30. 'url': 'https://www.anime-on-demand.de/anime/39',
  31. 'only_matching': True,
  32. }, {
  33. # Episodes without titles
  34. 'url': 'https://www.anime-on-demand.de/anime/162',
  35. 'only_matching': True,
  36. }, {
  37. # ger/jap, Dub/OmU, account required
  38. 'url': 'https://www.anime-on-demand.de/anime/169',
  39. 'only_matching': True,
  40. }]
  41. def _login(self):
  42. (username, password) = self._get_login_info()
  43. if username is None:
  44. return
  45. login_page = self._download_webpage(
  46. self._LOGIN_URL, None, 'Downloading login page')
  47. if '>Our licensing terms allow the distribution of animes only to German-speaking countries of Europe' in login_page:
  48. self.raise_geo_restricted(
  49. '%s is only available in German-speaking countries of Europe' % self.IE_NAME)
  50. login_form = self._form_hidden_inputs('new_user', login_page)
  51. login_form.update({
  52. 'user[login]': username,
  53. 'user[password]': password,
  54. })
  55. post_url = self._search_regex(
  56. r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page,
  57. 'post url', default=self._LOGIN_URL, group='url')
  58. if not post_url.startswith('http'):
  59. post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url)
  60. request = sanitized_Request(
  61. post_url, urlencode_postdata(login_form))
  62. request.add_header('Referer', self._LOGIN_URL)
  63. response = self._download_webpage(
  64. request, None, 'Logging in as %s' % username)
  65. if all(p not in response for p in ('>Logout<', 'href="/users/sign_out"')):
  66. error = self._search_regex(
  67. r'<p class="alert alert-danger">(.+?)</p>',
  68. response, 'error', default=None)
  69. if error:
  70. raise ExtractorError('Unable to login: %s' % error, expected=True)
  71. raise ExtractorError('Unable to log in')
  72. def _real_initialize(self):
  73. self._login()
  74. def _real_extract(self, url):
  75. anime_id = self._match_id(url)
  76. webpage = self._download_webpage(url, anime_id)
  77. if 'data-playlist=' not in webpage:
  78. self._download_webpage(
  79. self._APPLY_HTML5_URL, anime_id,
  80. 'Activating HTML5 beta', 'Unable to apply HTML5 beta')
  81. webpage = self._download_webpage(url, anime_id)
  82. csrf_token = self._html_search_meta(
  83. 'csrf-token', webpage, 'csrf token', fatal=True)
  84. anime_title = self._html_search_regex(
  85. r'(?s)<h1[^>]+itemprop="name"[^>]*>(.+?)</h1>',
  86. webpage, 'anime name')
  87. anime_description = self._html_search_regex(
  88. r'(?s)<div[^>]+itemprop="description"[^>]*>(.+?)</div>',
  89. webpage, 'anime description', default=None)
  90. entries = []
  91. for num, episode_html in enumerate(re.findall(
  92. r'(?s)<h3[^>]+class="episodebox-title".+?>Episodeninhalt<', webpage), 1):
  93. episodebox_title = self._search_regex(
  94. (r'class="episodebox-title"[^>]+title=(["\'])(?P<title>.+?)\1',
  95. r'class="episodebox-title"[^>]+>(?P<title>.+?)<'),
  96. episode_html, 'episodebox title', default=None, group='title')
  97. if not episodebox_title:
  98. continue
  99. episode_number = int(self._search_regex(
  100. r'(?:Episode|Film)\s*(\d+)',
  101. episodebox_title, 'episode number', default=num))
  102. episode_title = self._search_regex(
  103. r'(?:Episode|Film)\s*\d+\s*-\s*(.+)',
  104. episodebox_title, 'episode title', default=None)
  105. video_id = 'episode-%d' % episode_number
  106. common_info = {
  107. 'id': video_id,
  108. 'series': anime_title,
  109. 'episode': episode_title,
  110. 'episode_number': episode_number,
  111. }
  112. formats = []
  113. for input_ in re.findall(
  114. r'<input[^>]+class=["\'].*?streamstarter_html5[^>]+>', episode_html):
  115. attributes = extract_attributes(input_)
  116. playlist_urls = []
  117. for playlist_key in ('data-playlist', 'data-otherplaylist'):
  118. playlist_url = attributes.get(playlist_key)
  119. if isinstance(playlist_url, compat_str) and re.match(
  120. r'/?[\da-zA-Z]+', playlist_url):
  121. playlist_urls.append(attributes[playlist_key])
  122. if not playlist_urls:
  123. continue
  124. lang = attributes.get('data-lang')
  125. lang_note = attributes.get('value')
  126. for playlist_url in playlist_urls:
  127. kind = self._search_regex(
  128. r'videomaterialurl/\d+/([^/]+)/',
  129. playlist_url, 'media kind', default=None)
  130. format_id_list = []
  131. if lang:
  132. format_id_list.append(lang)
  133. if kind:
  134. format_id_list.append(kind)
  135. if not format_id_list:
  136. format_id_list.append(compat_str(num))
  137. format_id = '-'.join(format_id_list)
  138. format_note = ', '.join(filter(None, (kind, lang_note)))
  139. request = sanitized_Request(
  140. compat_urlparse.urljoin(url, playlist_url),
  141. headers={
  142. 'X-Requested-With': 'XMLHttpRequest',
  143. 'X-CSRF-Token': csrf_token,
  144. 'Referer': url,
  145. 'Accept': 'application/json, text/javascript, */*; q=0.01',
  146. })
  147. playlist = self._download_json(
  148. request, video_id, 'Downloading %s playlist JSON' % format_id,
  149. fatal=False)
  150. if not playlist:
  151. continue
  152. start_video = playlist.get('startvideo', 0)
  153. playlist = playlist.get('playlist')
  154. if not playlist or not isinstance(playlist, list):
  155. continue
  156. playlist = playlist[start_video]
  157. title = playlist.get('title')
  158. if not title:
  159. continue
  160. description = playlist.get('description')
  161. for source in playlist.get('sources', []):
  162. file_ = source.get('file')
  163. if not file_:
  164. continue
  165. ext = determine_ext(file_)
  166. format_id_list = [lang, kind]
  167. if ext == 'm3u8':
  168. format_id_list.append('hls')
  169. elif source.get('type') == 'video/dash' or ext == 'mpd':
  170. format_id_list.append('dash')
  171. format_id = '-'.join(filter(None, format_id_list))
  172. if ext == 'm3u8':
  173. file_formats = self._extract_m3u8_formats(
  174. file_, video_id, 'mp4',
  175. entry_protocol='m3u8_native', m3u8_id=format_id, fatal=False)
  176. elif source.get('type') == 'video/dash' or ext == 'mpd':
  177. continue
  178. file_formats = self._extract_mpd_formats(
  179. file_, video_id, mpd_id=format_id, fatal=False)
  180. else:
  181. continue
  182. for f in file_formats:
  183. f.update({
  184. 'language': lang,
  185. 'format_note': format_note,
  186. })
  187. formats.extend(file_formats)
  188. if formats:
  189. self._sort_formats(formats)
  190. f = common_info.copy()
  191. f.update({
  192. 'title': title,
  193. 'description': description,
  194. 'formats': formats,
  195. })
  196. entries.append(f)
  197. # Extract teaser only when full episode is not available
  198. if not formats:
  199. m = re.search(
  200. r'data-dialog-header=(["\'])(?P<title>.+?)\1[^>]+href=(["\'])(?P<href>.+?)\3[^>]*>Teaser<',
  201. episode_html)
  202. if m:
  203. f = common_info.copy()
  204. f.update({
  205. 'id': '%s-teaser' % f['id'],
  206. 'title': m.group('title'),
  207. 'url': compat_urlparse.urljoin(url, m.group('href')),
  208. })
  209. entries.append(f)
  210. return self.playlist_result(entries, anime_id, anime_title, anime_description)