def test_dict_parse_correct_string_string(self): key = b"some_key" value = b"some_value" expected = {key: value} source = b"d8:" + key + b"10:" + value + b"e" content = BencodeParser.parse(source) self.assertDictEqual(content[0], expected)
def __init__(self, torrent_file_path: str): if not isinstance(torrent_file_path, str): raise TypeError("Loader takes string as file_path") _check_file_correctness(torrent_file_path) threading.Thread.__init__(self) self.name = "Loader." + self.name self.log = Loader._get_logger() self.log.info("\n\nStart " + Path(torrent_file_path).name) self.torrent_file_path = torrent_file_path self.save_directory_path = None try: source = _read_source_from_file(torrent_file_path) except Exception as ex: ex = "Exception during reading torrent-file: " + str(ex) self.log.error(ex) print("!!! " + ex) return try: content = BencodeParser.parse(source)[0] except Exception as ex: ex = "Exception during parsing torrent-file: " + str(ex) self.log.error(ex) print("!!! " + ex) return self.log.info("INIT. Parsed torrent file successfully. " "File path: %s" % self.torrent_file_path) self.torrent = TorrentMeta(content, self.log.name) self.log.info("INIT. Interpreted torrent file successfully") self.allocator = None self.trackers = TrackersConnector(self) self.is_working = False self.is_finished = False self.start_download_time = None self.finish_download_time = None
def test_list_parse_correct_two_strings(self): str1 = b"abcde" str2 = b"something" expected = [str1, str2] source = b"l5:" + str1 + b"9:" + str2 + b"e" content = BencodeParser.parse(source) self.assertListEqual(content[0], expected)
def test_dict_parse_correct_dict_value(self): key_1 = b"key1" key_2 = b"key2" value = b"inner_value" expected = {key_1: {key_2: value}} source = b"d" + b"4:" + key_1 + \ b"d" + b"4:" + key_2 + b"11:" + value + b"ee" content = BencodeParser.parse(source) self.assertDictEqual(content[0], expected)
def test_dict_parse_correct_list_value(self): key = b"key" value_1 = b"some_value" value_2 = b"one_more_value" expected = {key: [value_1, value_2]} source = b"d3:" + key + b"l10:" + value_1 + \ b"14:" + value_2 + b"ee" content = BencodeParser.parse(source) self.assertDictEqual(content[0], expected)
def test_dict_parse_correct_two_pairs(self): key_1 = b"key1" key_2 = b"key2" value_1 = b"value1" value_2 = b"value2" expected = {key_1: value_1, key_2: value_2} source = b"d" + b"4:" + key_1 + b"6:" + value_1 + \ b"4:" + key_2 + b"6:" + value_2 + b"e" content = BencodeParser.parse(source) self.assertDictEqual(content[0], expected)
def _connect_http(tracker_url, params, logger: logging.Logger, loader): try: response = requests.get(tracker_url, params=params) try: tracker_answer = BencodeParser.parse(response.content)[0] return tracker_answer except Exception as ex: logger.error("Exception in parsing loader '%s' answer: %s\n %s" % (tracker_url.decode(), str(ex), response.content)) return None except Exception as ex: logger.error("Exception during HTTP connection with loader '%s':" "\n %s" % (tracker_url.decode(), str(ex))) return None
def test_list_parse_correct_int(self): expected = [125] source = b'li125ee' content = BencodeParser.parse(source) self.assertListEqual(content[0], expected)
def test_string_parse_correct(self): expected = "abcde" source = "5:" + expected content = BencodeParser.parse(source) self.assertEqual(content[0], expected.encode())
def test_int_parse_zero(self): expected = 0 source = b'i' + str(expected).encode() + b'e' content = BencodeParser.parse(source) self.assertEqual(content[0], expected)
def test_string_parse_empty(self): expected = b"" source = b"0:" + expected content = BencodeParser.parse(source) self.assertEqual(content[0], expected)
def test_list_parse_empty_correct(self): expected = [] source = b'le' content = BencodeParser.parse(source) self.assertListEqual(content[0], expected)
def test_dict_parse_correct_string_int(self): expected = {b"key123": 456} source = b"d" + b"6:key123" + b"i456e" + b"e" content = BencodeParser.parse(source) self.assertDictEqual(content[0], expected)
def test_list_parse_correct_int_string_and_inner_list(self): expected = [128, b"hello", [32, b"hi"]] source = b"l" + b"i128e" + b"5:hello" + \ b"l" + b"i32e" + b"2:hi" + b"e" + b"e" content = BencodeParser.parse(source) self.assertListEqual(content[0], expected)
def test_list_parse_correct_two_inner_list(self): expected = [[1], [2]] source = b"l" + b"li1ee" + b"li2ee" + b"e" content = BencodeParser.parse(source) self.assertListEqual(content[0], expected)
def test_list_parse_correct_two_integers(self): expected = [123, -15] source = b"l" + b"i123e" + b"i-15e" + b"e" content = BencodeParser.parse(source) self.assertListEqual(content[0], expected)
def test_dict_parse_empty_correct(self): expected = {} source = b'de' content = BencodeParser.parse(source) self.assertDictEqual(content[0], expected)
def test_list_parse_correct_string(self): string = b"abcde" expected = [string] source = b"l5:" + string + b"e" content = BencodeParser.parse(source) self.assertListEqual(content[0], expected)
def test_list_parse_correct_two_elements_string_and_int(self): string = b"abcde" expected = [string, 100] source = b"l5:" + string + b"i100e" + b"e" content = BencodeParser.parse(source) self.assertListEqual(content[0], expected)