From 0fc56e5cbd6dcd8837709be9ebb6facaa7fc687b Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Thu, 16 Nov 2023 10:27:26 +0300 Subject: [PATCH] Feat: Delete bookmarks --- delete | 16 ++++++++ delete.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ doc/ANNOUNCE | 2 + doc/ChangeLog | 2 + 4 files changed, 123 insertions(+) create mode 100755 delete create mode 100755 delete.py diff --git a/delete b/delete new file mode 100755 index 0000000..e28ddd7 --- /dev/null +++ b/delete @@ -0,0 +1,16 @@ +#! /bin/sh +# Delete bookmarks +# +# This file is a part of Bookmarks database and Internet robot. +# +# __author__ = "Oleg Broytman " +# __copyright__ = "Copyright (C) 2023 PhiloSoft Design" +# __license__ = "GNU GPL" + +. "`dirname \"$0\"`"/set-path + +delete.py "$@" || exit 1 + +BKMK_WRITER=flad db2bkmk.py && +convert_st.py json && +exec db2bkmk.py diff --git a/delete.py b/delete.py new file mode 100755 index 0000000..581aa7b --- /dev/null +++ b/delete.py @@ -0,0 +1,103 @@ +#! /usr/bin/env python3 +"""Run through the bookmarks database and delete bookmarks +listed in a file + +This file is a part of Bookmarks database and Internet robot. +""" + +from __future__ import print_function +import sys + + +__author__ = "Oleg Broytman " +__copyright__ = "Copyright (C) 2023 PhiloSoft Design" +__license__ = "GNU GPL" + + +def run(): + from getopt import getopt + optlist, args = getopt(sys.argv[1:], "s") + + report_stats = 1 + + for _opt, _arg in optlist: + if _opt == '-s': + report_stats = 0 + try: + del _opt, _arg + except NameError: + pass + + if report_stats: + print("Broytman delete bookmarks, Copyright (C)" + " 2023 PhiloSoft Design") + + if len(args) != 1: + sys.stderr.write("Usage: delete [-s] del_file\n") + sys.exit(1) + + # Read the file with bookmarks to delete + delete_urls = set() + + del_file = open(args[0], 'rt') + for line in del_file: + delete_urls.add(line[:-1]) # strip trailing newline + del_file.close() + + from storage import storage + storage = storage() + + if report_stats: + sys.stdout.write("Loading %s: " % storage.filename) + sys.stdout.flush() + + root_folder = storage.load() + from bkmk_objects import make_linear, break_tree + make_linear(root_folder) + objects = len(root_folder.linear) + + if report_stats: + print("Ok") + + # Run through the list of objects and store bookmarks indices + deleted = 0 + del_idx = [] + for object_no in range(objects): + object = root_folder.linear[object_no] + + if object.isBookmark: + if object.href in delete_urls: + del_idx.append(object_no) + deleted += 1 + + if deleted and report_stats: + sys.stdout.write("Saving %s: " % storage.filename) + sys.stdout.flush() + + if not deleted and report_stats: + sys.stdout.write("No need to save data\n") + sys.stdout.flush() + + if deleted: + for idx in reversed(del_idx): + bookmark = root_folder.linear[idx] + folder = bookmark.parent + folder_indices = [] + for object_no in range(len(folder)): + object = folder[object_no] + if object.isBookmark and object.href in delete_urls: + folder_indices.append(object_no) + for _idx in reversed(folder_indices): + del folder[_idx] + del root_folder.linear[idx] + break_tree(root_folder.linear) + storage.store(root_folder) + + if deleted and report_stats: + print("Ok") + print(objects, "objects passed") + print(deleted, "objects deleted") + + +if __name__ == '__main__': + run() diff --git a/doc/ANNOUNCE b/doc/ANNOUNCE index b6712fa..921b420 100644 --- a/doc/ANNOUNCE +++ b/doc/ANNOUNCE @@ -12,6 +12,8 @@ Version 5.0.0 (2023-??-??) Report redirects and set URLs. + Delete URLs. + Remove BeautifulSoup.py (use globally installed). diff --git a/doc/ChangeLog b/doc/ChangeLog index 0952548..9bafe6b 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -4,6 +4,8 @@ Version 5.0.0 (2023-??-??) Report redirects and set URLs. + Delete URLs. + Remove BeautifulSoup.py (use globally installed). Version 4.6.0 (2014-07-06) -- 2.39.5