1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-24 12:14:20 +01:00

Improve io error handling

This commit is contained in:
Rnhmjoj 2014-12-04 15:58:32 +01:00
parent b967607b5b
commit 10ba834ab0

View File

@ -32,6 +32,7 @@ import urllib.request as request
import urllib.parse as parse import urllib.parse as parse
from html.parser import HTMLParser from html.parser import HTMLParser
from urllib.error import URLError
from io import BytesIO from io import BytesIO
categories = { categories = {
@ -153,9 +154,7 @@ def print(*args, **kwargs):
try: try:
c = color_dict[kwargs.pop("color")] c = color_dict[kwargs.pop("color")]
args = (c + args[0],) + args[1:] + (colorama.Style.RESET_ALL,) args = (c + args[0],) + args[1:] + (colorama.Style.RESET_ALL,)
except KeyError as e: except (KeyError, IndexError):
pass
except IndexError as e:
pass pass
return builtins.print(*args, **kwargs) return builtins.print(*args, **kwargs)
else: else:
@ -166,13 +165,10 @@ def print(*args, **kwargs):
#todo: redo this with html parser instead of regex #todo: redo this with html parser instead of regex
def remote(args, mirror): def remote(args, mirror):
res_l = [] res_l = []
try: pages = int(args.pages)
pages = int(args.pages) if pages < 1:
if pages < 1: raise ValueError("Please provide an integer greater than 0 "
raise Exception('') "for the number of pages to fetch.")
except Exception:
raise Exception("Please provide an integer greater than 0"
"for the number of pages to fetch.")
if str(args.category) in categories.values(): if str(args.category) in categories.values():
category = args.category category = args.category
@ -216,7 +212,7 @@ def remote(args, mirror):
req = request.Request(mirror + path) req = request.Request(mirror + path)
req.add_header('Accept-encoding', 'gzip') req.add_header('Accept-encoding', 'gzip')
f = request.urlopen(req) f = request.urlopen(req, timeout=2)
if f.info().get('Content-Encoding') == 'gzip': if f.info().get('Content-Encoding') == 'gzip':
f = gzip.GzipFile(fileobj=BytesIO(f.read())) f = gzip.GzipFile(fileobj=BytesIO(f.read()))
res = f.read().decode('utf-8') res = f.read().decode('utf-8')
@ -230,7 +226,7 @@ def remote(args, mirror):
# 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,
# so let's try another mirror # so let's try another mirror
raise Exception("Blocked mirror detected.") raise IOError("Blocked mirror detected.")
# get sizes as well and substitute the &nbsp; character # get sizes as well and substitute the &nbsp; character
sizes.extend([match.replace("&nbsp;", " ") sizes.extend([match.replace("&nbsp;", " ")
@ -438,29 +434,28 @@ def main():
if args.database: if args.database:
mags = local(args) mags = local(args)
else: else:
mirrors = ["https://pirateproxy.sx"] mirrors = ["http://thepiratebay.se"]
try: try:
opener = request.build_opener(NoRedirection) opener = request.build_opener(NoRedirection)
f = opener.open("https://proxybay.info/list.txt") 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('utf8') res = f.read().decode('utf-8')
mirrors.append(res.split("\n")[3:]) mirrors.extend(res.split("\n")[3:])
except IOError: except IOError:
print("Could not fetch additional mirrors", color="WARN") print("Could not fetch additional mirrors", color="WARN")
for mirror in mirrors: for mirror in mirrors:
try: try:
print("Trying " + mirror) print("Trying", mirror, end="... ")
mags, sizes, uploaded, identifiers = remote(args, mirror) mags, sizes, uploaded, identifiers = remote(args, mirror)
site = mirror site = mirror
print("Ok", color="alt")
break break
except Exception as e: except URLError:
print(format(e)) print("Failed", color="WARN")
print("Could not contact", mirror, color="WARN")
if not mags or len(mags) == 0: if not mags or len(mags) == 0:
print("no results") print("No results")
return return
print_search_results(mags, sizes, uploaded) print_search_results(mags, sizes, uploaded)