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')
|
help='disable colored output')
|
||||||
args = parser.parse_args()
|
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
|
if (config.getboolean('Misc', 'colors') and not args.color
|
||||||
or not config.getboolean('Misc', 'colors')):
|
or not config.getboolean('Misc', 'colors')):
|
||||||
pirate.data.colored_output = False
|
pirate.data.colored_output = False
|
||||||
@ -182,8 +192,14 @@ def main():
|
|||||||
for mirror in mirrors:
|
for mirror in mirrors:
|
||||||
try:
|
try:
|
||||||
print('Trying', mirror, end='... ')
|
print('Trying', mirror, end='... ')
|
||||||
mags, sizes, uploaded, ids = pirate.torrent.remote(args,
|
mags, sizes, uploaded, ids = pirate.torrent.remote(
|
||||||
mirror)
|
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,
|
except (urllib.error.URLError, socket.timeout,
|
||||||
IOError, ValueError):
|
IOError, ValueError):
|
||||||
print('Failed', color='WARN')
|
print('Failed', color='WARN')
|
||||||
|
@ -11,52 +11,64 @@ from pirate.print import print
|
|||||||
|
|
||||||
from io import BytesIO
|
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
|
#todo: redo this with html parser instead of regex
|
||||||
def remote(args, mirror):
|
def remote(pages, category, sort, mode, terms, mirror):
|
||||||
res_l = []
|
res_l = []
|
||||||
pages = int(args.pages)
|
pages = int(pages)
|
||||||
if pages < 1:
|
if pages < 1:
|
||||||
raise ValueError('Please provide an integer greater than 0 '
|
raise ValueError('Please provide an integer greater than 0 '
|
||||||
'for the number of pages to fetch.')
|
'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
|
# Catch the Ctrl-C exception and exit cleanly
|
||||||
try:
|
try:
|
||||||
sizes = []
|
sizes = []
|
||||||
uploaded = []
|
uploaded = []
|
||||||
identifiers = []
|
identifiers = []
|
||||||
for page in range(pages):
|
for page in range(pages):
|
||||||
if args.browse:
|
if mode == 'browse':
|
||||||
path = '/browse/'
|
path = '/browse/'
|
||||||
if(category == 0):
|
if(category == 0):
|
||||||
category = 100
|
category = 100
|
||||||
path = '/browse/' + '/'.join(str(i) for i in (
|
path = '/browse/{}/{}/{}'.format(category, page, sort)
|
||||||
category, page, sort))
|
elif mode == 'recent':
|
||||||
elif len(args.search) == 0:
|
# This is not a typo. There is no / between 48h and the category.
|
||||||
path = '/top/48h' if args.recent else '/top/'
|
path = '/top/48h'
|
||||||
if(category == 0):
|
if(category == 0):
|
||||||
path += 'all'
|
path += 'all'
|
||||||
else:
|
else:
|
||||||
path += str(category)
|
path += str(category)
|
||||||
|
elif mode == 'top':
|
||||||
|
path = '/top/'
|
||||||
|
if(category == 0):
|
||||||
|
path += 'all'
|
||||||
else:
|
else:
|
||||||
path = '/search/' + '/'.join(str(i) for i in (
|
path += str(category)
|
||||||
'+'.join(args.search),
|
elif mode == 'search':
|
||||||
page, sort,
|
query = urllib.parse.quote_plus(' '.join(terms))
|
||||||
category))
|
path = '/search/{}/{}/{}/{}'.format(query, page, sort, category)
|
||||||
|
else:
|
||||||
|
raise Exception('Unknown mode.')
|
||||||
|
|
||||||
req = request.Request(mirror + path,
|
req = request.Request(mirror + path,
|
||||||
headers=pirate.data.default_headers)
|
headers=pirate.data.default_headers)
|
||||||
|
Loading…
Reference in New Issue
Block a user