mirror of
https://github.com/vikstrous/pirate-get
synced 2025-01-09 09:59:51 +01:00
add option to configure requests timeout
This commit is contained in:
parent
c23c3db3d8
commit
5a6429d61d
@ -42,6 +42,7 @@ def parse_config_file(text):
|
|||||||
config.set('Misc', 'transmission-port', '') # for backward compatibility
|
config.set('Misc', 'transmission-port', '') # for backward compatibility
|
||||||
config.set('Misc', 'colors', 'true')
|
config.set('Misc', 'colors', 'true')
|
||||||
config.set('Misc', 'mirror', pirate.data.default_mirror)
|
config.set('Misc', 'mirror', pirate.data.default_mirror)
|
||||||
|
config.set('Misc', 'timeout', pirate.data.default_timeout)
|
||||||
|
|
||||||
config.read_string(text)
|
config.read_string(text)
|
||||||
|
|
||||||
@ -179,6 +180,8 @@ def parse_args(args_in):
|
|||||||
parser.add_argument('-m', '--mirror',
|
parser.add_argument('-m', '--mirror',
|
||||||
type=str, nargs='+',
|
type=str, nargs='+',
|
||||||
help='the pirate bay mirror(s) to use')
|
help='the pirate bay mirror(s) to use')
|
||||||
|
parser.add_argument('-z', '--timeout', type=int,
|
||||||
|
help='timeout in seconds for http requests')
|
||||||
parser.add_argument('-v', '--version',
|
parser.add_argument('-v', '--version',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help='print pirate-get version number')
|
help='print pirate-get version number')
|
||||||
@ -220,6 +223,9 @@ def combine_configs(config, args):
|
|||||||
if not args.mirror:
|
if not args.mirror:
|
||||||
args.mirror = config.get('Misc', 'mirror').split()
|
args.mirror = config.get('Misc', 'mirror').split()
|
||||||
|
|
||||||
|
if not args.timeout:
|
||||||
|
args.timeout = int(config.get('Misc', 'timeout'))
|
||||||
|
|
||||||
args.transmission_command = ['transmission-remote']
|
args.transmission_command = ['transmission-remote']
|
||||||
if args.endpoint:
|
if args.endpoint:
|
||||||
args.transmission_command.append(args.endpoint)
|
args.transmission_command.append(args.endpoint)
|
||||||
@ -258,14 +264,15 @@ def combine_configs(config, args):
|
|||||||
def connect_mirror(mirror, printer, args):
|
def connect_mirror(mirror, printer, args):
|
||||||
try:
|
try:
|
||||||
printer.print('Trying', mirror, end='... ')
|
printer.print('Trying', mirror, end='... ')
|
||||||
url = pirate.torrent.find_api(mirror)
|
url = pirate.torrent.find_api(mirror, args.timeout)
|
||||||
results = pirate.torrent.remote(
|
results = pirate.torrent.remote(
|
||||||
printer=printer,
|
printer=printer,
|
||||||
category=pirate.torrent.parse_category(printer, args.category),
|
category=pirate.torrent.parse_category(printer, args.category),
|
||||||
sort=pirate.torrent.parse_sort(printer, args.sort),
|
sort=pirate.torrent.parse_sort(printer, args.sort),
|
||||||
mode=args.action,
|
mode=args.action,
|
||||||
terms=args.search,
|
terms=args.search,
|
||||||
mirror=url)
|
mirror=url,
|
||||||
|
timeout=args.timeout)
|
||||||
except (urllib.error.URLError, socket.timeout, IOError, ValueError) as e:
|
except (urllib.error.URLError, socket.timeout, IOError, ValueError) as e:
|
||||||
printer.print('Failed', color='WARN', end=' ')
|
printer.print('Failed', color='WARN', end=' ')
|
||||||
printer.print('(', e, ')', sep='')
|
printer.print('(', e, ')', sep='')
|
||||||
@ -286,7 +293,7 @@ def search_mirrors(printer, args):
|
|||||||
try:
|
try:
|
||||||
req = request.Request(pirate.data.mirror_list,
|
req = request.Request(pirate.data.mirror_list,
|
||||||
headers=pirate.data.default_headers)
|
headers=pirate.data.default_headers)
|
||||||
f = request.urlopen(req, timeout=pirate.data.default_timeout)
|
f = request.urlopen(req, timeout=args.timeout)
|
||||||
except urllib.error.URLError as e:
|
except urllib.error.URLError as e:
|
||||||
raise IOError('Could not fetch mirrors', e.reason)
|
raise IOError('Could not fetch mirrors', e.reason)
|
||||||
|
|
||||||
@ -400,9 +407,9 @@ def pirate_main(args):
|
|||||||
printer.print('Bye.', color='alt')
|
printer.print('Bye.', color='alt')
|
||||||
return
|
return
|
||||||
elif code == 'd':
|
elif code == 'd':
|
||||||
printer.descriptions(choices, results, site)
|
printer.descriptions(choices, results, site, args.timeout)
|
||||||
elif code == 'f':
|
elif code == 'f':
|
||||||
printer.file_lists(choices, results, site)
|
printer.file_lists(choices, results, site, args.timeout)
|
||||||
elif code == 'p':
|
elif code == 'p':
|
||||||
printer.search_results(results)
|
printer.search_results(results)
|
||||||
elif code == 'm':
|
elif code == 'm':
|
||||||
@ -412,7 +419,8 @@ def pirate_main(args):
|
|||||||
pirate.torrent.copy_magnets(printer, choices, results)
|
pirate.torrent.copy_magnets(printer, choices, results)
|
||||||
elif code == 't':
|
elif code == 't':
|
||||||
pirate.torrent.save_torrents(printer, choices, results,
|
pirate.torrent.save_torrents(printer, choices, results,
|
||||||
args.save_directory)
|
args.save_directory,
|
||||||
|
args.timeout)
|
||||||
elif not cmd:
|
elif not cmd:
|
||||||
printer.print('No links entered!', color='WARN')
|
printer.print('No links entered!', color='WARN')
|
||||||
else:
|
else:
|
||||||
@ -432,7 +440,8 @@ def pirate_main(args):
|
|||||||
if args.output == 'save_torrent_files':
|
if args.output == 'save_torrent_files':
|
||||||
printer.print('Saving selected torrents...')
|
printer.print('Saving selected torrents...')
|
||||||
pirate.torrent.save_torrents(printer, choices,
|
pirate.torrent.save_torrents(printer, choices,
|
||||||
results, args.save_directory)
|
results, args.save_directory,
|
||||||
|
args.timeout)
|
||||||
return
|
return
|
||||||
|
|
||||||
for choice in choices:
|
for choice in choices:
|
||||||
|
@ -95,16 +95,14 @@ class Printer:
|
|||||||
even = not even
|
even = not even
|
||||||
self.print(table)
|
self.print(table)
|
||||||
|
|
||||||
def descriptions(self, chosen_links, results, site):
|
def descriptions(self, chosen_links, results, site, timeout):
|
||||||
opener = request.build_opener(request.HTTPErrorProcessor)
|
|
||||||
|
|
||||||
for link in chosen_links:
|
for link in chosen_links:
|
||||||
result = results[link]
|
result = results[link]
|
||||||
req = request.Request(
|
req = request.Request(
|
||||||
site + '/t.php?id=' + result['id'],
|
site + '/t.php?id=' + result['id'],
|
||||||
headers=pirate.data.default_headers)
|
headers=pirate.data.default_headers)
|
||||||
req.add_header('Accept-encoding', 'gzip')
|
req.add_header('Accept-encoding', 'gzip')
|
||||||
f = opener.open(req, timeout=pirate.data.default_timeout)
|
f = request.urlopen(req, timeout=timeout)
|
||||||
|
|
||||||
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()))
|
||||||
@ -119,9 +117,7 @@ class Printer:
|
|||||||
color='zebra_1')
|
color='zebra_1')
|
||||||
self.print(desc, color='zebra_0')
|
self.print(desc, color='zebra_0')
|
||||||
|
|
||||||
def file_lists(self, chosen_links, results, site):
|
def file_lists(self, chosen_links, results, site, timeout):
|
||||||
opener = request.build_opener(request.HTTPErrorProcessor)
|
|
||||||
|
|
||||||
# the API may returns object instead of list
|
# the API may returns object instead of list
|
||||||
def get(obj):
|
def get(obj):
|
||||||
try:
|
try:
|
||||||
@ -135,7 +131,7 @@ class Printer:
|
|||||||
site + '/f.php?id=' + result['id'],
|
site + '/f.php?id=' + result['id'],
|
||||||
headers=pirate.data.default_headers)
|
headers=pirate.data.default_headers)
|
||||||
req.add_header('Accept-encoding', 'gzip')
|
req.add_header('Accept-encoding', 'gzip')
|
||||||
f = opener.open(req, timeout=pirate.data.default_timeout)
|
f = request.urlopen(req, timeout=timeout)
|
||||||
|
|
||||||
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()))
|
||||||
|
@ -64,7 +64,7 @@ def make_magnet(name, info_hash):
|
|||||||
info_hash, parse.quote(name, ''))
|
info_hash, parse.quote(name, ''))
|
||||||
|
|
||||||
|
|
||||||
def remote(printer, category, sort, mode, terms, mirror):
|
def remote(printer, category, sort, mode, terms, mirror, timeout):
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
# Catch the Ctrl-C exception and exit cleanly
|
# Catch the Ctrl-C exception and exit cleanly
|
||||||
@ -74,7 +74,7 @@ def remote(printer, category, sort, mode, terms, mirror):
|
|||||||
mirror, ' '.join(terms), category),
|
mirror, ' '.join(terms), category),
|
||||||
headers=pirate.data.default_headers)
|
headers=pirate.data.default_headers)
|
||||||
try:
|
try:
|
||||||
f = request.urlopen(req, timeout=pirate.data.default_timeout)
|
f = request.urlopen(req, timeout=timeout)
|
||||||
except urllib.error.URLError as e:
|
except urllib.error.URLError as e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
@ -94,13 +94,13 @@ def remote(printer, category, sort, mode, terms, mirror):
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
def find_api(mirror):
|
def find_api(mirror, timeout):
|
||||||
# try common paths
|
# try common paths
|
||||||
for path in ['', '/apip', '/api.php?url=']:
|
for path in ['', '/apip', '/api.php?url=']:
|
||||||
req = request.Request(mirror + path + '/q.php?q=test&cat=0',
|
req = request.Request(mirror + path + '/q.php?q=test&cat=0',
|
||||||
headers=pirate.data.default_headers)
|
headers=pirate.data.default_headers)
|
||||||
try:
|
try:
|
||||||
f = request.urlopen(req, timeout=pirate.data.default_timeout)
|
f = request.urlopen(req, timeout=timeout)
|
||||||
if f.info().get_content_type() == 'application/json':
|
if f.info().get_content_type() == 'application/json':
|
||||||
return mirror + path
|
return mirror + path
|
||||||
except urllib.error.URLError:
|
except urllib.error.URLError:
|
||||||
@ -110,7 +110,7 @@ def find_api(mirror):
|
|||||||
req = request.Request(mirror + '/static/main.js',
|
req = request.Request(mirror + '/static/main.js',
|
||||||
headers=pirate.data.default_headers)
|
headers=pirate.data.default_headers)
|
||||||
try:
|
try:
|
||||||
f = request.urlopen(req, timeout=pirate.data.default_timeout)
|
f = request.urlopen(req, timeout=timeout)
|
||||||
if f.info().get_content_type() == 'application/javascript':
|
if f.info().get_content_type() == 'application/javascript':
|
||||||
match = re.search("var server='([^']+)'", f.read().decode())
|
match = re.search("var server='([^']+)'", f.read().decode())
|
||||||
return mirror + match.group(1)
|
return mirror + match.group(1)
|
||||||
@ -120,27 +120,27 @@ def find_api(mirror):
|
|||||||
raise IOError('API not found')
|
raise IOError('API not found')
|
||||||
|
|
||||||
|
|
||||||
def get_torrent(info_hash):
|
def get_torrent(info_hash, timeout):
|
||||||
url = 'http://itorrents.org/torrent/{:X}.torrent'
|
url = 'http://itorrents.org/torrent/{:X}.torrent'
|
||||||
req = request.Request(url.format(info_hash),
|
req = request.Request(url.format(info_hash),
|
||||||
headers=pirate.data.default_headers)
|
headers=pirate.data.default_headers)
|
||||||
req.add_header('Accept-encoding', 'gzip')
|
req.add_header('Accept-encoding', 'gzip')
|
||||||
|
|
||||||
torrent = request.urlopen(req, timeout=pirate.data.default_timeout)
|
torrent = request.urlopen(req, timeout=timeout)
|
||||||
if torrent.info().get('Content-Encoding') == 'gzip':
|
if torrent.info().get('Content-Encoding') == 'gzip':
|
||||||
torrent = gzip.GzipFile(fileobj=BytesIO(torrent.read()))
|
torrent = gzip.GzipFile(fileobj=BytesIO(torrent.read()))
|
||||||
|
|
||||||
return torrent.read()
|
return torrent.read()
|
||||||
|
|
||||||
|
|
||||||
def save_torrents(printer, chosen_links, results, folder):
|
def save_torrents(printer, chosen_links, results, folder, timeout):
|
||||||
for link in chosen_links:
|
for link in chosen_links:
|
||||||
result = results[link]
|
result = results[link]
|
||||||
torrent_name = result['name'].replace('/', '_').replace('\\', '_')
|
torrent_name = result['name'].replace('/', '_').replace('\\', '_')
|
||||||
file = os.path.join(folder, torrent_name + '.torrent')
|
file = os.path.join(folder, torrent_name + '.torrent')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
torrent = get_torrent(result['info_hash'])
|
torrent = get_torrent(result['info_hash'], timeout)
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
printer.print('There is no cached file for this torrent :('
|
printer.print('There is no cached file for this torrent :('
|
||||||
' \nCode: {} - {}'.format(e.code, e.reason),
|
' \nCode: {} - {}'.format(e.code, e.reason),
|
||||||
|
Loading…
Reference in New Issue
Block a user