-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
262 lines (196 loc) · 5.08 KB
/
api.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import logging, os, signal, json, sys
import tornado.ioloop
import tornado.web
import tornado.httpserver
from conf import API_PORT, EXT_ID
from vars import STATUS_OK, STATUS_FAIL
from ISUtils.process_utils import getScrapers
from ISModels.schema import Schema
log_format = "%(asctime)s %(message)s"
def getConf():
try:
f = open(path_to_conf,'rb')
conf_ = json.loads(f.read())
f.close()
except IOError as e:
print e
conf_ = {}
return conf_
class Res():
def __init__(self):
self.result = STATUS_FAIL[0]
def emit(self):
return self.__dict__
class EngineHandler(tornado.web.RequestHandler):
def initialize(self, action):
self.action = action
def post(self, action):
res = Res()
if action == "sync":
try:
s = json.loads(self.request.body)
db_name = s['database']
del s['database']
except ValueError as e:
print e
self.finish(res.emit())
return
except KeyError as e:
print e
self.finish(res.emit())
return
db = None
if db_name == "M2X":
from ISData.m2xdb import M2XDB
db = M2XDB()
if db is not None:
db.updateConfig(s)
res.result = STATUS_OK[0]
else:
activate = False
if action == "start":
activate = True
res.data = {
"started" : [],
"stopped" : []
}
for scraper in getScrapers(scraper_dir):
s = Schema(scraper['url'])
if s.is_active != activate:
status = "started"
if not activate:
status = "stopped"
res.data[status].append(s._id)
s.activate(activate=activate)
res.result = STATUS_OK[0]
self.finish(res.emit())
class ConfigHandler(tornado.web.RequestHandler):
def get(self):
res = Res()
res.result = STATUS_OK[0]
conf_ = getConf()
sync = []
for key in conf_:
s = {
'vars' : [],
'database': key
}
for var in conf_[key].keys():
if var == "is_active":
s['is_active'] = conf_[key][var]
continue
s['vars'].append({
'key' : var,
'value' : conf_[key][var]
})
sync.append(s)
res.data = {
'sync' : sync,
'scrapers' : getScrapers(scraper_dir)
}
self.finish(res.emit())
def post(self):
res = Res()
try:
s = json.loads(self.request.body)
except ValueError as e:
print e
self.finish(res.emit())
return
id_ = s['id']
del s['id']
print "update config"
f = open(os.path.join(scraper_dir, id_, "conf.json"), 'rb')
schema = Schema(json.loads(f.read())['url'])
f.close()
should_activate = None
for key in s.keys():
if key == "is_active":
should_activate = s[key]
continue
if type(s[key]) == dict:
val = getattr(schema, key)
for key_ in s[key].keys():
val[key_] = s[key][key_]
schema.setattr(key, val)
else:
schema.setattr(key, s[key])
schema.save()
if should_activate is not None:
print "with activation: %s!" % should_activate
schema.activate(activate=should_activate)
res.result = STATUS_OK[0]
print res.emit()
self.finish(res.emit())
class MainHandler(tornado.web.RequestHandler):
def parseRequest(self):
print "parsing this request"
def validateRequest(self):
if self.request.headers['Origin'] == "chrome-extension://%s" % EXT_ID:
return True
return False
def get(self):
res = Res()
print "GET"
self.finish(res.emit())
def post(self):
res = Res()
if not self.validateRequest():
self.finish(res.emit())
return
try:
s = json.loads(self.request.body)['manifest']
except ValueError as e:
print e
self.finish(res.emit())
return
url = s['url']
del s['url']
as_test = False
try:
as_test = s['confirm']
print "found confirm directive: %s" % as_test
del s['confirm']
except KeyError as e:
print "no confirm directive"
print e
pass
as_test = True
if not as_test:
schema = Schema(url, create=True, **s)
schema.save()
self.finish(schema.activate())
else:
schema = Schema(url, create=False, as_test=True, **s)
print json.dumps(schema.emit())
self.finish(schema.scrape(as_test=True))
def put(self):
res = Res()
print "PUT"
self.finish(res.emit())
def delete(self):
res = Res()
print "DELETE"
self.finish(res.emit())
def terminationHandler(signal, frame):
sys.exit(0)
routes = [
(r'/', MainHandler),
(r'/config', ConfigHandler),
(r'/engine/(start|stop|sync)', EngineHandler, dict(action=None))
]
api = tornado.web.Application(routes)
signal.signal(signal.SIGINT, terminationHandler)
if __name__ == "__main__":
log_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
scraper_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "UserModels")
path_to_conf = os.path.join(os.path.dirname(os.path.abspath(__file__)), "conf.json")
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_file = os.path.join(log_dir, "api_log.txt")
logging.basicConfig(filename=log_file, format=log_format, level=logging.INFO)
logging.info("API Started.")
server = tornado.httpserver.HTTPServer(api)
server.bind(API_PORT)
server.start(5)
tornado.ioloop.IOLoop.instance().start()