]> git.phdru.name Git - bookmarks_db.git/blob - get_url.py
Fix(Robot): Stop splitting and un-splitting URLs
[bookmarks_db.git] / get_url.py
1 #! /usr/bin/env python3
2 """Get one file from an URL
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) 2024 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 import sys
12
13 from bkmk_objects import Bookmark
14
15
16 def run():
17     print("Broytman get_url, Copyright (C) 2024 PhiloSoft Design")
18
19     if len(sys.argv) != 3:
20         sys.stderr.write("Usage: get_url.py URL output_file\n")
21         sys.exit(1)
22
23     from robots import robot
24     robot = robot(sys.stdout.write)
25
26     url = sys.argv[1]
27     output_fname = sys.argv[2]
28
29     bookmark = Bookmark(href=url, add_date=None)
30     bookmark.parent = None
31
32     error, redirect_code, redirect_to, headers, content = \
33         robot.get(bookmark, url, True)
34
35     if error:
36         print(error)
37
38     elif redirect_code:
39         print("Moved to: %s" % redirect_to)
40
41     else:
42         with open(output_fname, 'wb') as outfile:
43             outfile.write(content)
44
45     robot.stop()
46
47
48 if __name__ == '__main__':
49     run()