-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (61 loc) · 2.41 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
67
68
69
70
71
72
73
74
"""The entry point to execute job_crawler
"""
import argparse
import time
from crawler import yourattor_crawler, onezfour_crawler
from worker import Worker
def worker_runner(args: argparse.Namespace, keyword: str) -> Worker:
"""Setup worker runner
Args:
args (argparse.Namespace): a object contain arguments of command
keyword (str): a keyword let crawler to filter result
Returns:
Worker: Worker class
"""
try:
setup_crawler = []
worker = Worker()
yourator = yourattor_crawler.YourattorCrawler(filter_keyword=keyword)
onezfour = onezfour_crawler.OneZaroFourCrawler(filter_keyword=keyword)
if args.yourattor:
setup_crawler.append(yourator)
elif args.onezfour:
setup_crawler.append(onezfour)
elif args.all:
setup_crawler.extend([yourator, onezfour])
worker.setup_crawler(setup_crawler)
return worker
except Exception:
raise
def main(args: argparse.Namespace) -> None:
"""Setup and execute worker_runner
Args:
args (argparse.Namespace): a object contain arguments of command
"""
try:
keyword = ''.join(args.keyword)
worker = worker_runner(args, keyword)
while True:
if args.enable_db:
worker.run_crawler_in_db()
else:
res = worker.run_crawler()
print('Crawler result:\n')
print(res)
if not args.interval:
break
time.sleep(args.interval)
except Exception:
raise
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Run Job crawler.')
parser.add_argument('-k', '--keyword', metavar='keyword to search',
type=str, help='an filter keyword for crawling', required=True)
parser.add_argument('-i', '--interval', metavar='seconds',
type=int, help='set interval running time', default=0)
parser.add_argument('-a', '--all', help='select all target source', action='store_true', default=True)
parser.add_argument('--yourattor', help='select yourattor as target source', action='store_true')
parser.add_argument('--onezfour', help='select 104 as target source', action='store_true')
parser.add_argument('--enable_db', help='enable save in database', action='store_true')
args = parser.parse_args()
main(args)