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