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.

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