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