mirror of
https://github.com/vikstrous/pirate-get
synced 2025-01-10 10:04:21 +01:00
organize argument passing to torrent.py
This commit is contained in:
parent
2026daa47d
commit
a430fab14c
@ -123,6 +123,16 @@ def main():
|
||||
help='disable colored output')
|
||||
args = parser.parse_args()
|
||||
|
||||
# figure out the mode - browse, search, top or recent
|
||||
if args.browse:
|
||||
args.mode = 'browse'
|
||||
elif args.recent:
|
||||
args.mode = 'recent'
|
||||
elif len(args.search) == 0:
|
||||
args.mode = 'top'
|
||||
else:
|
||||
args.mode = 'search'
|
||||
|
||||
if (config.getboolean('Misc', 'colors') and not args.color
|
||||
or not config.getboolean('Misc', 'colors')):
|
||||
pirate.data.colored_output = False
|
||||
@ -182,8 +192,14 @@ def main():
|
||||
for mirror in mirrors:
|
||||
try:
|
||||
print('Trying', mirror, end='... ')
|
||||
mags, sizes, uploaded, ids = pirate.torrent.remote(args,
|
||||
mirror)
|
||||
mags, sizes, uploaded, ids = pirate.torrent.remote(
|
||||
pages=args.pages,
|
||||
category=pirate.torrent.parse_category(args.category),
|
||||
sort=pirate.torrent.parse_sort(args.sort),
|
||||
mode=args.mode,
|
||||
terms=args.search,
|
||||
mirror=mirror
|
||||
)
|
||||
except (urllib.error.URLError, socket.timeout,
|
||||
IOError, ValueError):
|
||||
print('Failed', color='WARN')
|
||||
|
@ -11,52 +11,64 @@ from pirate.print import print
|
||||
|
||||
from io import BytesIO
|
||||
|
||||
|
||||
def parse_category(category):
|
||||
if str(category) in pirate.data.categories.values():
|
||||
return category
|
||||
elif category in pirate.data.categories.keys():
|
||||
return pirate.data.categories[category]
|
||||
else:
|
||||
print('Invalid category ignored', color='WARN')
|
||||
return '0'
|
||||
|
||||
|
||||
def parse_sort(sort):
|
||||
if str(sort) in pirate.data.sorts.values():
|
||||
return sort
|
||||
elif sort in pirate.data.sorts.keys():
|
||||
return pirate.data.sorts[sort]
|
||||
else:
|
||||
print('Invalid sort ignored', color='WARN')
|
||||
return '99'
|
||||
|
||||
|
||||
#todo: redo this with html parser instead of regex
|
||||
def remote(args, mirror):
|
||||
def remote(pages, category, sort, mode, terms, mirror):
|
||||
res_l = []
|
||||
pages = int(args.pages)
|
||||
pages = int(pages)
|
||||
if pages < 1:
|
||||
raise ValueError('Please provide an integer greater than 0 '
|
||||
'for the number of pages to fetch.')
|
||||
|
||||
if str(args.category) in pirate.data.categories.values():
|
||||
category = args.category
|
||||
elif args.category in pirate.data.categories.keys():
|
||||
category = pirate.data.categories[args.category]
|
||||
else:
|
||||
category = '0'
|
||||
print('Invalid category ignored', color='WARN')
|
||||
|
||||
if str(args.sort) in pirate.data.sorts.values():
|
||||
sort = args.sort
|
||||
elif args.sort in pirate.data.sorts.keys():
|
||||
sort = pirate.data.sorts[args.sort]
|
||||
else:
|
||||
sort = '99'
|
||||
print('Invalid sort ignored', color='WARN')
|
||||
# Catch the Ctrl-C exception and exit cleanly
|
||||
try:
|
||||
sizes = []
|
||||
uploaded = []
|
||||
identifiers = []
|
||||
for page in range(pages):
|
||||
if args.browse:
|
||||
if mode == 'browse':
|
||||
path = '/browse/'
|
||||
if(category == 0):
|
||||
category = 100
|
||||
path = '/browse/' + '/'.join(str(i) for i in (
|
||||
category, page, sort))
|
||||
elif len(args.search) == 0:
|
||||
path = '/top/48h' if args.recent else '/top/'
|
||||
path = '/browse/{}/{}/{}'.format(category, page, sort)
|
||||
elif mode == 'recent':
|
||||
# This is not a typo. There is no / between 48h and the category.
|
||||
path = '/top/48h'
|
||||
if(category == 0):
|
||||
path += 'all'
|
||||
else:
|
||||
path += str(category)
|
||||
elif mode == 'top':
|
||||
path = '/top/'
|
||||
if(category == 0):
|
||||
path += 'all'
|
||||
else:
|
||||
path = '/search/' + '/'.join(str(i) for i in (
|
||||
'+'.join(args.search),
|
||||
page, sort,
|
||||
category))
|
||||
path += str(category)
|
||||
elif mode == 'search':
|
||||
query = urllib.parse.quote_plus(' '.join(terms))
|
||||
path = '/search/{}/{}/{}/{}'.format(query, page, sort, category)
|
||||
else:
|
||||
raise Exception('Unknown mode.')
|
||||
|
||||
req = request.Request(mirror + path,
|
||||
headers=pirate.data.default_headers)
|
||||
|
Loading…
Reference in New Issue
Block a user