mirror of
https://github.com/vikstrous/pirate-get
synced 2025-01-10 10:04:21 +01:00
Merge pull request #74 from vikstrous/fix
fix bug with / or \ in torrent names, fix #73
This commit is contained in:
commit
0d202d898b
@ -8,10 +8,6 @@ class BayParser(parser.HTMLParser):
|
|||||||
state = 'looking'
|
state = 'looking'
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
def __init__(self, q):
|
|
||||||
super().__init__(self)
|
|
||||||
self.q = q.lower()
|
|
||||||
|
|
||||||
def handle_starttag(self, tag, attrs):
|
def handle_starttag(self, tag, attrs):
|
||||||
if tag == 'title':
|
if tag == 'title':
|
||||||
self.state = 'title'
|
self.state = 'title'
|
||||||
@ -36,6 +32,7 @@ class BayParser(parser.HTMLParser):
|
|||||||
|
|
||||||
def search(db, terms):
|
def search(db, terms):
|
||||||
xml = open(db).readlines()
|
xml = open(db).readlines()
|
||||||
parser = BayParser(' '.join(terms))
|
parser = BayParser()
|
||||||
|
parser.q = (' '.join(terms)).lower()
|
||||||
parser.feed(''.join(xml))
|
parser.feed(''.join(xml))
|
||||||
return parser.results
|
return parser.results
|
||||||
|
@ -160,13 +160,13 @@ def get_torrent(info_hash):
|
|||||||
return torrent.read()
|
return torrent.read()
|
||||||
|
|
||||||
|
|
||||||
# TODO: handle slashes in torrent names
|
|
||||||
def save_torrents(printer, chosen_links, results, folder):
|
def save_torrents(printer, chosen_links, results, folder):
|
||||||
for link in chosen_links:
|
for link in chosen_links:
|
||||||
magnet = results[link]['magnet']
|
magnet = results[link]['magnet']
|
||||||
name = re.search(r'dn=([^\&]*)', magnet)
|
name = re.search(r'dn=([^\&]*)', magnet)
|
||||||
torrent_name = parse.unquote(name.group(1)).replace('+', ' ')
|
torrent_name = parse.unquote(name.group(1)).replace('+', ' ')
|
||||||
info_hash = int(re.search(r'btih:([a-f0-9]{40})', magnet).group(1), 16)
|
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')
|
file = os.path.join(folder, torrent_name + '.torrent')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -178,13 +178,13 @@ def save_torrents(printer, chosen_links, results, folder):
|
|||||||
printer.print('Saved {:X} in {}'.format(info_hash, file))
|
printer.print('Saved {:X} in {}'.format(info_hash, file))
|
||||||
|
|
||||||
|
|
||||||
# TODO: handle slashes in torrent names
|
|
||||||
def save_magnets(printer, chosen_links, results, folder):
|
def save_magnets(printer, chosen_links, results, folder):
|
||||||
for link in chosen_links:
|
for link in chosen_links:
|
||||||
magnet = results[link]['magnet']
|
magnet = results[link]['magnet']
|
||||||
name = re.search(r'dn=([^\&]*)', magnet)
|
name = re.search(r'dn=([^\&]*)', magnet)
|
||||||
torrent_name = parse.unquote(name.group(1)).replace('+', ' ')
|
torrent_name = parse.unquote(name.group(1)).replace('+', ' ')
|
||||||
info_hash = int(re.search(r'btih:([a-f0-9]{40})', magnet).group(1), 16)
|
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')
|
file = os.path.join(folder, torrent_name + '.magnet')
|
||||||
|
|
||||||
printer.print('Saved {:X} in {}'.format(info_hash, file))
|
printer.print('Saved {:X} in {}'.format(info_hash, file))
|
||||||
|
@ -93,10 +93,10 @@ class TestTorrent(unittest.TestCase):
|
|||||||
@patch('pirate.torrent.get_torrent')
|
@patch('pirate.torrent.get_torrent')
|
||||||
def test_save_torrents(self, get_torrent):
|
def test_save_torrents(self, get_torrent):
|
||||||
with patch('pirate.torrent.open', mock.mock_open(), create=True) as open_:
|
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')
|
pirate.torrent.save_torrents(MagicMock(Printer), [0], [{'magnet':magnet}], 'path')
|
||||||
get_torrent.assert_called_once_with(293294978876299923284263767676068334936407502135)
|
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()))
|
@patch('pirate.torrent.get_torrent', side_effect=urllib.error.HTTPError('', '', '', '', io.StringIO()))
|
||||||
def test_save_torrents_fail(self, get_torrent):
|
def test_save_torrents_fail(self, get_torrent):
|
||||||
@ -105,9 +105,9 @@ class TestTorrent(unittest.TestCase):
|
|||||||
|
|
||||||
def test_save_magnets(self):
|
def test_save_magnets(self):
|
||||||
with patch('pirate.torrent.open', mock.mock_open(), create=True) as open_:
|
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')
|
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')
|
@patch('urllib.request.urlopen')
|
||||||
def test_get_torrent(self, urlopen):
|
def test_get_torrent(self, urlopen):
|
||||||
|
Loading…
Reference in New Issue
Block a user