From 980274a50fc90210d7bfa4ca361d791ee8515ede Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Tue, 6 Aug 2024 19:01:37 +0300 Subject: [PATCH] Feat(Robots): Do not return error from `check_url()` Break the entire program with `Ctrl-C`. --- Robots/bkmk_rforking.py | 5 +---- Robots/bkmk_robot_base.py | 9 +++------ bkmk-add.py | 38 +++++++++++++++++++------------------- check_urls.py | 3 +-- check_urls_db.py | 24 +++++++++--------------- 5 files changed, 33 insertions(+), 46 deletions(-) diff --git a/Robots/bkmk_rforking.py b/Robots/bkmk_rforking.py index 7b458df..c956d13 100644 --- a/Robots/bkmk_rforking.py +++ b/Robots/bkmk_rforking.py @@ -104,10 +104,7 @@ class robot_forking(Robot): sys.stderr.flush() except KeyboardInterrupt: - return 0 - - # Tested - return 1 + return def stop(self): stop_subp(None) # Stop subprocess; do not log restarting diff --git a/Robots/bkmk_robot_base.py b/Robots/bkmk_robot_base.py index b2e4442..6d3479a 100644 --- a/Robots/bkmk_robot_base.py +++ b/Robots/bkmk_robot_base.py @@ -107,11 +107,11 @@ class robot_base(Robot): if error is not None: bookmark.error = error - return 1 + return if http_status_code and (http_status_code >= 300): self.set_redirect(bookmark, http_status_code, redirect_to) - return 1 + return size = 0 last_modified = None @@ -321,7 +321,7 @@ class robot_base(Robot): except KeyboardInterrupt: self.log("Keyboard interrupt (^C)") - return 0 + return except socket.error as e: bookmark.error = get_error(e) @@ -336,9 +336,6 @@ class robot_base(Robot): finally: self.finish_check_url(bookmark) - # Tested - return 1 - def smart_get(self, bookmark, url, accept_charset=False): split_results = urlsplit(url) url_proto = split_results.scheme diff --git a/bkmk-add.py b/bkmk-add.py index 239ce4e..768af05 100755 --- a/bkmk-add.py +++ b/bkmk-add.py @@ -60,25 +60,25 @@ def run(): print("Using", robot) _robot = robot(None) - if _robot.check_url(bookmark): # get real title and last modified date - if title: # forced title - bookmark.name = title - elif hasattr(bookmark, "real_title"): - bookmark.name = bookmark.real_title - if report_stats: - sys.stdout.write("Adding %s with title '%s'\n" - % (href, bookmark.name)) - del bookmark.parent - root_folder.append(bookmark) - - if report_stats: - sys.stdout.write("Storing %s: " % storage.filename) - sys.stdout.flush() - - storage.store(root_folder) - - if report_stats: - print("Ok") + _robot.check_url(bookmark) # get real title and last modified date + if title: # forced title + bookmark.name = title + elif hasattr(bookmark, "real_title"): + bookmark.name = bookmark.real_title + if report_stats: + sys.stdout.write("Adding %s with title '%s'\n" + % (href, bookmark.name)) + del bookmark.parent + root_folder.append(bookmark) + + if report_stats: + sys.stdout.write("Storing %s: " % storage.filename) + sys.stdout.flush() + + storage.store(root_folder) + + if report_stats: + print("Ok") if __name__ == '__main__': diff --git a/check_urls.py b/check_urls.py index 0be8f56..9009d00 100755 --- a/check_urls.py +++ b/check_urls.py @@ -48,8 +48,7 @@ def run(): bookmark = Bookmark(href=url, add_date=None) bookmark.parent = None - rcode = robot.check_url(bookmark) - print("check_urls: %s" % rcode) + robot.check_url(bookmark) if hasattr(bookmark, 'error'): print(bookmark.error) diff --git a/check_urls_db.py b/check_urls_db.py index b8db639..dbab6bf 100755 --- a/check_urls_db.py +++ b/check_urls_db.py @@ -99,7 +99,6 @@ def run(): size = 0 checked = {} - rcode = 1 for object_no in range(objects): if show_pbar: @@ -134,18 +133,14 @@ def run(): getattr(old_object, attr_name)) else: log("Checking %s" % href) - rcode = robot.check_url(object) - - if rcode: - checked[href] = object_no - urls_no = urls_no + 1 - try: - size = size + int(object.size) - except (AttributeError, TypeError, ValueError): - pass # Some object does not have a size :( - else: - log("Interrupted by user (^C)") - break + robot.check_url(object) + + checked[href] = object_no + urls_no = urls_no + 1 + try: + size = size + int(object.size) + except (AttributeError, TypeError, ValueError): + pass # Some object does not have a size :( robot.stop() if show_pbar: @@ -161,8 +156,7 @@ def run(): del root_folder.linear storage.store(root_folder) - if rcode: - log("check_urls_db finished ok") + log("check_urls_db finished ok") log.close() -- 2.39.5