]> git.phdru.name Git - bookmarks_db.git/blob - check_urls.py
TODO: Configuration file
[bookmarks_db.git] / check_urls.py
1 #! /usr/bin/env python
2 """Robot interface - check URLs from bookmarks database
3
4 This file is a part of Bookmarks database and Internet robot.
5 """
6
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2000-2012 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 import sys, os
12
13
14 def run():
15    from getopt import getopt
16    optlist, args = getopt(sys.argv[1:], "ise")
17
18    show_pbar = 1
19    report_stats = 1
20    only_errors = 0
21
22    for _opt, _arg in optlist:
23       if _opt == '-i':
24          show_pbar = 0
25       if _opt == '-s':
26          report_stats = 0
27       if _opt == '-e':
28          only_errors = 1
29    try:
30       del _opt, _arg
31    except NameError:
32       pass
33
34    if report_stats:
35       print "Broytman check_urls, Copyright (C) 1997-2010 PhiloSoft Design"
36
37    if args:
38       sys.stderr.write("check_urls: too many arguments\n")
39       sys.stderr.write("Usage: check_urls [-ise]\n")
40       sys.exit(1)
41
42    if show_pbar:
43       show_pbar = sys.stderr.isatty()
44
45    if show_pbar:
46       try:
47          from m_lib.pbar.tty_pbar import ttyProgressBar
48       except ImportError:
49          show_pbar = 0
50
51    from m_lib.flog import makelog, openlog
52    if only_errors:
53       log = openlog("check.log")
54       log("chk_urls restarted for errors")
55       if report_stats:
56          print "chk_urls restarted for errors"
57    else:
58       log = makelog("check.log")
59       log("check_urls started")
60       if report_stats:
61          print "   check_urls: normal start"
62
63    from storage import storage
64    storage = storage()
65
66    from robots import robot
67    robot = robot(log)
68
69    if report_stats:
70       sys.stdout.write("Loading %s: " % storage.filename)
71       sys.stdout.flush()
72
73    root_folder = storage.load()
74    from bkmk_objects import make_linear, break_tree
75    make_linear(root_folder)
76    objects = len(root_folder.linear)
77
78    if report_stats:
79       print "Ok"
80
81    if report_stats:
82       if only_errors:
83          s = "Rechecking errors: "
84       else:
85          s = "Checking: "
86       sys.stdout.write(s)
87       sys.stdout.flush()
88
89    if show_pbar:
90       pbar = ttyProgressBar(0, objects)
91
92    urls_no = 0
93    object_count = 0
94    size = 0
95
96    checked = {}
97    rcode = 1
98
99    for object_no in range(objects):
100       if show_pbar:
101          pbar.display(object_no+1)
102
103       object = root_folder.linear[object_no]
104       object_count = object_count + 1
105
106       if object.isBookmark:
107          href = object.href
108          if (href.startswith('place:') # Firefox SmartBookmarks
109                or '%s' in href): # Bookmark with keyword
110             log("Skipped %s" % href)
111             continue
112
113          if only_errors:
114             if hasattr(object, "error"):
115                delattr(object, "error")
116             else:
117                continue
118
119          if checked.has_key(href):
120             log("Already checked %s" % href)
121             old_object = root_folder.linear[checked[href]]
122             for attr_name in ("last_visit", "last_modified",
123                   "error", "no_error", "moved", "size", "md5", "real_title",
124                   "last_tested", "test_time", "icon", "charset"):
125                if hasattr(old_object, attr_name):
126                   setattr(object, attr_name, getattr(old_object, attr_name))
127          else:
128             log("Checking %s" % href)
129             rcode = robot.check_url(object)
130
131             if rcode:
132                checked[href] = object_no
133                urls_no = urls_no + 1
134                try:
135                   size = size + int(object.size)
136                except (AttributeError, TypeError, ValueError):
137                   pass # Some object does not have a size :(
138             else:
139                log("Interrupted by user (^C)")
140                break
141    robot.stop()
142
143    if show_pbar:
144       del pbar
145
146    if report_stats:
147       print "Ok"
148       print object_count, "objects passed"
149       print urls_no, "URLs checked"
150       print size, "bytes eaten"
151
152    break_tree(root_folder.linear)
153    storage.store(root_folder)
154
155    if rcode:
156       log("check_urls finished ok")
157    log.close()
158
159
160 if __name__ == '__main__':
161    run()