mirror of
https://github.com/vikstrous/pirate-get
synced 2025-01-10 10:04:21 +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:
commit
218c3114e5
@ -228,40 +228,44 @@ def combine_configs(config, args):
|
|||||||
|
|
||||||
|
|
||||||
def search_mirrors(pages, category, sort, action, search):
|
def search_mirrors(pages, category, sort, action, search):
|
||||||
mirrors = OrderedDict()
|
mirror_sources = [None, 'https://proxybay.co/list.txt']
|
||||||
mirrors['https://thepiratebay.mn'] = None
|
for mirror_source in mirror_sources:
|
||||||
try:
|
mirrors = OrderedDict()
|
||||||
req = request.Request('https://proxybay.co/list.txt',
|
if mirror_source is None:
|
||||||
headers=pirate.data.default_headers)
|
mirrors['https://thepiratebay.mn'] = None
|
||||||
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:
|
else:
|
||||||
print('Ok', color='alt')
|
try:
|
||||||
return results, mirror
|
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:
|
else:
|
||||||
print('No available mirrors :(', color='WARN')
|
print('No available mirrors :(', color='WARN')
|
||||||
return [], None
|
return [], None
|
||||||
|
@ -122,9 +122,13 @@ class TestPirate(unittest.TestCase):
|
|||||||
with patch('urllib.request.urlopen', return_value=response_obj) as urlopen:
|
with patch('urllib.request.urlopen', return_value=response_obj) as urlopen:
|
||||||
with patch('pirate.torrent.remote', return_value=[]) as remote:
|
with patch('pirate.torrent.remote', return_value=[]) as remote:
|
||||||
results, mirror = pirate.pirate.search_mirrors(pages, category, sort, action, search)
|
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')
|
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:
|
with patch('pirate.torrent.remote', side_effect=[socket.timeout, []]) as remote:
|
||||||
results, mirror = pirate.pirate.search_mirrors(pages, category, sort, action, search)
|
results, mirror = pirate.pirate.search_mirrors(pages, category, sort, action, search)
|
||||||
|
self.assertEqual(results, [])
|
||||||
|
self.assertEqual(mirror, 'https://example.com')
|
||||||
remote.assert_has_calls([
|
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://thepiratebay.mn'),
|
||||||
call(pages=1, category=100, sort=10, mode='browse', terms=[], mirror='https://example.com')
|
call(pages=1, category=100, sort=10, mode='browse', terms=[], mirror='https://example.com')
|
||||||
|
Loading…
Reference in New Issue
Block a user