From: Oleg Broytman Date: Thu, 1 Aug 2024 04:34:11 +0000 (+0300) Subject: Feat(get_url): Parse args, save/print headers/body X-Git-Tag: 5.4.0~8 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=b927897aebe441578719d63512226f452963e382;p=bookmarks_db.git Feat(get_url): Parse args, save/print headers/body --- diff --git a/get_url.py b/get_url.py index 72046fc..996b829 100755 --- a/get_url.py +++ b/get_url.py @@ -8,6 +8,7 @@ __author__ = "Oleg Broytman " __copyright__ = "Copyright (C) 2024 PhiloSoft Design" __license__ = "GNU GPL" +import argparse import sys from bkmk_objects import Bookmark @@ -16,17 +17,17 @@ from bkmk_objects import Bookmark def run(): print("Broytman get_url, Copyright (C) 2024 PhiloSoft Design") - if len(sys.argv) != 3: - sys.stderr.write("Usage: get_url.py URL output_file\n") - sys.exit(1) + parser = argparse.ArgumentParser(description='Get URL') + parser.add_argument('-H', '--headers', help='file name to save headers') + parser.add_argument('-B', '--body', help='file name to save body') + parser.add_argument('URL', help='URL') + args = parser.parse_args() from robots import robot print("Using", robot) robot = robot(sys.stdout.write) - url = sys.argv[1] - output_fname = sys.argv[2] - + url = args.URL bookmark = Bookmark(href=url, add_date=None) bookmark.parent = None @@ -34,15 +35,23 @@ def run(): robot.smart_get(bookmark, url, True) if error: - print(error) + sys.stderr.write(error + '\n') elif redirect_code: print("Moved to: %s" % redirect_to) else: - print(headers) - with open(output_fname, 'wb') as outfile: - outfile.write(content) + if args.headers: + with open(args.headers, 'wb') as outfile: + outfile.write(str(headers).encode()) + else: + print(headers) + + if args.body: + with open(args.body, 'wb') as outfile: + outfile.write(content) + else: + print(content) robot.stop()