forked from mnot/redbot
/
redbot
executable file
·100 lines (77 loc) · 2.93 KB
/
redbot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
"""
CLI interface to REDbot
"""
__author__ = "Jerome Renard <jerome.renard@gmail.com>"
__copyright__ = """\
Copyright (c) 2008-2012 Mark Nottingham
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import sys
assert sys.version_info[0] == 2 and sys.version_info[1] >= 6, \
"Please use Python 2.6 or greater"
from optparse import OptionParser
import thor
from redbot import droid
from redbot.formatter import find_formatter, available_formatters
lang = "en" # TODO: add as CL option
charset = "utf-8"
def main():
usage = """Usage: %prog [options] <url>"""
version = """Redbot version %s, http://redbot.org/ """ % droid.__version__
opt_parser = OptionParser(usage=usage, version=version)
opt_parser.set_defaults(
version=False,
descend=False,
output_format="txt",
show_recommendations=False
)
opt_parser.add_option(
"-a", "--assets",
action="store_true", dest="descend",
help="check assets, if the URL contains HTML"
)
opt_parser.add_option(
"-o", "--output-format",
action="store", dest="output_format",
help="one of: %s" % ", ".join(available_formatters())
)
(options, args) = opt_parser.parse_args()
if len(args) != 1:
opt_parser.error("Please specify a URL.")
if options.output_format not in available_formatters():
opt_parser.error("Unrecognised output format.")
url = args[0]
red = droid.InspectingResourceExpertDroid(
url,
descend=options.descend
)
formatter = find_formatter(options.output_format, 'txt', options.descend)(
sys.argv[0], url, [], lang, output,
tty_out = sys.stdout.isatty()
)
formatter.set_red(red.state)
formatter.start_output()
def done():
formatter.finish_output()
thor.stop()
red.run(done)
thor.run()
def output(out):
sys.stdout.write(out.encode(charset, 'replace'))
if __name__ == "__main__":
main()