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.

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