]> git.phdru.name Git - bookmarks_db.git/blob - check_url.py
Fix(robots): Store charset
[bookmarks_db.git] / check_url.py
1 #! /usr/bin/env python3
2 """Robot interface - check URLs from the command line
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) 2010-2023 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 import sys
12
13 from bkmk_objects import Bookmark
14 from Writers.bkmk_wflad import strftime
15
16 try:
17     import httplib
18 except ImportError:
19     pass
20 else:
21     HTTP = httplib.HTTP
22
23     class MyHTTP(HTTP):
24         def _setup(self, conn):
25             HTTP._setup(self, conn)
26             self.set_debuglevel(1)
27
28     httplib.HTTP = MyHTTP
29
30
31 def run():
32     print("Broytman check_url, Copyright (C) 2010-2023 PhiloSoft Design")
33
34     if len(sys.argv) < 2:
35         sys.stderr.write("Usage: check_url.py url1 [url2...]\n")
36         sys.exit(1)
37
38     from m_lib.flog import makelog
39     log = makelog("check.log")
40     log.outfile.reconfigure(encoding='utf-8')
41
42     from robots import robot
43     robot = robot(log)
44
45     for url in sys.argv[1:]:
46         bookmark = Bookmark(href=url, add_date=None)
47         bookmark.parent = None
48
49         rcode = robot.check_url(bookmark)
50         print("check_url: %s" % rcode)
51
52         if hasattr(bookmark, 'error'):
53             print(bookmark.error)
54
55         else:
56             print("""\
57   URL: %s
58   Title: %s
59   LastModified: %s
60   IconURI: %s
61   Icon: %s
62   Charset: %s
63   """ % (
64               bookmark.href, getattr(bookmark, 'real_title', ''),
65               strftime(bookmark.last_modified), bookmark.icon_href,
66               bookmark.icon, bookmark.charset))
67
68     robot.stop()
69     log.close()
70
71
72 if __name__ == '__main__':
73     run()