-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.py
71 lines (60 loc) · 1.93 KB
/
crawler.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
67
68
69
70
71
import argparse
import sys
import logging
import signal
import json
from common import crawl, print_details, group_by_name
def signal_handler(sig, frame):
print("Catched SIGINT, closing...")
exit(-1)
if __name__ == "__main__":
# general configs
signal.signal(signal.SIGINT, signal_handler)
logging.basicConfig(level=logging.INFO)
ap = argparse.ArgumentParser()
# 3 possible inputs: org, repo, list
ap.add_argument(
"-i",
"--input",
required=True,
help="Input type: 'org', 'repo', or 'list'."
)
# name of the org, repo, or list
ap.add_argument("name", help="Name of the organization or repository.")
# Output expected: print at stdout, file, db [NOT IMPLEMENTED]
ap.add_argument(
"-o",
"--output",
required=False,
default="print",
help="Output type: 'print' or 'db'. By default, prints to stdout.",
)
# Fields to print
ap.add_argument(
"-f",
"--fields",
required=False,
nargs="+",
default=["name", "license", "language"],
help="Fields to print: 'name', 'license', 'authors', etc.",
)
args = ap.parse_args()
if args.input not in ["org", "repo", "list"]:
logging.error("Invalid input type. Check --help.")
sys.exit(-1)
# TODO: implement db and file outputs
if args.output not in ["print", "file"]:
logging.error("Invalid output type. Check --help.")
sys.exit(-1)
results = crawl(args.input, args.name)
if not results:
logging.error("No results found. Exiting.")
sys.exit(-1)
# Files are always grouped, if you don't want it just > file_name
if args.input in ["org", "list"] and args.output == "file":
results = group_by_name(results)
with open("output.json", "w") as f:
json.dump(results, f, indent=4)
else:
print_details(results, args.fields)
sys.exit(0)