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.

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