mirror of
https://github.com/vikstrous/pirate-get
synced 2025-01-09 09:59:51 +01:00
new interface for pirate-get
This commit is contained in:
parent
f0f17b8b8f
commit
19a57a21f3
177
pirate/pirate.py
177
pirate/pirate.py
@ -118,79 +118,93 @@ def parse_torrent_command(l):
|
|||||||
choices = [elem for elem in choices]
|
choices = [elem for elem in choices]
|
||||||
return code, choices
|
return code, choices
|
||||||
|
|
||||||
|
# XXX: make this a part of a class
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description='finds and downloads torrents from the Pirate Bay')
|
||||||
|
|
||||||
|
# common options
|
||||||
|
parser.add_argument('--disable-colors', dest='color',
|
||||||
|
action='store_false',
|
||||||
|
help='disable colored output')
|
||||||
|
# XXX: this option doesn't always apply
|
||||||
|
parser.add_argument('-c', dest='category', metavar='category',
|
||||||
|
help='specify a category to search', default='All')
|
||||||
|
# XXX: this option doesn't always apply
|
||||||
|
parser.add_argument('-s', dest='sort', metavar='sort',
|
||||||
|
help='specify a sort option', default='SeedersDsc')
|
||||||
|
# XXX: this option doesn't always apply
|
||||||
|
parser.add_argument('-L', '--local', dest='database',
|
||||||
|
help='an xml file containing the Pirate Bay database')
|
||||||
|
# XXX: this option doesn't always apply
|
||||||
|
parser.add_argument('-p', dest='pages', default=1, type=int,
|
||||||
|
help='the number of pages to fetch '
|
||||||
|
"(doesn't work with --local)")
|
||||||
|
parser.add_argument('-0', dest='first',
|
||||||
|
action='store_true',
|
||||||
|
help='choose the top result')
|
||||||
|
parser.add_argument('-a', '--download-all',
|
||||||
|
action='store_true',
|
||||||
|
help='download all results')
|
||||||
|
# XXX: this option doesn't always apply
|
||||||
|
parser.add_argument('-C', '--command', dest='command',
|
||||||
|
help='open magnets with a custom command'
|
||||||
|
' (%%s will be replaced with the url)')
|
||||||
|
|
||||||
|
# XXX: this applies only for transmission
|
||||||
|
parser.add_argument('-P', '--port', dest='port',
|
||||||
|
help='transmission-remote rpc port. default is 9091')
|
||||||
|
|
||||||
|
# output options
|
||||||
|
parser.add_argument('-S', '--save-directory',
|
||||||
|
help='directory to store output in')
|
||||||
|
parser.add_argument('-o', '--output',
|
||||||
|
choices=['transmission', 'command', 'magnet', 'torrent', 'browser'],
|
||||||
|
default='browser',
|
||||||
|
help='what to do with the chosen torrent')
|
||||||
|
# subcommands
|
||||||
|
subparsers = parser.add_subparsers(help='commands')
|
||||||
|
|
||||||
|
# search command
|
||||||
|
search_parser = subparsers.add_parser('search', help='search for torrents')
|
||||||
|
search_parser.set_defaults(action='search')
|
||||||
|
search_parser.add_argument('terms', metavar='terms',
|
||||||
|
nargs='*', help='terms to search for')
|
||||||
|
# browse command
|
||||||
|
browse_parser = subparsers.add_parser('browse', help='browse for torrents')
|
||||||
|
browse_parser.set_defaults(action='browse')
|
||||||
|
|
||||||
|
# recent command
|
||||||
|
recent_parser = subparsers.add_parser('recent', help='view torrents uploaded in the last 48h')
|
||||||
|
recent_parser.set_defaults(action='recent')
|
||||||
|
|
||||||
|
# categories command
|
||||||
|
categories_parser = subparsers.add_parser('list_categories', help='list valid categories')
|
||||||
|
categories_parser.set_defaults(action='list_categories')
|
||||||
|
|
||||||
|
# sorts command
|
||||||
|
sorts_parser = subparsers.add_parser('list_sorts', help='list valid sorts')
|
||||||
|
sorts_parser.set_defaults(action='list_sorts')
|
||||||
|
|
||||||
|
# top command
|
||||||
|
sorts_parser = subparsers.add_parser('top', help='top recent torrents')
|
||||||
|
sorts_parser.set_defaults(action='top')
|
||||||
|
|
||||||
|
|
||||||
def parse_args(args_in):
|
def parse_args(args_in):
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
description='finds and downloads torrents from the Pirate Bay')
|
|
||||||
parser.add_argument('-b', dest='browse',
|
|
||||||
action='store_true',
|
|
||||||
help='display in Browse mode')
|
|
||||||
parser.add_argument('search', metavar='search',
|
|
||||||
nargs='*', help='term to search for')
|
|
||||||
parser.add_argument('-c', dest='category', metavar='category',
|
|
||||||
help='specify a category to search', default='All')
|
|
||||||
parser.add_argument('-s', dest='sort', metavar='sort',
|
|
||||||
help='specify a sort option', default='SeedersDsc')
|
|
||||||
parser.add_argument('-R', dest='recent', action='store_true',
|
|
||||||
help='torrents uploaded in the last 48hours.'
|
|
||||||
'*ignored in searches*')
|
|
||||||
parser.add_argument('-l', dest='list_categories',
|
|
||||||
action='store_true',
|
|
||||||
help='list categories')
|
|
||||||
parser.add_argument('--list_sorts', dest='list_sorts',
|
|
||||||
action='store_true',
|
|
||||||
help='list Sortable Types')
|
|
||||||
parser.add_argument('-L', '--local', dest='database',
|
|
||||||
help='an xml file containing the Pirate Bay database')
|
|
||||||
parser.add_argument('-p', dest='pages', default=1, type=int,
|
|
||||||
help='the number of pages to fetch '
|
|
||||||
"(doesn't work with --local)")
|
|
||||||
parser.add_argument('-0', dest='first',
|
|
||||||
action='store_true',
|
|
||||||
help='choose the top result')
|
|
||||||
parser.add_argument('-a', '--download-all',
|
|
||||||
action='store_true',
|
|
||||||
help='download all results')
|
|
||||||
parser.add_argument('-t', '--transmission',
|
|
||||||
action='store_true',
|
|
||||||
help='open magnets with transmission-remote')
|
|
||||||
parser.add_argument('-P', '--port', dest='port',
|
|
||||||
help='transmission-remote rpc port. default is 9091')
|
|
||||||
parser.add_argument('-C', '--custom', dest='command',
|
|
||||||
help='open magnets with a custom command'
|
|
||||||
' (%%s will be replaced with the url)')
|
|
||||||
parser.add_argument('-M', '--save-magnets',
|
|
||||||
action='store_true',
|
|
||||||
help='save magnets links as files')
|
|
||||||
parser.add_argument('-T', '--save-torrents',
|
|
||||||
action='store_true',
|
|
||||||
help='save torrent files')
|
|
||||||
parser.add_argument('-S', '--save-directory',
|
|
||||||
type=str, metavar='DIRECTORY',
|
|
||||||
help='directory where to save downloaded files'
|
|
||||||
' (if none is given $PWD will be used)')
|
|
||||||
parser.add_argument('--disable-colors', dest='color',
|
|
||||||
action='store_false',
|
|
||||||
help='disable colored output')
|
|
||||||
args = parser.parse_args(args_in)
|
args = parser.parse_args(args_in)
|
||||||
|
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def combine_configs(config, args):
|
def combine_configs(config, args):
|
||||||
# figure out the action - browse, search, top, etc.
|
if args.command:
|
||||||
if args.browse:
|
args.action = 'command'
|
||||||
args.action = 'browse'
|
|
||||||
elif args.recent:
|
if getattr(args, 'action', None) is None:
|
||||||
args.action = 'recent'
|
parser.print_help()
|
||||||
elif args.list_categories:
|
sys.exit(0)
|
||||||
args.action = 'list_categories'
|
|
||||||
elif args.list_sorts:
|
if args.action == 'search' and len(args.terms) == 0:
|
||||||
args.action = 'list_sorts'
|
|
||||||
elif len(args.search) == 0:
|
|
||||||
args.action = 'top'
|
args.action = 'top'
|
||||||
else:
|
|
||||||
args.action = 'search'
|
|
||||||
|
|
||||||
args.source = 'tpb'
|
args.source = 'tpb'
|
||||||
if args.database or config.getboolean('LocalDB', 'enabled'):
|
if args.database or config.getboolean('LocalDB', 'enabled'):
|
||||||
@ -199,9 +213,8 @@ def combine_configs(config, args):
|
|||||||
if not args.database:
|
if not args.database:
|
||||||
args.database = config.get('LocalDB', 'path')
|
args.database = config.get('LocalDB', 'path')
|
||||||
|
|
||||||
if not args.color or not config.getboolean('Misc', 'colors'):
|
if args.color:
|
||||||
# TODO: consider how this can be moved to the args
|
args.color = config.getboolean('Misc', 'colors')
|
||||||
pirate.data.colored_output = False
|
|
||||||
|
|
||||||
if not args.save_directory:
|
if not args.save_directory:
|
||||||
args.save_directory = config.get('Save', 'directory')
|
args.save_directory = config.get('Save', 'directory')
|
||||||
@ -210,16 +223,6 @@ def combine_configs(config, args):
|
|||||||
if args.port:
|
if args.port:
|
||||||
args.transmission_command.append(args.port)
|
args.transmission_command.append(args.port)
|
||||||
|
|
||||||
args.output = 'browser_open'
|
|
||||||
if args.transmission or config.getboolean('Misc', 'transmission'):
|
|
||||||
args.output = 'transmission'
|
|
||||||
elif args.save_magnets or config.getboolean('Save', 'magnets'):
|
|
||||||
args.output = 'save_magnet_files'
|
|
||||||
elif args.save_torrents or config.getboolean('Save', 'torrents'):
|
|
||||||
args.output = 'save_torrent_files'
|
|
||||||
elif args.command or config.get('Misc', 'openCommand'):
|
|
||||||
args.output = 'open_command'
|
|
||||||
|
|
||||||
args.open_command = args.command
|
args.open_command = args.command
|
||||||
if not args.open_command:
|
if not args.open_command:
|
||||||
args.open_command = config.get('Misc', 'openCommand')
|
args.open_command = config.get('Misc', 'openCommand')
|
||||||
@ -227,7 +230,7 @@ def combine_configs(config, args):
|
|||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def search_mirrors(printer, pages, category, sort, action, search):
|
def search_mirrors(printer, pages, category, sort, action, terms):
|
||||||
mirror_sources = [None, 'https://proxybay.co/list.txt']
|
mirror_sources = [None, 'https://proxybay.co/list.txt']
|
||||||
for mirror_source in mirror_sources:
|
for mirror_source in mirror_sources:
|
||||||
mirrors = OrderedDict()
|
mirrors = OrderedDict()
|
||||||
@ -258,7 +261,7 @@ def search_mirrors(printer, pages, category, sort, action, search):
|
|||||||
category=pirate.torrent.parse_category(printer, category),
|
category=pirate.torrent.parse_category(printer, category),
|
||||||
sort=pirate.torrent.parse_sort(printer, sort),
|
sort=pirate.torrent.parse_sort(printer, sort),
|
||||||
mode=action,
|
mode=action,
|
||||||
terms=search,
|
terms=terms,
|
||||||
mirror=mirror
|
mirror=mirror
|
||||||
)
|
)
|
||||||
except (urllib.error.URLError, socket.timeout,
|
except (urllib.error.URLError, socket.timeout,
|
||||||
@ -276,7 +279,7 @@ def pirate_main(args):
|
|||||||
printer = Printer(args.color)
|
printer = Printer(args.color)
|
||||||
|
|
||||||
# check it transmission is running
|
# check it transmission is running
|
||||||
if args.transmission:
|
if args.output == 'transmission':
|
||||||
ret = subprocess.call(args.transmission_command + ['-l'],
|
ret = subprocess.call(args.transmission_command + ['-l'],
|
||||||
stdout=subprocess.DEVNULL,
|
stdout=subprocess.DEVNULL,
|
||||||
stderr=subprocess.DEVNULL)
|
stderr=subprocess.DEVNULL)
|
||||||
@ -303,9 +306,9 @@ def pirate_main(args):
|
|||||||
# fetch torrents
|
# fetch torrents
|
||||||
|
|
||||||
if args.source == 'local_tpb':
|
if args.source == 'local_tpb':
|
||||||
results = pirate.local.search(args.database, args.search)
|
results = pirate.local.search(args.database, args.terms)
|
||||||
elif args.source == 'tpb':
|
elif args.source == 'tpb':
|
||||||
results, site = search_mirrors(printer, args.pages, args.category, args.sort, args.action, args.search)
|
results, site = search_mirrors(printer, args.pages, args.category, args.sort, args.action, args.terms)
|
||||||
|
|
||||||
if len(results) == 0:
|
if len(results) == 0:
|
||||||
printer.print('No results')
|
printer.print('No results')
|
||||||
@ -367,12 +370,12 @@ def pirate_main(args):
|
|||||||
|
|
||||||
# output
|
# output
|
||||||
|
|
||||||
if args.output == 'save_magnet_files':
|
if args.output == 'magnet':
|
||||||
printer.print('Saving selected magnets...')
|
printer.print('Saving selected magnets...')
|
||||||
pirate.torrent.save_magnets(choices, results, args.save_directory)
|
pirate.torrent.save_magnets(choices, results, args.save_directory)
|
||||||
return
|
return
|
||||||
|
|
||||||
if args.output == 'save_torrent_files':
|
if args.output == 'torrent':
|
||||||
printer.print('Saving selected torrents...')
|
printer.print('Saving selected torrents...')
|
||||||
pirate.torrent.save_torrents(choices, results, args.save_directory)
|
pirate.torrent.save_torrents(choices, results, args.save_directory)
|
||||||
return
|
return
|
||||||
@ -382,9 +385,9 @@ def pirate_main(args):
|
|||||||
|
|
||||||
if args.output == 'transmission':
|
if args.output == 'transmission':
|
||||||
subprocess.call(args.transmission_command + ['--add', url])
|
subprocess.call(args.transmission_command + ['--add', url])
|
||||||
elif args.output == 'open_command':
|
elif args.output == 'command':
|
||||||
subprocess.call(parse_cmd(args.open_command, url))
|
subprocess.call(parse_cmd(args.open_command, url))
|
||||||
elif args.output == 'browser_open':
|
elif args.output == 'browser':
|
||||||
webbrowser.open(url)
|
webbrowser.open(url)
|
||||||
|
|
||||||
if args.output == 'transmission':
|
if args.output == 'transmission':
|
||||||
|
@ -34,7 +34,7 @@ class TestPirate(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
with patch('pirate.torrent.remote', return_value=[result]) as mock_remote:
|
with patch('pirate.torrent.remote', return_value=[result]) as mock_remote:
|
||||||
config = pirate.pirate.parse_config_file('')
|
config = pirate.pirate.parse_config_file('')
|
||||||
args = pirate.pirate.combine_configs(config, pirate.pirate.parse_args(['-0', 'term', '-C', 'blah %s']))
|
args = pirate.pirate.combine_configs(config, pirate.pirate.parse_args(['-0', '-C', 'blah %s', '-o', 'command', 'search', 'term']))
|
||||||
pirate.pirate.pirate_main(args)
|
pirate.pirate.pirate_main(args)
|
||||||
mock_call.assert_called_once_with(['blah', 'dn=derp'])
|
mock_call.assert_called_once_with(['blah', 'dn=derp'])
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ class TestPirate(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
with patch('pirate.torrent.remote', return_value=[result]) as mock_remote:
|
with patch('pirate.torrent.remote', return_value=[result]) as mock_remote:
|
||||||
config = pirate.pirate.parse_config_file('')
|
config = pirate.pirate.parse_config_file('')
|
||||||
args = pirate.pirate.combine_configs(config, pirate.pirate.parse_args(['term', '-C', 'blah %s']))
|
args = pirate.pirate.combine_configs(config, pirate.pirate.parse_args(['-C', 'blah %s', '-o', 'command', 'search', 'term']))
|
||||||
pirate.pirate.pirate_main(args)
|
pirate.pirate.pirate_main(args)
|
||||||
mock_call.assert_called_once_with(['blah', 'dn=derp'])
|
mock_call.assert_called_once_with(['blah', 'dn=derp'])
|
||||||
|
|
||||||
@ -120,22 +120,22 @@ class TestPirate(unittest.TestCase):
|
|||||||
|
|
||||||
def test_parse_args(self):
|
def test_parse_args(self):
|
||||||
tests = [
|
tests = [
|
||||||
('', ['-b'], {'action': 'browse'}),
|
('', ['browse'], {'action': 'browse'}),
|
||||||
('', [], {'action': 'top'}),
|
('', ['search'], {'action': 'top'}),
|
||||||
('', ['-R'], {'action': 'recent'}),
|
('', ['recent'], {'action': 'recent'}),
|
||||||
('', ['-l'], {'action': 'list_categories'}),
|
('', ['list_categories'], {'action': 'list_categories'}),
|
||||||
('', ['--list_sorts'], {'action': 'list_sorts'}),
|
('', ['list_sorts'], {'action': 'list_sorts'}),
|
||||||
('', ['term'], {'action': 'search', 'source': 'tpb'}),
|
('', ['search', 'term'], {'action': 'search', 'source': 'tpb'}),
|
||||||
('', ['-L', 'filename', 'term'], {'action': 'search', 'source': 'local_tpb', 'database': 'filename'}),
|
('', ['-L', 'filename', 'search', 'term'], {'action': 'search', 'source': 'local_tpb', 'database': 'filename'}),
|
||||||
('', ['term', '-S', 'dir'], {'action': 'search', 'save_directory': 'dir'}),
|
('', ['-S', 'dir', 'search', 'term'], {'action': 'search', 'save_directory': 'dir'}),
|
||||||
('', ['-P', '1337'], {'transmission_command': ['transmission-remote', '1337']}),
|
('', ['-P', '1337', 'search', 'term'], {'transmission_command': ['transmission-remote', '1337']}),
|
||||||
('', ['term'], {'output': 'browser_open'}),
|
('', ['search', 'term'], {'output': 'browser'}),
|
||||||
('', ['term', '-t'], {'output': 'transmission'}),
|
('', ['-o', 'transmission', 'search', 'term'], {'output': 'transmission'}),
|
||||||
('', ['term', '--save-magnets'], {'output': 'save_magnet_files'}),
|
('', ['-o', 'magnet', 'search', 'term'], {'output': 'magnet'}),
|
||||||
('', ['term', '--save-torrents'], {'output': 'save_torrent_files'}),
|
('', ['-o', 'torrent', 'search', 'term'], {'output': 'torrent'}),
|
||||||
('', ['term', '-C', 'command'], {'output': 'open_command', 'open_command': 'command'}),
|
('', ['-o', 'command', '-C', 'command', 'search', 'term'], {'output': 'command', 'command': 'command'}),
|
||||||
('', ['internets'], {'action': 'search', 'search': ['internets']}),
|
('', ['search', 'internets'], {'action': 'search', 'terms': ['internets']}),
|
||||||
('', ['internets lol', 'lel'], {'action': 'search', 'search': ['internets lol', 'lel']}),
|
('', ['search', 'internets lol', 'lel'], {'action': 'search', 'terms': ['internets lol', 'lel']}),
|
||||||
]
|
]
|
||||||
for test in tests:
|
for test in tests:
|
||||||
args = pirate.pirate.parse_args(test[1])
|
args = pirate.pirate.parse_args(test[1])
|
||||||
|
Loading…
Reference in New Issue
Block a user