]> git.phdru.name Git - bookmarks_db.git/blob - set-urls.py
Fix(Robot): Stop splitting and un-splitting URLs
[bookmarks_db.git] / set-urls.py
1 #! /usr/bin/env python3
2 """Run through the bookmarks database and set URLs from redirects
3 from an external file
4
5 This file is a part of Bookmarks database and Internet robot.
6 """
7
8 from __future__ import print_function
9 import sys
10
11
12 __author__ = "Oleg Broytman <phd@phdru.name>"
13 __copyright__ = "Copyright (C) 2023, 2024 PhiloSoft Design"
14 __license__ = "GNU GPL"
15
16
17 def run():
18     from getopt import getopt
19     optlist, args = getopt(sys.argv[1:], "s")
20
21     report_stats = 1
22
23     for _opt, _arg in optlist:
24         if _opt == '-s':
25             report_stats = 0
26     try:
27         del _opt, _arg
28     except NameError:
29         pass
30
31     if report_stats:
32         print("Broytman set-urls, Copyright (C) 2023, 2024 PhiloSoft Design")
33
34     if len(args) != 1:
35         sys.stderr.write("Usage: set-urls [-s] urls_file")
36         sys.exit(1)
37
38     # Read the external file with URLs and build a mapping (URL => redirect)
39     urls_dict = {}
40
41     URL = None
42     redirect = None
43
44     urls_file = open(args[0], 'rt')
45     for line in urls_file:
46         line = line[:-1]  # strip trailing newline
47         if URL is None:
48             URL = line
49
50         elif redirect is None:
51             redirect = line
52
53         elif line:  # the third line in every 3 lines must be empty
54             raise ValueError(
55                 "line is not empty for URL `%s', redirect `%s': line `%s'"
56                 % (URL, redirect, line)
57             )
58
59         else:  # We've got 3 lines - add new entry to the mapping
60             if not redirect.startswith('https://') \
61                     and not redirect.startswith('http://'):
62                 raise ValueError("Redirect is not an URL: `%s'" % redirect)
63             if URL in urls_dict:
64                 if redirect != urls_dict[URL]:
65                     raise ValueError(
66                         "Redirects are not identical for URL `%s':"
67                         " `%s' != `%s'" % (URL, redirect, urls_dict[URL])
68                     )
69
70             else:
71                 urls_dict[URL] = redirect
72
73             # reset
74             URL = None
75             redirect = None
76
77     urls_file.close()
78
79     from storage import storage
80     storage = storage()
81
82     if report_stats:
83         sys.stdout.write("Loading %s: " % storage.filename)
84         sys.stdout.flush()
85
86     root_folder = storage.load()
87     from bkmk_objects import make_linear, break_tree
88     make_linear(root_folder)
89     objects = len(root_folder.linear)
90
91     if report_stats:
92         print("Ok")
93
94     # Run through the list of objects and check URLs/redirects
95     changed = 0
96     for object_no in range(objects):
97         object = root_folder.linear[object_no]
98
99         if object.isBookmark:
100             URL = object.href
101             if URL in urls_dict:
102                 redirect = urls_dict[URL]
103                 object.href = redirect
104                 changed += 1
105
106     if changed and report_stats:
107         sys.stdout.write("Saving %s: " % storage.filename)
108         sys.stdout.flush()
109
110     if not changed and report_stats:
111         sys.stdout.write("No need to save data\n")
112         sys.stdout.flush()
113
114     if changed:
115         break_tree(root_folder.linear)
116         storage.store(root_folder)
117
118     if changed and report_stats:
119         print("Ok")
120         print(objects, "objects passed")
121         print(changed, "objects changed")
122
123
124 if __name__ == '__main__':
125     run()