From b1d026b5f6afd1596d35ffe34a5d1393ea924111 Mon Sep 17 00:00:00 2001 From: Viktor Stanchev Date: Thu, 8 Oct 2015 20:33:13 -0700 Subject: [PATCH] fix bug with / or \ in torrent names, fix #73 --- pirate/local.py | 7 ++----- pirate/torrent.py | 4 ++-- tests/test_torrent.py | 8 ++++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/pirate/local.py b/pirate/local.py index 2d62382..8163539 100644 --- a/pirate/local.py +++ b/pirate/local.py @@ -8,10 +8,6 @@ class BayParser(parser.HTMLParser): state = 'looking' results = [] - def __init__(self, q): - super().__init__(self) - self.q = q.lower() - def handle_starttag(self, tag, attrs): if tag == 'title': self.state = 'title' @@ -36,6 +32,7 @@ class BayParser(parser.HTMLParser): def search(db, terms): xml = open(db).readlines() - parser = BayParser(' '.join(terms)) + parser = BayParser() + parser.q = (' '.join(terms)).lower() parser.feed(''.join(xml)) return parser.results diff --git a/pirate/torrent.py b/pirate/torrent.py index 039157b..e59d6d4 100644 --- a/pirate/torrent.py +++ b/pirate/torrent.py @@ -160,13 +160,13 @@ def get_torrent(info_hash): return torrent.read() -# TODO: handle slashes in torrent names def save_torrents(printer, chosen_links, results, folder): for link in chosen_links: magnet = results[link]['magnet'] name = re.search(r'dn=([^\&]*)', magnet) torrent_name = parse.unquote(name.group(1)).replace('+', ' ') info_hash = int(re.search(r'btih:([a-f0-9]{40})', magnet).group(1), 16) + torrent_name = torrent_name.replace('/', '_').replace('\\', '_') file = os.path.join(folder, torrent_name + '.torrent') try: @@ -178,13 +178,13 @@ def save_torrents(printer, chosen_links, results, folder): printer.print('Saved {:X} in {}'.format(info_hash, file)) -# TODO: handle slashes in torrent names def save_magnets(printer, chosen_links, results, folder): for link in chosen_links: magnet = results[link]['magnet'] name = re.search(r'dn=([^\&]*)', magnet) torrent_name = parse.unquote(name.group(1)).replace('+', ' ') info_hash = int(re.search(r'btih:([a-f0-9]{40})', magnet).group(1), 16) + torrent_name = torrent_name.replace('/', '_').replace('\\', '_') file = os.path.join(folder, torrent_name + '.magnet') printer.print('Saved {:X} in {}'.format(info_hash, file)) diff --git a/tests/test_torrent.py b/tests/test_torrent.py index 5ee3ff5..66a50dd 100755 --- a/tests/test_torrent.py +++ b/tests/test_torrent.py @@ -93,10 +93,10 @@ class TestTorrent(unittest.TestCase): @patch('pirate.torrent.get_torrent') def test_save_torrents(self, get_torrent): with patch('pirate.torrent.open', mock.mock_open(), create=True) as open_: - magnet = 'magnet:?xt=urn:btih:335fcd3cfbecc85554616d73de888033c6c16d37&dn=Test+Drive+Unlimited+%5BPC+Version%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969' + magnet = 'magnet:?xt=urn:btih:335fcd3cfbecc85554616d73de888033c6c16d37&dn=Test+Drive+Unl\im/ited+%5BPC+Version%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969' pirate.torrent.save_torrents(MagicMock(Printer), [0], [{'magnet':magnet}], 'path') get_torrent.assert_called_once_with(293294978876299923284263767676068334936407502135) - open_.assert_called_once_with('path/Test Drive Unlimited [PC Version].torrent', 'wb') + open_.assert_called_once_with('path/Test Drive Unl_im_ited [PC Version].torrent', 'wb') @patch('pirate.torrent.get_torrent', side_effect=urllib.error.HTTPError('', '', '', '', io.StringIO())) def test_save_torrents_fail(self, get_torrent): @@ -105,9 +105,9 @@ class TestTorrent(unittest.TestCase): def test_save_magnets(self): with patch('pirate.torrent.open', mock.mock_open(), create=True) as open_: - magnet = 'magnet:?xt=urn:btih:335fcd3cfbecc85554616d73de888033c6c16d37&dn=Test+Drive+Unlimited+%5BPC+Version%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969' + magnet = 'magnet:?xt=urn:btih:335fcd3cfbecc85554616d73de888033c6c16d37&dn=Test+Drive+Unl\im/ited+%5BPC+Version%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969' pirate.torrent.save_magnets(MagicMock(Printer), [0], [{'magnet':magnet}], 'path') - open_.assert_called_once_with('path/Test Drive Unlimited [PC Version].magnet', 'w') + open_.assert_called_once_with('path/Test Drive Unl_im_ited [PC Version].magnet', 'w') @patch('urllib.request.urlopen') def test_get_torrent(self, urlopen):