1 #! /usr/bin/env python3
2 """Run through the bookmarks database and set URLs from redirects
5 This file is a part of Bookmarks database and Internet robot.
8 from __future__ import print_function
12 __author__ = "Oleg Broytman <phd@phdru.name>"
13 __copyright__ = "Copyright (C) 2023 PhiloSoft Design"
14 __license__ = "GNU GPL"
18 from getopt import getopt
19 optlist, args = getopt(sys.argv[1:], "s")
23 for _opt, _arg in optlist:
32 print("Broytman set-URLs, Copyright (C) 2023 PhiloSoft Design")
35 sys.stderr.write("Usage: set-URLs [-s] urls_file")
38 # Read the external file with URLs and build a mapping (URL => redirect)
44 urls_file = open(args[0], 'rt')
45 for line in urls_file:
46 line = line[:-1] # strip trailing newline
50 elif redirect is None:
53 elif line: # the third line in every 3 lines must be empty
55 "line is not empty for URL `%s', redirect `%s': line `%s'"
56 % (URL, redirect, line)
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)
64 if redirect != urls_dict[URL]:
66 "Redirects are not identical for URL `%s':"
67 " `%s' != `%s'" % (URL, redirect, urls_dict[URL])
71 urls_dict[URL] = redirect
79 from storage import storage
83 sys.stdout.write("Loading %s: " % storage.filename)
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)
94 # Run through the list of objects and check URLs/redirects
96 for object_no in range(objects):
97 object = root_folder.linear[object_no]
102 redirect = urls_dict[URL]
103 object.href = redirect
106 if changed and report_stats:
107 sys.stdout.write("Saving %s: " % storage.filename)
110 if not changed and report_stats:
111 sys.stdout.write("No need to save data\n")
115 break_tree(root_folder.linear)
116 storage.store(root_folder)
118 if changed and report_stats:
120 print(objects, "objects passed")
121 print(changed, "objects changed")
124 if __name__ == '__main__':