| 
									
										
										
										
											2014-03-24 16:39:53 +01:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  | import re | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from .common import InfoExtractor | 
					
						
							| 
									
										
										
										
											2014-11-02 11:23:40 +01:00
										 |  |  | from ..compat import ( | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |     compat_HTTPError, | 
					
						
							|  |  |  |     compat_str, | 
					
						
							|  |  |  |     compat_urllib_parse, | 
					
						
							|  |  |  |     compat_urllib_parse_urlparse, | 
					
						
							| 
									
										
										
										
											2014-11-02 11:23:40 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | from ..utils import ( | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |     ExtractorError, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AddAnimeIE(InfoExtractor): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 20:34:47 +07:00
										 |  |  |     _VALID_URL = r'^http://(?:\w+\.)?add-anime\.net/watch_video\.php\?(?:.*?)v=(?P<video_id>[\w_]+)(?:.*)' | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |     _TEST = { | 
					
						
							| 
									
										
										
										
											2014-03-24 16:39:53 +01:00
										 |  |  |         'url': 'http://www.add-anime.net/watch_video.php?v=24MR3YO5SAS9', | 
					
						
							|  |  |  |         'md5': '72954ea10bc979ab5e2eb288b21425a0', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': '24MR3YO5SAS9', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'description': 'One Piece 606', | 
					
						
							|  |  |  |             'title': 'One Piece 606', | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             mobj = re.match(self._VALID_URL, url) | 
					
						
							|  |  |  |             video_id = mobj.group('video_id') | 
					
						
							|  |  |  |             webpage = self._download_webpage(url, video_id) | 
					
						
							|  |  |  |         except ExtractorError as ee: | 
					
						
							| 
									
										
										
										
											2013-10-28 11:24:47 +01:00
										 |  |  |             if not isinstance(ee.cause, compat_HTTPError) or \ | 
					
						
							|  |  |  |                ee.cause.code != 503: | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |                 raise | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             redir_webpage = ee.cause.read().decode('utf-8') | 
					
						
							|  |  |  |             action = self._search_regex( | 
					
						
							|  |  |  |                 r'<form id="challenge-form" action="([^"]+)"', | 
					
						
							| 
									
										
										
										
											2014-03-24 16:39:53 +01:00
										 |  |  |                 redir_webpage, 'Redirect form') | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |             vc = self._search_regex( | 
					
						
							|  |  |  |                 r'<input type="hidden" name="jschl_vc" value="([^"]+)"/>', | 
					
						
							| 
									
										
										
										
											2014-03-24 16:39:53 +01:00
										 |  |  |                 redir_webpage, 'redirect vc value') | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |             av = re.search( | 
					
						
							|  |  |  |                 r'a\.value = ([0-9]+)[+]([0-9]+)[*]([0-9]+);', | 
					
						
							|  |  |  |                 redir_webpage) | 
					
						
							|  |  |  |             if av is None: | 
					
						
							|  |  |  |                 raise ExtractorError(u'Cannot find redirect math task') | 
					
						
							|  |  |  |             av_res = int(av.group(1)) + int(av.group(2)) * int(av.group(3)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             parsed_url = compat_urllib_parse_urlparse(url) | 
					
						
							|  |  |  |             av_val = av_res + len(parsed_url.netloc) | 
					
						
							|  |  |  |             confirm_url = ( | 
					
						
							| 
									
										
										
										
											2014-03-24 16:39:53 +01:00
										 |  |  |                 parsed_url.scheme + '://' + parsed_url.netloc + | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |                 action + '?' + | 
					
						
							|  |  |  |                 compat_urllib_parse.urlencode({ | 
					
						
							|  |  |  |                     'jschl_vc': vc, 'jschl_answer': compat_str(av_val)})) | 
					
						
							|  |  |  |             self._download_webpage( | 
					
						
							|  |  |  |                 confirm_url, video_id, | 
					
						
							| 
									
										
										
										
											2014-03-24 16:39:53 +01:00
										 |  |  |                 note='Confirming after redirect') | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |             webpage = self._download_webpage(url, video_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-28 11:24:47 +01:00
										 |  |  |         formats = [] | 
					
						
							|  |  |  |         for format_id in ('normal', 'hq'): | 
					
						
							|  |  |  |             rex = r"var %s_video_file = '(.*?)';" % re.escape(format_id) | 
					
						
							| 
									
										
										
										
											2014-03-24 16:39:53 +01:00
										 |  |  |             video_url = self._search_regex(rex, webpage, 'video file URLx', | 
					
						
							| 
									
										
										
										
											2013-10-28 11:24:47 +01:00
										 |  |  |                                            fatal=False) | 
					
						
							|  |  |  |             if not video_url: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             formats.append({ | 
					
						
							|  |  |  |                 'format_id': format_id, | 
					
						
							|  |  |  |                 'url': video_url, | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2014-03-24 16:39:53 +01:00
										 |  |  |         self._sort_formats(formats) | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |         video_title = self._og_search_title(webpage) | 
					
						
							|  |  |  |         video_description = self._og_search_description(webpage) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             '_type': 'video', | 
					
						
							| 
									
										
										
										
											2014-03-24 16:39:53 +01:00
										 |  |  |             'id': video_id, | 
					
						
							| 
									
										
										
										
											2013-10-28 11:24:47 +01:00
										 |  |  |             'formats': formats, | 
					
						
							| 
									
										
										
										
											2013-08-28 10:28:16 +02:00
										 |  |  |             'title': video_title, | 
					
						
							|  |  |  |             'description': video_description | 
					
						
							|  |  |  |         } |