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