]> git.phdru.name Git - bookmarks_db.git/blob - check_title.py
Fix(Robot): Stop splitting and un-splitting URLs
[bookmarks_db.git] / check_title.py
1 #! /usr/bin/env python3
2 """Check and show URLs in the bookmarks database where name != 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 from bkmk_objects import make_linear, quote_title, unquote_title
10
11
12 __author__ = "Oleg Broytman <phd@phdru.name>"
13 __copyright__ = "Copyright (C) 2002-2023 PhiloSoft Design"
14 __license__ = "GNU GPL"
15
16
17 def report_title(href, title, real_title):
18     if real_title is None:
19         real_title = "NO REAL TITLE!!!"
20     s = "%s\n%s\n%s\n" % (href, title, real_title)
21
22     if log_file:
23         log_file.write("%s\n" % s)
24     else:
25         print(s)
26
27
28 def run():
29     from getopt import getopt
30     optlist, args = getopt(sys.argv[1:], "sl:")
31
32     report_stats = 1
33     global log_file
34     log_filename = None
35
36     for _opt, _arg in optlist:
37         if _opt == '-s':
38             report_stats = 0
39         if _opt == '-l':
40             log_filename = _arg
41     try:
42         del _opt, _arg
43     except NameError:
44         pass
45
46     if report_stats:
47         print("Broytman check_title, Copyright (C) 2002-2023 PhiloSoft Design")
48
49     if args:
50         sys.stderr.write("check_title: too many arguments\n")
51         sys.stderr.write("Usage: check_title [-s] [-l logfile]\n")
52         sys.exit(1)
53
54     if log_filename:
55         log_file = open(log_filename, 'wt', encoding='utf-8')
56
57     from storage import storage
58     storage = storage()
59
60     if report_stats:
61         sys.stdout.write("Loading %s: " % storage.filename)
62         sys.stdout.flush()
63
64     root_folder = storage.load()
65     make_linear(root_folder)
66     objects = len(root_folder.linear)
67
68     if report_stats:
69         print("Ok")
70
71     changed_titles = no_titles = 0
72     for object_no in range(objects):
73         object = root_folder.linear[object_no]
74
75         if object.isBookmark:
76             if hasattr(object, "moved") or hasattr(object, "error") or \
77                     object.href.startswith('place:'):  # Firefox SmartBookmarks
78                 continue
79
80             if hasattr(object, "real_title") \
81                     and (object.real_title is not None):
82                 unquoted_title = unquote_title(quote_title(object.real_title))
83                 unquoted_name = unquote_title(object.name)
84                 if unquoted_name != unquoted_title:
85                     report_title(object.href, unquoted_name, unquoted_title)
86                     changed_titles += 1
87             else:
88                 report_title(object.href, object.name, None)
89                 no_titles += 1
90
91     if report_stats:
92         print(objects, "objects passed")
93         print(changed_titles, "changed titles")
94         print(no_titles, "no titles")
95
96
97 if __name__ == '__main__':
98     run()