From 18774bbee29bac0990a57587bb36aedf9249c69f Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Wed, 6 Mar 2024 02:47:23 +0300 Subject: [PATCH] Add `get_url.py`: a script to get one file from an URL --- Makefile | 2 +- get_url.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100755 get_url.py diff --git a/Makefile b/Makefile index d982f0d..1994f9f 100644 --- 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 index 0000000..d18e186 --- /dev/null +++ b/get_url.py @@ -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 " +__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() -- 2.39.5