]> git.phdru.name Git - bookmarks_db.git/commitdiff
Feat(get_url): Parse args, save/print headers/body
authorOleg Broytman <phd@phdru.name>
Thu, 1 Aug 2024 04:34:11 +0000 (07:34 +0300)
committerOleg Broytman <phd@phdru.name>
Thu, 1 Aug 2024 04:34:11 +0000 (07:34 +0300)
get_url.py

index 72046fcf99b861952d7643306b536dd677a780e0..996b829ac0dc497629aa42c43404f3fd28ce95db 100755 (executable)
@@ -8,6 +8,7 @@ __author__ = "Oleg Broytman <phd@phdru.name>"
 __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()