def test_bdecode(self): """ Test the custom bdecode function """ bencoded_data = b'd5:hello5:world7:numbersli1ei2eeeEXTRA_DATA_HERE' self.assertEqual(helpers.bdecode(bencoded_data, True), {'hello': b'world', 'numbers': [1, 2]}) self.assertRaisesRegexp(BTFailure, 'data after valid prefix', helpers.bdecode, bencoded_data, False) self.assertRaisesRegexp(BTFailure, 'not a valid bencoded string', helpers.bdecode, b'Heythere', False)
def validateRSS(self): # pylint: disable=too-many-return-statements try: if self.cookies: cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$') if not cookie_validator.match(self.cookies): return False, 'Cookie is not correctly formatted: {0}'.format( self.cookies) add_dict_to_cookiejar( self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';'))) # pylint: disable=protected-access # Access to a protected member of a client class data = self.cache._get_rss_data()['entries'] if not data: return False, 'No items found in the RSS feed {0}'.format( self.url) title, url = self._get_title_and_url(data[0]) if not title: return False, 'Unable to get title from first item' if not url: return False, 'Unable to get torrent url from first item' if url.startswith('magnet:') and re.search( r'urn:btih:([\w]{32,40})', url): return True, 'RSS feed Parsed correctly' else: torrent_file = self.get_url(url, returns='content') try: helpers.bdecode(torrent_file, True) except (BTFailure, Exception) as error: self.dumpHTML(torrent_file) return False, 'Torrent link is not a valid torrent file: {0}'.format( error) return True, 'RSS feed Parsed correctly' except Exception as error: return False, 'Error when trying to load RSS: {0}'.format( ex(error))
def validateRSS(self): # pylint: disable=too-many-return-statements try: if self.cookies: cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$') if not cookie_validator.match(self.cookies): return False, 'Cookie is not correctly formatted: {0}'.format(self.cookies) add_dict_to_cookiejar(self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';'))) # pylint: disable=protected-access # Access to a protected member of a client class data = self.cache._get_rss_data()['entries'] if not data: return False, 'No items found in the RSS feed {0}'.format(self.url) title, url = self._get_title_and_url(data[0]) if not title: return False, 'Unable to get title from first item' if not url: return False, 'Unable to get torrent url from first item' if url.startswith('magnet:') and re.search(r'urn:btih:([\w]{32,40})', url): return True, 'RSS feed Parsed correctly' else: torrent_file = self.get_url(url, returns='content') try: helpers.bdecode(torrent_file, True) except (BTFailure, Exception) as error: self.dumpHTML(torrent_file) return False, 'Torrent link is not a valid torrent file: {0}'.format(error) return True, 'RSS feed Parsed correctly' except Exception as error: return False, 'Error when trying to load RSS: {0}'.format(ex(error))
def _get_torrent_hash(result): """ Gets the torrent hash from either the magnet or torrent file content params: :result: an instance of the searchResult class """ if result.url.startswith('magnet'): result.hash = re.findall(r'urn:btih:([\w]{32,40})', result.url)[0] if len(result.hash) == 32: result.hash = b16encode(b32decode(result.hash)).lower() else: if not result.content: logger.log('Torrent without content', logger.ERROR) raise Exception('Torrent without content') try: torrent_bdecode = helpers.bdecode(result.content, True) except (bencode.BTL.BTFailure, Exception) as error: logger.log('Unable to bdecode torrent', logger.ERROR) logger.log('Error is: {0}'.format(error), logger.INFO) logger.log( 'Torrent bencoded data: {0!r}'.format(result.content), logger.INFO) raise try: info = torrent_bdecode[b'info'] except Exception: logger.log('Unable to find info field in torrent', logger.ERROR) logger.log( 'Torrent bencoded data: {0!r}'.format(result.content), logger.INFO) raise try: result.hash = sha1(bencode.bencode(info)).hexdigest() logger.log('Result Hash is {0}'.format(result.hash), logger.DEBUG) except (bencode.BTL.BTFailure, Exception) as error: logger.log('Unable to bencode torrent info', logger.ERROR) logger.log('Error is: {0}'.format(error), logger.INFO) logger.log( 'Torrent bencoded data: {0!r}'.format(result.content), logger.INFO) raise return result
def _get_torrent_hash(result): """ Gets the torrent hash from either the magnet or torrent file content params: :result: an instance of the searchResult class """ if result.url.startswith('magnet'): result.hash = re.findall(r'urn:btih:([\w]{32,40})', result.url)[0] if len(result.hash) == 32: result.hash = b16encode(b32decode(result.hash)).lower() else: if not result.content: logger.log('Torrent without content', logger.ERROR) raise Exception('Torrent without content') try: torrent_bdecode = helpers.bdecode(result.content, True) except (bencode.BTL.BTFailure, Exception) as error: logger.log('Unable to bdecode torrent', logger.ERROR) logger.log('Error is: {0}'.format(error), logger.DEBUG) # logger.log('Torrent bencoded data: {0!r}'.format(result.content), logger.DEBUG) raise try: info = torrent_bdecode[b'info'] except Exception: logger.log('Unable to find info field in torrent', logger.ERROR) raise try: result.hash = sha1(bencode.bencode(info)).hexdigest() logger.log('Result Hash is {0}'.format(result.hash), logger.DEBUG) except (bencode.BTL.BTFailure, Exception) as error: logger.log('Unable to bencode torrent info', logger.ERROR) logger.log('Error is: {0}'.format(error), logger.DEBUG) # logger.log('Torrent bencoded data: {0!r}'.format(result.content), logger.DEBUG) raise return result