mirror of
https://github.com/vikstrous/pirate-get
synced 2025-01-24 12:14:20 +01:00
Improve connection error handling
This commit is contained in:
parent
17a035ff7c
commit
9a13c0308e
@ -33,6 +33,7 @@ import urllib.parse as parse
|
|||||||
|
|
||||||
from html.parser import HTMLParser
|
from html.parser import HTMLParser
|
||||||
from urllib.error import URLError
|
from urllib.error import URLError
|
||||||
|
from socket import timeout
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
categories = {
|
categories = {
|
||||||
@ -106,7 +107,7 @@ class NoRedirection(request.HTTPErrorProcessor):
|
|||||||
|
|
||||||
|
|
||||||
# create a subclass and override the handler methods
|
# create a subclass and override the handler methods
|
||||||
class MyHTMLParser(HTMLParser):
|
class BayParser(HTMLParser):
|
||||||
title = ''
|
title = ''
|
||||||
q = ''
|
q = ''
|
||||||
state = 'looking'
|
state = 'looking'
|
||||||
@ -185,7 +186,6 @@ def remote(args, mirror):
|
|||||||
else:
|
else:
|
||||||
sort = '99'
|
sort = '99'
|
||||||
print('Invalid sort ignored', color='WARN')
|
print('Invalid sort ignored', color='WARN')
|
||||||
|
|
||||||
# Catch the Ctrl-C exception and exit cleanly
|
# Catch the Ctrl-C exception and exit cleanly
|
||||||
try:
|
try:
|
||||||
sizes = []
|
sizes = []
|
||||||
@ -221,7 +221,7 @@ def remote(args, mirror):
|
|||||||
|
|
||||||
# check for a blocked mirror
|
# check for a blocked mirror
|
||||||
no_results = re.search(r'"No hits\.', res)
|
no_results = re.search(r'"No hits\.', res)
|
||||||
if found == [] and not no_results is None:
|
if found == [] and no_results is None:
|
||||||
# Contradiction - we found no results,
|
# Contradiction - we found no results,
|
||||||
# but the page didn't say there were no results.
|
# but the page didn't say there were no results.
|
||||||
# The page is probably not actually the pirate bay,
|
# The page is probably not actually the pirate bay,
|
||||||
@ -263,13 +263,11 @@ def remote(args, mirror):
|
|||||||
return res_l, sizes, uploaded, identifiers
|
return res_l, sizes, uploaded, identifiers
|
||||||
|
|
||||||
|
|
||||||
def local(args):
|
def local(db, search):
|
||||||
xml_str = ''
|
xml = open(db).read()
|
||||||
with open(args.database, 'r') as f:
|
parser = BaParser(search)
|
||||||
xml_str += f.read()
|
parser.feed(xml)
|
||||||
htmlparser = MyHTMLParser(args.q)
|
return parser.results
|
||||||
htmlparser.feed(xml_str)
|
|
||||||
return htmlparser.results
|
|
||||||
|
|
||||||
|
|
||||||
# load user options, to override default ones
|
# load user options, to override default ones
|
||||||
@ -405,7 +403,7 @@ def main():
|
|||||||
parser.add_argument('--custom', dest='command',
|
parser.add_argument('--custom', dest='command',
|
||||||
help='call custom command, %%s will be replaced with'
|
help='call custom command, %%s will be replaced with'
|
||||||
'the url')
|
'the url')
|
||||||
parser.add_argument('--local', dest='database',
|
parser.add_argument('-L', '--local', dest='database',
|
||||||
help='an xml file containing the Pirate Bay database')
|
help='an xml file containing the Pirate Bay database')
|
||||||
parser.add_argument('-p', dest='pages', default=1,
|
parser.add_argument('-p', dest='pages', default=1,
|
||||||
help="the number of pages to fetch (doesn't work with"
|
help="the number of pages to fetch (doesn't work with"
|
||||||
@ -436,7 +434,7 @@ def main():
|
|||||||
return
|
return
|
||||||
|
|
||||||
if args.database:
|
if args.database:
|
||||||
mags = local(args)
|
mags = local(args.database, args.search)
|
||||||
else:
|
else:
|
||||||
mags, mirrors = [], ['http://thepiratebay.se']
|
mags, mirrors = [], ['http://thepiratebay.se']
|
||||||
try:
|
try:
|
||||||
@ -444,10 +442,11 @@ def main():
|
|||||||
f = opener.open('https://proxybay.info/list.txt', timeout=5)
|
f = opener.open('https://proxybay.info/list.txt', timeout=5)
|
||||||
if f.getcode() != 200:
|
if f.getcode() != 200:
|
||||||
raise IOError('The pirate bay responded with an error.')
|
raise IOError('The pirate bay responded with an error.')
|
||||||
res = f.read().decode('utf-8')
|
mirrors.extend([i.decode('utf-8').strip()
|
||||||
mirrors.extend(res.split('\n')[3:])
|
for i in f.readlines()][3:])
|
||||||
except IOError:
|
except IOError:
|
||||||
print('Could not fetch additional mirrors', color='WARN')
|
print('Could not fetch additional mirrors', color='WARN')
|
||||||
|
print(*mirrors, sep="\n")
|
||||||
for mirror in mirrors:
|
for mirror in mirrors:
|
||||||
try:
|
try:
|
||||||
print('Trying', mirror, end='... ')
|
print('Trying', mirror, end='... ')
|
||||||
@ -455,8 +454,11 @@ def main():
|
|||||||
site = mirror
|
site = mirror
|
||||||
print('Ok', color='alt')
|
print('Ok', color='alt')
|
||||||
break
|
break
|
||||||
except URLError:
|
except (URLError, IOError, ValueError, timeout) as e:
|
||||||
print('Failed', color='WARN')
|
print('Failed', color='WARN')
|
||||||
|
else:
|
||||||
|
print('No available mirrors :(', color='WARN')
|
||||||
|
return
|
||||||
|
|
||||||
if not mags:
|
if not mags:
|
||||||
print('No results')
|
print('No results')
|
||||||
|
Loading…
Reference in New Issue
Block a user