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