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