def _verify_download(self, file_name=None): """Validate torrent file.""" if not file_name or not os.path.isfile(file_name): return False try: with open(file_name, 'rb') as f: meta_info = bencode.bdecode(f.read()) return 'info' in meta_info and meta_info['info'] except BTFailure as e: log.debug('Failed to validate torrent file: {name}. Error: {error}', {'name': file_name, 'error': e}) remove_file_failed(file_name) log.debug('{result} is not a valid torrent file', {'result': file_name}) return False
def _verify_download(self, file_name=None): """Validate torrent file.""" if not file_name or not os.path.isfile(file_name): return False try: with open(file_name, 'rb') as f: # `bencode.bdecode` is monkeypatched in `medusa.init` meta_info = bdecode(f.read(), allow_extra_data=True) return 'info' in meta_info and meta_info['info'] except BencodeDecodeError as error: log.debug('Failed to validate torrent file: {name}. Error: {error}', {'name': file_name, 'error': error}) remove_file_failed(file_name) log.debug('{result} is not a valid torrent file', {'result': file_name}) return False
def download_result(self, result): """Save the result to disk.""" # check for auth if not self.login(): return False urls, filename = self._make_url(result) for url in urls: # Search results don't return torrent files directly, # it returns show sheets so we must parse showSheet to access torrent. response = self.get_url(url, returns='response') url_torrent = re.search( r'http://tumejorserie.com/descargar/.+\.torrent', response.text, re.DOTALL).group() if url_torrent.startswith('http'): self.headers.update( {'Referer': '/'.join(url_torrent.split('/')[:3]) + '/'}) log.info('Downloading a result from {0}', url) if helpers.download_file(url_torrent, filename, session=self.session, headers=self.headers): if self._verify_download(filename): log.info('Saved result to {0}', filename) return True else: log.warning('Could not download {0}', url) helpers.remove_file_failed(filename) if urls: log.warning('Failed to download any results') return False