def get_challenge(self, code): data = {} for find in re.findall( r'<input type=hidden name="([^"]+)" value="([^"]+)">'): data[find[0]] = find[1] data['adcopy_response'] = code url = self.path url = url[:url.find('media?c=')] + 'verify.noscript' data = util.post(url, data, random_ua=True) if not data: logging.error('Failed to submit challenge!') return None info = re.search(r'URL=(http[^"]+)', data) if not info: return None url = info.group(1) if self.secure: url = url.replace('http://', 'https://') data = util.get(url, random_ua=True) if data: return re.search(r'id=gibberish>([^<]+)', data).group(1) else: return None
def ask_for_code(self, link): data = util.get('http://api.recaptcha.net/challenge?k=' + self.id_, random_ua=True) challenge = re.search(r'challenge.*?:.*?\'(.*)?\',', data) server = re.search(r'server.*?:.*?\'(.*?)\',', data) if not challenge or not server: raise Exception('Failed to get ReCaptcha API response') self.challenge = challenge.group(1) self.server = server.group(1) self.captcha_address = self.server + 'image?c=' + self.challenge while self.tries < self.MAX_TRIES: if self.tries > 0: self.reload() code = self.get_code() if not self.challenge or not code: raise Exception('Invalid challenge or code!') data = { 'recaptcha_challenge_field': self.challenge, 'recaptcha_response_field': code } self.tries += 1 self.data = util.post(link, data, random_ua=True) info = re.search(r'challenge\?k=(.+?)"', self.data) if not info: # No ID found \o/ break self.id_ = info.group(1).replace('&error=1', '')
def get_challenge(self, code): data = {} for find in re.findall(r'<input type=hidden name="([^"]+)" value="([^"]+)">'): data[find[0]] = find[1] data['adcopy_response'] = code url = self.path url = url[:url.find('media?c=')] + 'verify.noscript' data = util.post(url, data, random_ua=True) if not data: logging.error('Failed to submit challenge!') return None info = re.search(r'URL=(http[^"]+)', data) if not info: return None url = info.group(1) if self.secure: url = url.replace('http://', 'https://') data = util.get(url, random_ua=True) if data: return re.search(r'id=gibberish>([^<]+)', data).group(1) else: return None
def process(link, dest): tries = 3 while tries > 0: if tries < 3: time.sleep(0.3) tries -= 1 with open(dest, 'wb') as stream: res = util.download(link, stream, random_ua=True) if not res: return False with open(dest, 'r') as stream: if '<html' not in stream.read(1024): # Let's assume we downloaded the file. return True stream.seek(0) data = stream.read() break tries += 1 while tries > 0: if tries < 3: time.sleep(0.3) tries -= 1 if 'solvemedia.com/papi/' in data: logging.info('Detected SolveMedia captcha in "%s".', link) try: sv = SolveMedia(data) code = sv.get_code() challenge = sv.get_challenge(code) except: logging.exception('Captcha solving failed!') else: data = { 'adcopy_challenge': challenge, 'adcopy_response': code.replace(' ', '+') } data = util.post(link, data, random_ua=True) if not data: logging.error('Failed to submit captcha response!') elif re.search(r'(api\.recaptcha\.net|google\.com/recaptcha/api/)', data): logging.info('Detected ReCaptcha in "%s".', link) try: rc = ReCaptcha(data) info = re.search(r'challenge\?k=(.+?)"', data) if info: rc.id_ = info.group(1) rc.ask_for_code(link) data = rc.data else: logging.error('Can\'t find the ReCaptcha challenge!') except: logging.exception('Captcha solving failed!') else: info = MEDIAFIRE_EXTRACT.search(data) if not info: logging.error( 'Failed to parse MediaFire\'s response! Probably a CAPTCHA...' ) continue else: with open(dest, 'wb') as stream: return util.download(info.group(1), stream, random_ua=True)
def process(link, dest): tries = 3 while tries > 0: if tries < 3: time.sleep(0.3) tries -= 1 with open(dest, 'wb') as stream: res = util.download(link, stream, random_ua=True) if not res: return False with open(dest, 'r') as stream: if '<html' not in stream.read(1024): # Let's assume we downloaded the file. return True stream.seek(0) data = stream.read() break tries += 1 while tries > 0: if tries < 3: time.sleep(0.3) tries -= 1 if 'solvemedia.com/papi/' in data: logging.info('Detected SolveMedia captcha in "%s".', link) try: sv = SolveMedia(data) code = sv.get_code() challenge = sv.get_challenge(code) except: logging.exception('Captcha solving failed!') else: data = { 'adcopy_challenge': challenge, 'adcopy_response': code.replace(' ', '+') } data = util.post(link, data, random_ua=True) if not data: logging.error('Failed to submit captcha response!') elif re.search(r'(api\.recaptcha\.net|google\.com/recaptcha/api/)', data): logging.info('Detected ReCaptcha in "%s".', link) try: rc = ReCaptcha(data) info = re.search(r'challenge\?k=(.+?)"', data) if info: rc.id_ = info.group(1) rc.ask_for_code(link) data = rc.data else: logging.error('Can\'t find the ReCaptcha challenge!') except: logging.exception('Captcha solving failed!') else: info = MEDIAFIRE_EXTRACT.search(data) if not info: logging.error('Failed to parse MediaFire\'s response! Probably a CAPTCHA...') continue else: with open(dest, 'wb') as stream: return util.download(info.group(1), stream, random_ua=True)