]> git.phdru.name Git - bookmarks_db.git/blobdiff - get_url.py
Add `get_url.py`: a script to get one file from an URL
[bookmarks_db.git] / get_url.py
diff --git a/get_url.py b/get_url.py
new file mode 100755 (executable)
index 0000000..d18e186
--- /dev/null
@@ -0,0 +1,72 @@
+#! /usr/bin/env python3
+"""Get one file from an URL
+
+This file is a part of Bookmarks database and Internet robot.
+"""
+
+__author__ = "Oleg Broytman <phd@phdru.name>"
+__copyright__ = "Copyright (C) 2024 PhiloSoft Design"
+__license__ = "GNU GPL"
+
+import sys
+
+from bkmk_objects import Bookmark
+from Writers.bkmk_wflad import strftime
+
+
+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)
+
+    from robots import robot
+    robot = robot(sys.stdout.write)
+
+    url = sys.argv[1]
+    output_fname = sys.argv[2]
+
+    bookmark = Bookmark(href=url, add_date=None)
+    bookmark.parent = None
+
+    error, redirect_code, redirect_to, headers, content = \
+        robot.get(bookmark, url, True)
+
+    if error:
+        print(error)
+
+    elif redirect_code:
+        print("Moved to: %s" % redirect_to)
+
+    else:
+        print("""\
+Title: %s
+URL: %s
+LastModified: %s
+Moved: %s
+Size: %s
+Md5: %s
+IconURI: %s
+Icon: %s
+Charset: %s
+""" % (
+          getattr(bookmark, 'real_title', None)
+          or getattr(bookmark, 'title', None),
+          bookmark.href,
+          strftime(bookmark.last_modified),
+          getattr(bookmark, 'moved', None),
+          getattr(bookmark, 'size', None),
+          getattr(bookmark, 'md5', None),
+          bookmark.icon_href, bookmark.icon, bookmark.charset,
+          )
+        )
+
+        with open(output_fname, 'wb') as outfile:
+            outfile.write(content)
+
+    robot.stop()
+
+
+if __name__ == '__main__':
+    run()