-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·44 lines (36 loc) · 1.24 KB
/
main.py
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
#!/usr/bin/env python3
import argparse
import io
import sys
from contextlib import contextmanager
from generators import Generator
from plugins import load_plugins
def find_parser(url, args):
plugins = load_plugins()
for plugin in plugins:
if plugin.can_handle(url):
return plugin.get_parser(url, args)
raise Exception("No plugin for URL: %s" % url)
@contextmanager
def smart_open(file=None):
if file and file != '-':
fd = io.open(file, mode="wb")
else:
fd = sys.stdout.buffer
try:
yield fd
finally:
if fd is not sys.stdout.buffer:
fd.close()
if __name__ == '__main__':
arguments = argparse.ArgumentParser(description="Generate RSS for sites.")
arguments.add_argument("-o", "--output", default='-',
help="output file name (default: stdout)")
arguments.add_argument("url", metavar="URL", help="site URL")
arguments.add_argument("parser_args", metavar="ARGS", nargs="*",
help="parser specific arguments")
args = arguments.parse_args()
parser = find_parser(args.url, args.parser_args)
generator = Generator(parser)
with smart_open(args.output) as fd:
generator.write_xml(fd)