This repository has been archived by the owner on Apr 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·66 lines (54 loc) · 2.09 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python3
"""
extracts tags from website and generates new website
from these tags
----------USAGE----------
-h --help show this help-page
-s --source <arg> specify source_url e.g. -s https://www.github.com
-t --tags '<arg1> <arg..n>' specify html tags to get from page e.g. -t "img p h1 h2"
-o --output <arg> specify output filename e.g. -o index.html
-d --debug inserts test values in required fields
-------------------------
"""
import getopt
import sys
from website_parser import WebsiteParser
from parser_properties import ParserProperties
from website_generator import WebsiteGenerator
def print_user_error():
print(__doc__)
print("source and tags are required parameter")
sys.exit(0)
def main():
properties = ParserProperties()
properties.output_filename = "index.html"
try:
opts, args = getopt.getopt(sys.argv[1:], "hs:t:o:d",
["help", "source=", "tags=", "output=", "debug"])
except getopt.GetoptError as err:
# print help information and exit:
print(err) # will print something like "option -a not recognized"
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
print(__doc__)
sys.exit(0)
elif o in ("-s", "--source"):
properties.source_url = a
elif o in ("-t", "--tags"):
properties.tags_to_download = a.split(' ')
elif o in ("-o", "--output"):
properties.output_filename = a
elif o in ("-d", "--debug"):
properties.source_url = "http://www.golem.de/"
properties.tags_to_download = ["img"]
else:
assert False, "unhandled option"
"""end program if required parameters aren't given"""
if properties.source_url is None or len(properties.tags_to_download) < 1:
print_user_error()
parser = WebsiteParser(properties)
generator = WebsiteGenerator(properties)
generator.generate(parser.html_tags_dict)
if __name__ == "__main__":
main()