Add curl/wget style --download option.

This commit is contained in:
Solderpunk 2023-11-19 15:24:26 +01:00
parent 305f7f9f2c
commit cbbc410976
1 changed files with 21 additions and 0 deletions

21
av98.py
View File

@ -1394,6 +1394,8 @@ def main():
parser = argparse.ArgumentParser(description='A command line gemini client.')
parser.add_argument('--bookmarks', action='store_true',
help='start with your list of bookmarks')
parser.add_argument('--dl', '--download', action='store_true',
help='download a single URL and quit')
parser.add_argument('--tls-cert', metavar='FILE', help='TLS client certificate file')
parser.add_argument('--tls-key', metavar='FILE', help='TLS client certificate private key file')
parser.add_argument('--restricted', action="store_true", help='Disallow shell, add, and save commands')
@ -1411,6 +1413,25 @@ def main():
# Instantiate client
gc = GeminiClient(args.restricted)
# Handle --download
if args.dl:
gc.onecmd("set debug True")
# Download
gi = GeminiItem(args.url[0])
gi, mime = gc._fetch_over_network(gi)
# Parse gemtext in the hopes of getting a gi.name for the filename
if mime == "text/gemini":
gc.active_raw_file = gc.raw_file_buffer
gc._handle_gemtext(gi)
# Copy from temp file to pwd with a nice name
filename = gi.derive_filename(mime)
shutil.copyfile(gc.raw_file_buffer, filename)
size = os.path.getsize(filename)
# Notify user where the file ended up
print("Wrote %d byte %s response to %s." % (size, mime, filename))
gc.do_quit()
sys.exit()
# Process config file
rcfile = os.path.join(gc.config_dir, "av98rc")
if os.path.exists(rcfile):