-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·63 lines (54 loc) · 2.53 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
#!/usr/bin/env python3
import os, sys, json, markdown, actions, math
from housepy import config, log, server, util, process, strings
process.secure_pid(os.path.abspath(os.path.join(os.path.dirname(__file__), "run")))
class Home(server.Handler):
def get(self, source=None, start=None, end=None, page=None):
self.set_header("Access-Control-Allow-Origin", "*")
if len(source):
page = None if page is None or not len(page) else strings.as_numeric(page)
if not len(start):
start = "*"
if not len(end):
end = "*"
try:
filters = {key: strings.as_numeric(value[0]) for (key, value) in self.request.arguments.items()}
results, start_t, end_t, count, page = actions.retrieve(self.db, source, start, end, filters, page)
data = {'query': {'sources': source, 'start': util.datestring(start_t, tz=config['tz']), 'end': util.datestring(end_t, tz=config['tz']), 'filters': filters}}
# log.info(data)
data['results'] = results
data['results_total'] = count
data['results_returned'] = len(results)
data['page'] = page
data['pages'] = math.ceil(count / 100)
return self.json(data)
except Exception as e:
log.error(log.exc(e))
return self.error("Request malformed: %s" % e)
readme = "README failed to load"
try:
with open(os.path.abspath(os.path.join(os.path.dirname(__file__), "README.md"))) as f:
text = f.read()
readme = markdown.markdown(text)
except Exception as e:
log.error(log.exc(e))
sources = self.db.entries.find().distinct('source')
return self.render("index.html", readme=readme, sources=sources)
def post(self, nop=None, nop2=None, nop3=None, nop4=None):
log.info("POST")
self.set_header("Access-Control-Allow-Origin", "*")
try:
data = json.loads(str(self.request.body, encoding='utf-8'))
except Exception as e:
log.error(log.exc(e))
return self.error()
try:
entry_id = actions.insert(self.db, data)
except Exception as e:
log.error(log.exc(e))
return self.error("ERROR: %s" % e)
return self.text(str(entry_id))
handlers = [
(r"/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)", Home),
]
server.start(handlers)