-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_sources.py
62 lines (43 loc) · 1.46 KB
/
run_sources.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
"""
Run all sources to collect domain from registered sources.
Developed by Namjun Kim (bunseokbot@gmail.com)
"""
from apscheduler.schedulers.background import BackgroundScheduler
from utils.logging.log import Log
import source as sources
import time
def run(source):
_class = source()
status = _class.active
if _class.active:
Log.i("Trying to run {} source".format(_class.name))
try:
_class.collect()
except:
Log.e("Failed to collect data from {} source".format(_class.name))
if _class.urls:
_class.save()
else:
Log.i("{} source is now disabled".format(_class.name))
del _class
return status
def main():
"""Main method for running all sources."""
scheduler = BackgroundScheduler()
scheduler.start()
Log.i("{} source(s) detected!".format(len(sources.__all__)))
job_id = 1
for source in sources.__all__:
status = run(source) # initial run source.
if status:
# register a scheduler for running periodically. (only for active source)
scheduler.add_job(run, "interval",
minutes=source().cycle, id=str(job_id),
args=(source, ))
Log.i("Successfully add a new job")
job_id += 1
while True:
time.sleep(60) # sleep 1 mintue for running scheduler normally.
scheduler.shutdown()
if __name__ == "__main__":
main()