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.

87 lines
3.0 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_HTTPError,
  6. compat_str,
  7. compat_urllib_parse,
  8. compat_urllib_parse_urlparse,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. )
  13. class AddAnimeIE(InfoExtractor):
  14. _VALID_URL = r'^http://(?:\w+\.)?add-anime\.net/watch_video\.php\?(?:.*?)v=(?P<id>[\w_]+)(?:.*)'
  15. _TEST = {
  16. 'url': 'http://www.add-anime.net/watch_video.php?v=24MR3YO5SAS9',
  17. 'md5': '72954ea10bc979ab5e2eb288b21425a0',
  18. 'info_dict': {
  19. 'id': '24MR3YO5SAS9',
  20. 'ext': 'mp4',
  21. 'description': 'One Piece 606',
  22. 'title': 'One Piece 606',
  23. }
  24. }
  25. def _real_extract(self, url):
  26. video_id = self._match_id(url)
  27. try:
  28. webpage = self._download_webpage(url, video_id)
  29. except ExtractorError as ee:
  30. if not isinstance(ee.cause, compat_HTTPError) or \
  31. ee.cause.code != 503:
  32. raise
  33. redir_webpage = ee.cause.read().decode('utf-8')
  34. action = self._search_regex(
  35. r'<form id="challenge-form" action="([^"]+)"',
  36. redir_webpage, 'Redirect form')
  37. vc = self._search_regex(
  38. r'<input type="hidden" name="jschl_vc" value="([^"]+)"/>',
  39. redir_webpage, 'redirect vc value')
  40. av = re.search(
  41. r'a\.value = ([0-9]+)[+]([0-9]+)[*]([0-9]+);',
  42. redir_webpage)
  43. if av is None:
  44. raise ExtractorError('Cannot find redirect math task')
  45. av_res = int(av.group(1)) + int(av.group(2)) * int(av.group(3))
  46. parsed_url = compat_urllib_parse_urlparse(url)
  47. av_val = av_res + len(parsed_url.netloc)
  48. confirm_url = (
  49. parsed_url.scheme + '://' + parsed_url.netloc +
  50. action + '?' +
  51. compat_urllib_parse.urlencode({
  52. 'jschl_vc': vc, 'jschl_answer': compat_str(av_val)}))
  53. self._download_webpage(
  54. confirm_url, video_id,
  55. note='Confirming after redirect')
  56. webpage = self._download_webpage(url, video_id)
  57. formats = []
  58. for format_id in ('normal', 'hq'):
  59. rex = r"var %s_video_file = '(.*?)';" % re.escape(format_id)
  60. video_url = self._search_regex(rex, webpage, 'video file URLx',
  61. fatal=False)
  62. if not video_url:
  63. continue
  64. formats.append({
  65. 'format_id': format_id,
  66. 'url': video_url,
  67. })
  68. self._sort_formats(formats)
  69. video_title = self._og_search_title(webpage)
  70. video_description = self._og_search_description(webpage)
  71. return {
  72. '_type': 'video',
  73. 'id': video_id,
  74. 'formats': formats,
  75. 'title': video_title,
  76. 'description': video_description
  77. }