]> git.phdru.name Git - bookmarks_db.git/blob - set-real_title.py
Fix(Robot): Stop splitting and un-splitting URLs
[bookmarks_db.git] / set-real_title.py
1 #! /usr/bin/env python3
2 """Run through the bookmarks database and set name to real title
3
4 This file is a part of Bookmarks database and Internet robot.
5 """
6
7 from __future__ import print_function
8 import sys
9
10
11 __author__ = "Oleg Broytman <phd@phdru.name>"
12 __copyright__ = "Copyright (C) 2002-2023 PhiloSoft Design"
13 __license__ = "GNU GPL"
14
15
16 def run():
17     from getopt import getopt
18     optlist, args = getopt(sys.argv[1:], "s")
19
20     report_stats = 1
21
22     for _opt, _arg in optlist:
23         if _opt == '-s':
24             report_stats = 0
25     try:
26         del _opt, _arg
27     except NameError:
28         pass
29
30     if report_stats:
31         print("Broytman set-real_title, Copyright (C)"
32               " 2003-2023 PhiloSoft Design")
33     if args:
34         sys.stderr.write("set-real_title: too many arguments\n")
35         sys.stderr.write("Usage: set-real_title [-s]\n")
36         sys.exit(1)
37
38     from storage import storage
39     storage = storage()
40
41     if report_stats:
42         sys.stdout.write("Loading %s: " % storage.filename)
43         sys.stdout.flush()
44
45     root_folder = storage.load()
46     from bkmk_objects import make_linear
47     make_linear(root_folder)
48     objects = len(root_folder.linear)
49
50     if report_stats:
51         print("Ok")
52
53     changed = 0
54     for object_no in range(objects):
55         object = root_folder.linear[object_no]
56
57         if object.isBookmark:
58             if not hasattr(object, "real_title"):
59                 continue
60
61             real_title = object.real_title
62             if not real_title:
63                 real_title = object.href
64             if object.name != real_title:
65                 object.name = real_title
66                 changed += 1
67
68     if changed and report_stats:
69         sys.stdout.write("Saving %s: " % storage.filename)
70         sys.stdout.flush()
71
72     if not changed and report_stats:
73         sys.stdout.write("No need to save data\n")
74         sys.stdout.flush()
75
76     if changed:
77         storage.store(root_folder)
78
79     if changed and report_stats:
80         print("Ok")
81         print(objects, "objects passed")
82         print(changed, "objects changed")
83
84
85 if __name__ == '__main__':
86     run()