1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-09 09:59:51 +01:00

Merge pull request #68 from vikstrous/mirror_sources

use an array of mirror sources and check only if necessary
This commit is contained in:
Viktor Stanchev 2015-09-20 13:35:49 -07:00
commit 218c3114e5
2 changed files with 41 additions and 33 deletions

View File

@ -228,40 +228,44 @@ def combine_configs(config, args):
def search_mirrors(pages, category, sort, action, search):
mirrors = OrderedDict()
mirrors['https://thepiratebay.mn'] = None
try:
req = request.Request('https://proxybay.co/list.txt',
headers=pirate.data.default_headers)
f = request.urlopen(req, timeout=pirate.data.default_timeout)
except IOError:
print('Could not fetch additional mirrors', color='WARN')
else:
if f.getcode() != 200:
raise IOError('The proxy bay responded with an error.')
for mirror in [i.decode('utf-8').strip() for i in f.readlines()][3:]:
mirrors[mirror] = None
for mirror in pirate.data.blacklist:
if mirror in mirrors:
del mirrors[mirror]
for mirror in mirrors.keys():
try:
print('Trying', mirror, end='... \n')
results = pirate.torrent.remote(
pages=pages,
category=pirate.torrent.parse_category(category),
sort=pirate.torrent.parse_sort(sort),
mode=action,
terms=search,
mirror=mirror
)
except (urllib.error.URLError, socket.timeout,
IOError, ValueError):
print('Failed', color='WARN')
mirror_sources = [None, 'https://proxybay.co/list.txt']
for mirror_source in mirror_sources:
mirrors = OrderedDict()
if mirror_source is None:
mirrors['https://thepiratebay.mn'] = None
else:
print('Ok', color='alt')
return results, mirror
try:
req = request.Request(mirror_source,
headers=pirate.data.default_headers)
f = request.urlopen(req, timeout=pirate.data.default_timeout)
except IOError:
print('Could not fetch additional mirrors', color='WARN')
else:
if f.getcode() != 200:
raise IOError('The proxy bay responded with an error.')
for mirror in [i.decode('utf-8').strip() for i in f.readlines()][3:]:
mirrors[mirror] = None
for mirror in pirate.data.blacklist:
if mirror in mirrors:
del mirrors[mirror]
for mirror in mirrors.keys():
try:
print('Trying', mirror, end='... \n')
results = pirate.torrent.remote(
pages=pages,
category=pirate.torrent.parse_category(category),
sort=pirate.torrent.parse_sort(sort),
mode=action,
terms=search,
mirror=mirror
)
except (urllib.error.URLError, socket.timeout,
IOError, ValueError):
print('Failed', color='WARN')
else:
print('Ok', color='alt')
return results, mirror
else:
print('No available mirrors :(', color='WARN')
return [], None

View File

@ -122,9 +122,13 @@ class TestPirate(unittest.TestCase):
with patch('urllib.request.urlopen', return_value=response_obj) as urlopen:
with patch('pirate.torrent.remote', return_value=[]) as remote:
results, mirror = pirate.pirate.search_mirrors(pages, category, sort, action, search)
self.assertEqual(results, [])
self.assertEqual(mirror, 'https://thepiratebay.mn')
remote.assert_called_once_with(pages=1, category=100, sort=10, mode='browse', terms=[], mirror='https://thepiratebay.mn')
with patch('pirate.torrent.remote', side_effect=[socket.timeout, []]) as remote:
results, mirror = pirate.pirate.search_mirrors(pages, category, sort, action, search)
self.assertEqual(results, [])
self.assertEqual(mirror, 'https://example.com')
remote.assert_has_calls([
call(pages=1, category=100, sort=10, mode='browse', terms=[], mirror='https://thepiratebay.mn'),
call(pages=1, category=100, sort=10, mode='browse', terms=[], mirror='https://example.com')