1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-10 10:04:21 +01:00

New options system

Now there are 3 download methods:
* open magnet
* save magnets in a file
* save .torrent files

Each method can be used interactively or automatically via command line
options and/or config file
This commit is contained in:
Rnhmjoj 2015-03-26 00:13:39 +01:00
parent a18e6bbb9c
commit 3ef1c89a56

View File

@ -36,6 +36,8 @@ from html.parser import HTMLParser
from urllib.error import URLError, HTTPError from urllib.error import URLError, HTTPError
from socket import timeout from socket import timeout
from io import BytesIO from io import BytesIO
from os.path import expanduser, expandvars
colored_output = True colored_output = True
default_timeout = 10 default_timeout = 10
@ -277,12 +279,39 @@ def local(db, search):
return parser.results return parser.results
# load user options, to override default ones def load_config():
def config_to_load(): config = configparser.ConfigParser()
if os.path.isfile(os.path.expandvars('$XDG_CONFIG_HOME/pirate-get')):
return os.path.expandvars('$XDG_CONFIG_HOME/pirate-get') # default options
else: config.add_section('Save')
return os.path.expanduser('~/.config/pirate-get') config.set('Save', 'magnets', 'false')
config.set('Save', 'torrents', 'false')
config.set('Save', 'directory', expanduser('~/downloads/pirate-get'))
config.add_section('LocalDB')
config.set('LocalDB', 'enabled', 'false')
config.set('LocalDB', 'path', expanduser('~/downloads/pirate-get/db'))
config.add_section('Misc')
config.set('Misc', 'openCommand', '')
config.set('Misc', 'transmission', 'false')
config.set('Misc', 'colors', 'true')
# user-defined config files
main = expandvars('$XDG_CONFIG_HOME/pirate-get')
alt = expanduser('~/.config/pirate-get')
# read config file
config.read([main] if os.path.isfile(main) else [alt])
# expand env variables
directory = expanduser(expandvars(config.get('Save', 'Directory')))
path = expanduser(expandvars(config.get('LocalDB', 'path')))
config.set('Save', 'Directory', directory)
config.set('LocalDB', 'path', path)
return config
def get_torrent(info_hash): def get_torrent(info_hash):
@ -402,17 +431,18 @@ def save_torrents(chosen_links, mags, folder):
print('There is no cached file for this torrent :(', color='ERROR') print('There is no cached file for this torrent :(', color='ERROR')
def save_magnets(chosen_links, mags, folder):
filename = os.path.join(folder, id_generator() + '.magnets')
print('Saving magnets to', filename)
with open(filename, 'w') as f:
for link in chosen_links:
url = mags[int(link)][0]
f.write(url + '\n')
def main(): def main():
# new ConfigParser config = load_config()
config = configparser.ConfigParser()
# default options so we don't die later
config.add_section('SaveToFile')
config.set('SaveToFile', 'enabled', 'false')
config.set('SaveToFile', 'directory',
os.path.expanduser('~/downloads/pirate-get/'))
config.read([config_to_load()])
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='finds and downloads torrents from the Pirate Bay') description='finds and downloads torrents from the Pirate Bay')
@ -434,9 +464,6 @@ def main():
parser.add_argument('--list_sorts', dest='list_sorts', parser.add_argument('--list_sorts', dest='list_sorts',
action='store_true', action='store_true',
help='list Sortable Types') help='list Sortable Types')
parser.add_argument('-t', dest='transmission',
action='store_true',
help='call transmission-remote to start the download')
parser.add_argument('--custom', dest='command', parser.add_argument('--custom', dest='command',
help='call custom command, %%s will be replaced with' help='call custom command, %%s will be replaced with'
'the url') 'the url')
@ -451,6 +478,15 @@ def main():
parser.add_argument('-a', dest='download_all', parser.add_argument('-a', dest='download_all',
action='store_true', action='store_true',
help='download all results') help='download all results')
parser.add_argument('-t', '--transmission',
action='store_true',
help='open magnets with transmission-remote')
parser.add_argument('-M', '--save-magnets',
action='store_true', default=False,
help='save magnets links into a file')
parser.add_argument('-T', '--save-torrents',
action='store_true', default=False,
help='save torrent files')
parser.add_argument('--color', dest='color', parser.add_argument('--color', dest='color',
action='store_false', default=True, action='store_false', default=True,
help='disable colored output') help='disable colored output')
@ -483,8 +519,13 @@ def main():
print(str(value), '\t', key, sep='', color=cur_color) print(str(value), '\t', key, sep='', color=cur_color)
return return
if args.database: if args.database or config.getboolean('LocalDB', 'enabled'):
mags = local(args.database, args.search) if args.database:
path = args.database
else:
path = config.get('LocalDB', 'path')
mags = local(path, args.search)
else: else:
mags, mirrors = [], [] mags, mirrors = [], []
try: try:
@ -535,8 +576,8 @@ def main():
try: try:
# Very permissive handling # Very permissive handling
# Check for any occurances or d, f, p s, or q # Check for any occurances or d, f, p, t, m, or q
cmd_code_match = re.search(r'([hdfpsq])', l, cmd_code_match = re.search(r'([hdfpmtq])', l,
flags=re.IGNORECASE) flags=re.IGNORECASE)
if cmd_code_match: if cmd_code_match:
code = cmd_code_match.group(0).lower() code = cmd_code_match.group(0).lower()
@ -557,7 +598,8 @@ def main():
if code == 'h': if code == 'h':
print('Options:', print('Options:',
'<links>: Download selected torrents', '<links>: Download selected torrents',
'[s<links>]: Save torrent file', '[m<links>]: Save magnets in a file',
'[t<links>]: Save .torrent files',
'[d<links>]: Get descriptions', '[d<links>]: Get descriptions',
'[f<links>]: Get files', '[f<links>]: Get files',
'[p] Print search results', '[p] Print search results',
@ -571,9 +613,12 @@ def main():
print_file_lists(choices, mags, site, identifiers) print_file_lists(choices, mags, site, identifiers)
elif code == 'p': elif code == 'p':
print_search_results(mags, sizes, uploaded) print_search_results(mags, sizes, uploaded)
elif code == 's': elif code == 'm':
downloads = config.get('SaveToFile', 'directory') save_magnets(choices, mags,
save_torrents(choices, mags, downloads) config.get('Save', 'directory'))
elif code == 't':
save_torrents(choices, mags,
config.get('Save', 'directory'))
elif not l: elif not l:
print('No links entered!', color='WARN') print('No links entered!', color='WARN')
else: else:
@ -582,33 +627,36 @@ def main():
print('Exception:', e, color='ERROR') print('Exception:', e, color='ERROR')
choices = () choices = ()
if config.getboolean('SaveToFile', 'enabled'): save_to_file = False
# Save to file is enabled
fileName = os.path.expanduser(
config.get('SaveToFile','directory')
) + id_generator() + '.magnet'
print('Saving to File:', fileName) if args.save_magnets or config.getboolean('Save', 'magnets'):
print('Saving selected magnets...')
save_magnets(choices, mags, config.get('Save', 'directory'))
save_to_file = True
f = open(fileName, 'w') if args.save_torrents or config.getboolean('Save', 'torrents'):
for choice in choices: print('Saving selected torrents...')
choice = int(choice) save_torrents(choices, mags, config.get('Save', 'directory'))
url = mags[choice][0] save_to_file = True
f.write(url + '\n')
f.close()
else: if save_to_file:
# use transmission as default return
for choice in choices:
url = mags[int(choice)][0] for choice in choices:
print(url) url = mags[int(choice)][0]
if args.transmission:
os.system('transmission-remote --add "%s" ' % (url)) if args.transmission or config.getboolean('Misc', 'transmission'):
os.system('transmission-remote -l') os.system('transmission-remote --add "%s" ' % url)
elif args.command: os.system('transmission-remote -l')
os.system(args.command % (url))
else: elif args.command or config.get('Misc', 'openCommand'):
webbrowser.open(url) command = config.get('Misc', 'openCommand')
if args.command:
command = args.command
os.system(command % url)
else:
webbrowser.open(url)
if __name__ == '__main__': if __name__ == '__main__':