]> git.phdru.name Git - bookmarks_db.git/commitdiff
Feat: Delete bookmarks
authorOleg Broytman <phd@phdru.name>
Thu, 16 Nov 2023 07:27:26 +0000 (10:27 +0300)
committerOleg Broytman <phd@phdru.name>
Fri, 17 Nov 2023 10:58:35 +0000 (13:58 +0300)
delete [new file with mode: 0755]
delete.py [new file with mode: 0755]
doc/ANNOUNCE
doc/ChangeLog

diff --git a/delete b/delete
new file mode 100755 (executable)
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 <phd@phdru.name>"
+# __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 (executable)
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 <phd@phdru.name>"
+__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()
index b6712fa284a20a8980659b7feee406036b0544ee..921b420eb7527a32df0f1c2891890218b28b8ed5 100644 (file)
@@ -12,6 +12,8 @@ Version 5.0.0 (2023-??-??)
 
    Report redirects and set URLs.
 
+   Delete URLs.
+
    Remove BeautifulSoup.py (use globally installed).
 
 
index 0952548b7e1b56176ef94e3ddcb3ddade8e3e050..9bafe6b574f3586e388f57429d35aeda69041914 100644 (file)
@@ -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)