forked from bloodcurdle/xA-Scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
179 lines (130 loc) · 4.43 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import time
import sys
import flags
import signal
import multiprocessing
import multiprocessing.managers
import threading
import logSetup
import rewrite.status_monitor
from apscheduler.schedulers.background import BackgroundScheduler
import rewrite.modules.da.daScrape as das
import rewrite.modules.fa.faScrape as fas
import rewrite.modules.hf.hfScrape as hfs
import rewrite.modules.px.pxScrape as pxs
import rewrite.modules.wy.wyScrape as wys
import rewrite.modules.ib.ibScrape as ibs
import rewrite.modules.sf.sfScrape as sfs
import rewrite.modules.artstation.asScrape as ass
import rewrite.modules.tumblr.tumblrScrape as tus
import rewrite.modules.patreon.patreonScrape as pts
import rewrite.modules.yiff_party.yiff_scrape as yps
from settings import settings
import cherrypy
class Nopper():
pluginName = "Nop Job"
def __init__(self):
pass
def go(self, *args, **kwargs):
pass
JOBS = [
(das.GetDA, settings["da"]["runInterval"], "da"),
(fas.GetFA, settings["fa"]["runInterval"], "fa"),
(hfs.GetHF, settings["hf"]["runInterval"], "hf"),
(wys.GetWy, settings["wy"]["runInterval"], "wy"),
(ibs.GetIb, settings["ib"]["runInterval"], "ib"),
(pxs.GetPX, settings["px"]["runInterval"], "px"),
(sfs.GetSf, settings["sf"]["runInterval"], "sf"),
# (ass.GetAs, settings["as"]["runInterval"], "as"),
(tus.GetTumblr, settings["tum"]["runInterval"], "tum"),
(pts.GetPatreon, settings["pat"]["runInterval"], "pat"),
# (yps.GetYp, settings["yp"]["runInterval"], "yp"),
(Nopper, settings["yp"]["runInterval"], "yp"),
]
import rewrite
def runScraper(scraper_class, managed_namespace):
print("Scheduler executing class: ", scraper_class)
instance = scraper_class()
instance.go(ctrlNamespace=managed_namespace)
def runServer():
cherrypy.tree.graft(rewrite.app, "/")
# Unsubscribe the default server
cherrypy.server.unsubscribe()
# Instantiate a new server object
server = cherrypy._cpserver.Server()
# Configure the server object
server.socket_host = "0.0.0.0"
server.socket_port = 6543
server.thread_pool = 30
server.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
def serverProcess(managedNamespace):
webThread = threading.Thread(target=runServer)
webThread.start()
while managedNamespace.serverRun:
time.sleep(0.1)
print("Stopping server.")
cherrypy.engine.exit()
print("Server stopped")
def scheduleJobs(sched, managedNamespace):
for scraperClass, interval, name in JOBS:
print(scraperClass, interval)
sched.add_job(runScraper, trigger='interval', seconds=interval, start_date='2014-1-4 0:00:00', name=name, args=(scraperClass, managedNamespace,))
# sched.add_interval_job(printWat, seconds=10, start_date='2014-1-1 01:00')
def go(managedNamespace):
print("Go()")
resetter = rewrite.status_monitor.StatusResetter()
resetter.resetRunState()
# statusMgr = manage.statusDbManager.StatusResource()
managedNamespace.run = True
managedNamespace.serverRun = True
server_process = multiprocessing.Process(target=serverProcess, args=(managedNamespace,))
if "debug" in sys.argv:
print("Not starting scheduler due to debug mode!")
sched = None
else:
sched = BackgroundScheduler({
'apscheduler.jobstores.default': {
'type': 'memory'
},
'apscheduler.executors.default': {
'class': 'apscheduler.executors.pool:ThreadPoolExecutor',
'max_workers': '5'
},
'apscheduler.job_defaults.coalesce': 'true',
'apscheduler.job_defaults.max_instances': '1',
})
scheduleJobs(sched, managedNamespace)
sched.start()
print("Scheduler is running!")
server_process.start()
loopCtr = 0
while managedNamespace.run:
time.sleep(0.1)
# if loopCtr % 100 == 0:
# for job in sched.get_jobs():
# statusMgr.updateNextRunTime(job.name, job.next_run_time.timestamp())
loopCtr += 1
if sched:
sched.shutdown()
server_process.join()
def mgr_init():
signal.signal(signal.SIGINT, signal.SIG_IGN)
print('initialized manager')
def signal_handler(dummy_signal, dummy_frame):
if flags.namespace.run:
flags.namespace.run = False
flags.namespace.serverRun = False
print("Telling threads to stop")
else:
print("Multiple keyboard interrupts. Raising")
raise KeyboardInterrupt
if __name__ == "__main__":
manager = multiprocessing.managers.SyncManager()
manager.start(mgr_init)
flags.namespace = manager.Namespace()
signal.signal(signal.SIGINT, signal_handler)
logSetup.initLogging()
go(flags.namespace)
manager.shutdown()