]> git.phdru.name Git - bookmarks_db.git/commitdiff
Add `get_url.py`: a script to get one file from an URL
authorOleg Broytman <phd@phdru.name>
Tue, 5 Mar 2024 23:47:23 +0000 (02:47 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 5 Mar 2024 23:48:37 +0000 (02:48 +0300)
Makefile
get_url.py [new file with mode: 0755]

index d982f0dfa23ff05b564bc317d5fbc9eaa97315ea..1994f9f7590cbc31a7c4e74ab44ddd5986cbdb5b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,7 @@ VERSION=5.2.5
 EXAMPLE_SHELL=\
    bkmk-add bkmk-add.py bkmk-chk bkmk-publish bkmk-rsync bkmk-sort bkmk2db \
    check_dups.py check_redirects.py check_title.py check_url.py \
-   delete delete.py hotexplode.pl mk-distr set-urls set-urls.py \
+   delete delete.py get_url.py hotexplode.pl mk-distr set-urls set-urls.py \
    set-real_title.py set-title-list set-title-list.py sort_db.py
 
 .PHONY: all
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()