-
Notifications
You must be signed in to change notification settings - Fork 0
/
kpopnet.py
executable file
·55 lines (40 loc) · 1.45 KB
/
kpopnet.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
#!/usr/bin/env pipenv run python
import sys
import argparse
from scrapy import signals
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
def main():
parser = argparse.ArgumentParser(
prog="kpopnet",
description="kpopnet web spiders and utils",
)
parser.add_argument("kastden", nargs="?")
args = parser.parse_args()
if args.kastden:
def process_spider_error(failure, response, spider):
nonlocal had_error
had_error = True
def process_spider_closed(spider, reason):
nonlocal had_error
if reason != "finished":
had_error = True
had_error = False
process = CrawlerProcess(get_project_settings())
crawler = process.create_crawler("kastden")
crawler.signals.connect(process_spider_error, signals.spider_error)
crawler.signals.connect(process_spider_closed, signals.spider_closed)
process.crawl(crawler)
process.start()
# XXX(Kagami): hackish way to catch exception in closed()
if crawler.stats and crawler.stats.get_value("log_count/ERROR", 0) != 0:
had_error = True
if had_error:
at = "@" * 50
print(f"\n{at}\nERROR OCCURED, PLEASE CHECK LOGS\n{at}", file=sys.stderr)
sys.exit(1)
else:
parser.print_usage(sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()