]> git.phdru.name Git - bookmarks_db.git/blob - check_dups.py
Fix(Robot): Stop splitting and un-splitting URLs
[bookmarks_db.git] / check_dups.py
1 #! /usr/bin/env python
2 """Check duplicate URLs in the bookmarks database
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) 2000-2017 PhiloSoft Design"
13 __license__ = "GNU GPL"
14
15
16 log_file = None
17
18 def report_dup(href, object_no):
19     s = "Duplicate URL: %s (first at rec. %d)" % (href, object_no)
20
21     if log_file:
22         log_file.write("%s\n" % s)
23     else:
24         print(s)
25
26
27 def run():
28     from getopt import getopt
29     optlist, args = getopt(sys.argv[1:], "sl:")
30
31     report_stats = 1
32     global log_file
33     log_filename = None
34
35     for _opt, _arg in optlist:
36         if _opt == '-s':
37             report_stats = 0
38         if _opt == '-l':
39             log_filename = _arg
40     try:
41         del _opt, _arg
42     except NameError:
43         pass
44
45     if report_stats:
46         print("Broytman check_dups, Copyright (C) 2000-2017 PhiloSoft Design")
47
48     if args:
49         sys.stderr.write("check_urls: too many arguments\n")
50         sys.stderr.write("Usage: check_urls [-s] [-l logfile]\n")
51         sys.exit(1)
52
53     if log_filename:
54         log_file = open(log_filename, 'w')
55
56     from storage import storage
57     storage = storage()
58
59     if report_stats:
60         sys.stdout.write("Loading %s: " % storage.filename)
61         sys.stdout.flush()
62
63     root_folder = storage.load()
64     from bkmk_objects import make_linear
65     make_linear(root_folder)
66     objects = len(root_folder.linear)
67
68     if report_stats:
69         print("Ok")
70
71
72     dup_dict = {}
73
74     for object_no in range(objects):
75         object = root_folder.linear[object_no]
76
77         if object.isBookmark:
78             href = object.href
79             if dup_dict.has_key(href):
80                 report_dup(href, dup_dict[href])
81             else:
82                 dup_dict[href] = object_no
83
84
85     if log_filename:
86         log_file.close()
87
88     if report_stats:
89         print("Ok")
90         print(objects, "objects passed")
91
92
93 if __name__ == '__main__':
94     run()