def test_not_raise_exception_when_add_hash_fields(self): parse_torrent_file(self.FILE, hash_fields={'info_hash': (20, False)}) with open(self.FILE, 'rb') as f: TorrentFileParser(f).hash_field('info_hash').parse() with open(self.FILE, 'rb') as f: data = f.read() decode(data, hash_fields={'info_hash': (20, False)})
def parse_response(self): self._request_obj.raise_for_status() content = self._response.get('content') if not isinstance(content, bytes): raise TypeError('response content must be instance of bytes') if self._request['action'] == 'SCRAPE': tmp = tp.decode(content, encoding='latin1') self._response['parsed'] = {} for info_hash, infos in tmp['files'].items(): self._response['parsed'][info_hash.encode( 'latin1').hex()] = infos return self._response['parsed']
def search(name): db = Database(DB_IP, get_open_port()) CONTACT = db.contact _dict = db[INDEX_KEY] try: torrent_keys = _dict[name] except: return flask.jsonify([]) metainfos = [] for tk in torrent_keys: value = _dict[tk] if not value: continue metainfos.append(torrent_parser.decode(value)) return flask.jsonify(metainfos)
def metainfo(client_id, ip, port): if flask.request.method == "POST": metainfo_encoded = flask.request.data metainfo_decoded = torrent_parser.decode(metainfo_encoded) name = metainfo_decoded["info"]["name"] infohash = get_infohash(metainfo_decoded) #Real app TRACKER.database[name + infohash + "metainfo"] = utils_tracker.assign( metainfo_decoded, name) TRACKER.database[name + infohash + "peers"] = utils_tracker.assign( { "ip": ip, "port": int(port), "id": client_id }, to_update=True) return ""
def get_torrent_details(data_dict): try: torrent_ditails = torrent_parser.decode(data_dict['data']) except torrent_parser.InvalidTorrentDataException: torrent_ditails = None except TypeError: torrent_ditails = None file_amount = 0 if torrent_ditails is None: file_amount = 0 torrent_ditails = {} elif 'files' in torrent_ditails['info'].keys(): file_amount = len([i for i in torrent_ditails['info']['files'] if get_ext(i.get('path')[-1]) in media_ext()]) elif 'name' in torrent_ditails['info'].keys(): file_amount = 1 torrent_ditails.update({'file_amount': file_amount}) torrent_ditails.update(data_dict) return torrent_ditails
def test_hash_raw_decode(self): data = b'd4:hash4:\xAA\xBB\xCC\xDDe' res = decode(data, hash_fields={'hash': (4, False)}, hash_raw=False) self.assertEqual(res['hash'], 'aabbccdd') res = decode(data, hash_fields={'hash': (4, False)}, hash_raw=True) self.assertEqual(res['hash'], b'\xAA\xBB\xCC\xDD')
def test_decode(self): self.assertEqual(decode(b'i12345e'), 12345)
def get_torrent(self, id): url = f"{make_ygg_url()}/engine/download_torrent?id={id}" response = self.scraper.session.get(url) return torrent_parser.decode(response.content)