def test_convert_torrent_to_json_tuple(self): """ Test whether the conversion from db torrent tuple to json works """ input_tuple = (1, '2', 'abc', 4, 5, 6, 7, 8, 0, 0.123) output = { 'id': 1, 'infohash': '2'.encode('hex'), 'name': 'abc', 'size': 4, 'category': 5, 'num_seeders': 6, 'num_leechers': 7, 'last_tracker_check': 8, 'relevance_score': 0.123 } self.assertEqual(convert_search_torrent_to_json(input_tuple), output) input_tuple = (1, '2', None, 4, 5, 6, 7, 8, 0, 0.123) output['name'] = 'Unnamed torrent' self.assertEqual(convert_search_torrent_to_json(input_tuple), output) input_tuple = (1, '2', ' \t\n\n\t \t', 4, 5, 6, 7, 8, 0, 0.123) output['name'] = 'Unnamed torrent' self.assertEqual(convert_search_torrent_to_json(input_tuple), output)
def test_convert_torrent_to_json_dict(self): """ Test whether the conversion from remote torrent dict to json works """ input = { 'torrent_id': 42, 'infohash': 'a', 'name': 'test torrent', 'length': 43, 'category': 'other', 'num_seeders': 1, 'num_leechers': 2 } output = { 'id': 42, 'infohash': 'a'.encode('hex'), 'name': 'test torrent', 'size': 43, 'category': 'other', 'num_seeders': 1, 'num_leechers': 2, 'last_tracker_check': 0 } self.assertEqual(convert_search_torrent_to_json(input), output) input['name'] = None output['name'] = 'Unnamed torrent' self.assertEqual(convert_search_torrent_to_json(input), output) input['name'] = ' \t\n\n\t \t' output['name'] = 'Unnamed torrent' self.assertEqual(convert_search_torrent_to_json(input), output)
def test_convert_torrent_to_json_tuple(self): """ Test whether the conversion from db torrent tuple to json works """ input_tuple = (1, '2', 'abc', 4, 5, 6, 7, 8, 0, 0.123) output = {'id': 1, 'infohash': '2'.encode('hex'), 'name': 'abc', 'size': 4, 'category': 5, 'num_seeders': 6, 'num_leechers': 7, 'last_tracker_check': 8, 'relevance_score': 0.123} self.assertEqual(convert_search_torrent_to_json(input_tuple), output) input_tuple = (1, '2', None, 4, 5, 6, 7, 8, 0, 0.123) output['name'] = 'Unnamed torrent' self.assertEqual(convert_search_torrent_to_json(input_tuple), output) input_tuple = (1, '2', ' \t\n\n\t \t', 4, 5, 6, 7, 8, 0, 0.123) output['name'] = 'Unnamed torrent' self.assertEqual(convert_search_torrent_to_json(input_tuple), output)
def on_search_results_torrents(self, subject, changetype, objectID, results): """ Returns the torrent search results over the events endpoint. """ query = ' '.join(results['keywords']) for torrent in results['result_list']: torrent_json = convert_search_torrent_to_json(torrent) torrent_name = torrent_json['name'] torrent_json['relevance_score'] = torrent_json['relevance_score'] if 'relevance_score' in torrent_json \ else self.session.lm.torrent_db.relevance_score_remote_torrent(torrent_name) if self.session.config.get_family_filter_enabled( ) and torrent_json['category'] == 'xxx': continue if 'infohash' in torrent_json and torrent_json[ 'infohash'] not in self.infohashes_sent: self.write_data({ "type": "search_result_torrent", "event": { "query": query, "result": torrent_json } }) self.infohashes_sent.add(torrent_json['infohash'])
def test_convert_torrent_to_json_dict(self): """ Test whether the conversion from remote torrent dict to json works """ input = {'torrent_id': 42, 'infohash': 'a', 'name': 'test torrent', 'length': 43, 'category': 'other', 'num_seeders': 1, 'num_leechers': 2} output = {'id': 42, 'infohash': 'a'.encode('hex'), 'name': 'test torrent', 'size': 43, 'category': 'other', 'num_seeders': 1, 'num_leechers': 2, 'last_tracker_check': 0} self.assertEqual(convert_search_torrent_to_json(input), output) input['name'] = None output['name'] = 'Unnamed torrent' self.assertEqual(convert_search_torrent_to_json(input), output) input['name'] = ' \t\n\n\t \t' output['name'] = 'Unnamed torrent' self.assertEqual(convert_search_torrent_to_json(input), output)
def test_convert_torrent_to_json_dict(self): """ Test whether the conversion from remote torrent dict to json works """ mocked_db = MockObject() mocked_db.latest_matchinfo_torrent = None self.session.open_dbhandler = lambda _: mocked_db input = {'torrent_id': 42, 'infohash': 'a', 'name': 'test torrent', 'length': 43, 'category': 'other', 'num_seeders': 1, 'num_leechers': 2} output = {'id': 42, 'infohash': 'a'.encode('hex'), 'name': 'test torrent', 'size': 43, 'category': 'other', 'num_seeders': 1, 'num_leechers': 2, 'last_tracker_check': 0, 'relevance_score': 0.0} self.assertEqual(convert_search_torrent_to_json(input), output) input['name'] = None output['name'] = 'Unnamed torrent' self.assertEqual(convert_search_torrent_to_json(input), output) input['name'] = ' \t\n\n\t \t' output['name'] = 'Unnamed torrent' self.assertEqual(convert_search_torrent_to_json(input), output)
def on_search_results_torrents(self, subject, changetype, objectID, results): """ Returns the torrent search results over the events endpoint. """ query = ' '.join(results['keywords']) for torrent in results['result_list']: torrent_json = convert_search_torrent_to_json(torrent) torrent_name = torrent_json['name'] torrent_json['relevance_score'] = torrent_json['relevance_score'] if 'relevance_score' in torrent_json \ else self.session.lm.torrent_db.relevance_score_remote_torrent(torrent_name) if self.session.config.get_family_filter_enabled() and torrent_json['category'] == 'xxx': continue if 'infohash' in torrent_json and torrent_json['infohash'] not in self.infohashes_sent: self.write_data({"type": "search_result_torrent", "event": {"query": query, "result": torrent_json}}) self.infohashes_sent.add(torrent_json['infohash'])