def _get_torrent_hash(result): 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(u'Torrent without content', logger.ERROR) raise Exception('Torrent without content') bencoder = Bencoder() try: torrent_bdecode = bencoder.decode(result.content) except (BencodeDecodeError, Exception) as error: logger.log(u'Unable to bdecode torrent', logger.ERROR) logger.log(u'Error is: {0!r}'.format(error), logger.DEBUG) logger.log(u'Torrent bencoded data: {0!r}'.format(result.content), logger.DEBUG) raise try: info = torrent_bdecode["info"] except Exception: logger.log(u'Unable to find info field in torrent', logger.ERROR) raise try: result.hash = sha1(bencoder.encode(info)).hexdigest() except (BencodeEncodeError, Exception) as error: logger.log(u'Unable to bencode torrent info', logger.ERROR) logger.log(u'Error is: {0!r}'.format(error), logger.DEBUG) logger.log(u'Torrent bencoded data: {0!r}'.format(result.content), logger.DEBUG) raise return result
def test_can_decode_encoded(self, items): if items == "" or items == {}: return items = convert2utf8(items) encoded = Bencoder.encode(items) decoded = Bencoder.decode(encoded) self.assertEqual(items, decoded)
def _get_torrent_hash(result): 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(u'Torrent without content', logger.ERROR) raise Exception('Torrent without content') bencoder = Bencoder() try: torrent_bdecode = bencoder.decode(result.content) except (BencodeDecodeError, Exception) as error: logger.log(u'Unable to bdecode torrent', logger.ERROR) logger.log(u'Error is: {0!r}'.format(error), logger.DEBUG) logger.log( u'Torrent bencoded data: {0!r}'.format(result.content), logger.DEBUG) raise try: info = torrent_bdecode["info"] except Exception: logger.log(u'Unable to find info field in torrent', logger.ERROR) raise try: result.hash = sha1(bencoder.encode(info)).hexdigest() except (BencodeEncodeError, Exception) as error: logger.log(u'Unable to bencode torrent info', logger.ERROR) logger.log(u'Error is: {0!r}'.format(error), logger.DEBUG) logger.log( u'Torrent bencoded data: {0!r}'.format(result.content), logger.DEBUG) raise return result
def rewrite(path, mapping=None): """Rewrite paths in rtorrent session files.""" mapping = mapping or [] mapping = [tuple(m.split(':', 1)) for m in mapping] for dirpath, dirnames, filenames in os.walk(path): for filename in filenames: if filename.endswith('.rtorrent'): filepath = os.path.join(dirpath, filename) with open(filepath, 'rb') as f: data = Bencoder.decode(f.read()) print data for key in ('directory', 'loaded_file'): if key in data: value = data[key].decode('utf-8') for orig, new in mapping: if value.startswith(orig): print 'updating', value data[key] = value.replace(orig, new, 1).encode('utf-8') print 'to', data[key] break print data with open(filepath, 'wb') as f: f.write(Bencoder.encode(data))
def scan(path): """Scan rtorrent session files for common paths.""" counts = Counter() for dirpath, dirnames, filenames in os.walk(path): for filename in filenames: if filename.endswith('.rtorrent'): filepath = os.path.join(dirpath, filename) with open(filepath, 'rb') as f: data = Bencoder.decode(f.read()) counts.update(get_ancestors(data.get('directory', ''))) counts.update(get_ancestors(data.get('loaded_file', ''))) for p, c in counts.most_common(): print p, ':', c
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') bencoder = Bencoder() try: torrent_bdecode = bencoder.decode(result.content) except (BencodeDecodeError, 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}'.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(bencoder.encode(info)).hexdigest() except (BencodeEncodeError, 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}'.format(result.content), logger.DEBUG) raise return result
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._getRSSData()['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: Bencoder().decode(torrent_file) except (BencodeDecodeError, 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 test_critcal(self): strs = Bencoder.decode("llee") self.assertEqual(strs, [[]])
def test_dict_decode(self): """test dict decode""" strs = Bencoder.decode("d4:abcdli34eee") self.assertEqual(strs.get("abcd"), [34])
def test_integer_decode(self): """test integer decode""" strs = Bencoder.decode("i34e") self.assertEqual(strs, 34)
def test_list_decode(self): """tests list decode""" strs = Bencoder.decode("li34ee") self.assertEqual(strs, [34])
def test_string_decode(self): """tests string decode""" strs = Bencoder.decode("1:a") self.assertEqual(strs, "a")
def test_dict_encoding(self): """tests dictionary encoding""" strs = Bencoder.encode(dict(a="1")) self.assertEqual(strs, "d1:a1:1e")
def test_list_encoding(self): """tests list encoding""" strs = Bencoder.encode([1, 2]) self.assertEqual(strs, "li1ei2ee")
def test_integer_encoding(self): """tests integer encoding""" strs = Bencoder.encode(1) self.assertEqual(strs, "i1e")
def test_string_encoding(self): """tests string encoding""" strs = Bencoder.encode("announce") self.assertEqual(strs, "8:announce")