-
Notifications
You must be signed in to change notification settings - Fork 1
/
w8.py
executable file
·182 lines (143 loc) · 4.74 KB
/
w8.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
#!/usr/bin/env python
"""
Copyright Ben Gosney 2014-2016
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import json
import hashlib
import time
from datetime import datetime
from dateutil import relativedelta
from data.data import Data
from data.config import args
from flask import Flask, jsonify, url_for, request
from werkzeug.contrib.cache import SimpleCache
from functools import wraps
CACHE_TIMEOUT = 300
cache = SimpleCache()
def cached(timeout=5 * 60, key='view/%s'):
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
cache_key = key % request.path
rv = cache.get(cache_key)
if rv is not None:
return rv
rv = f(*args, **kwargs)
cache.set(cache_key, rv, timeout=timeout)
return rv
return decorated_function
return decorator
app = Flask(__name__)
def list_routes():
import urllib
output = {}
for rule in app.url_map.iter_rules():
doc = app.view_functions[rule.endpoint].__doc__
if rule.rule.startswith('/api/') and doc is not None:
output[rule.rule] = doc.strip()
return output
def _pretty_date_format(qty, quantifier):
return "%d %s%s ago" % (qty, quantifier, "s" if qty > 1 else "")
def pretty_date(obj_time=False):
dt1 = datetime.fromtimestamp(time.time()) # 1973-11-29 22:33:09
dt2 = datetime.fromtimestamp(obj_time) # 1977-06-07 23:44:50
rd = relativedelta.relativedelta (dt1, dt2)
if rd.years > 0:
return _pretty_date_format(rd.years, "year")
if rd.months > 0:
return _pretty_date_format(rd.months, "month")
if rd.days > 0:
return _pretty_date_format(rd.days, "day")
if rd.hours > 0:
return _pretty_date_format(rd.hours, "hour")
if rd.minutes > 0:
return _pretty_date_format(rd.minutes, "minute")
if rd.seconds > 0:
return _pretty_date_format(rd.seconds, "second")
return "just now"
base_path = '/api/v1.0/'
@app.route(base_path)
@cached(86400)
def api():
"""
List the api calls
"""
return jsonify({
'success': True,
'version': 1.0,
'methods': list_routes(),
})
#@cached(60)
@app.route(base_path + 'domain/list/')
def domains():
"""
List the domains that have errors
"""
errors_obj = Data(args)
domains = errors_obj.domains()
json_domains = []
for d in domains:
json_domains.append(d.__dict__)
return jsonify({'domains': json_domains,
'hash': hashlib.md5(str(json_domains)).hexdigest()})
@app.route(base_path + 'domain/<string:host>/errors/')
@app.route(base_path + 'domain/<string:host>/errors/<int:mode>/')
def errors(host, mode=1):
"""
List the errors for a domain
"""
errors_obj = Data(args)
errs = errors_obj.errors(host, mode)
json_errs = []
for e in errs:
err_dict = e.__dict__
for k,v in err_dict.items():
try:
json.dumps(v)
except (UnicodeDecodeError) as e:
err_dict[k] = None
err_dict['ptime'] = pretty_date(err_dict['time'])
err_dict['iso_time'] = datetime.fromtimestamp(err_dict['time']).isoformat()
json_errs.append(err_dict)
return json.dumps({'errors': json_errs, 'hash': hashlib.md5(str(json_errs)).hexdigest()})
#@cached(86400)
@app.route(base_path + 'levels/')
def error_levels():
"""
List the error levels
"""
errors_obj = Data(args)
return jsonify({'levels': errors_obj.levels.keys})
@app.route(base_path + 'delete/<int:id>/')
@app.route(base_path + 'delete/group/<int:id>/', defaults={'group': True})
def delete_error(id, group=False):
"""
Deletes an error based on ID
"""
errors_obj = Data(args)
try:
error = errors_obj.get_error(id)
except LookupError:
return jsonify({'success': False})
if group:
errors_obj.delete_type(error)
else:
errors_obj.delete_entry(error)
return jsonify({'success': True})
#@cached(86400)
@app.route('/')
def template():
return open('data/template.html').read(100000)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')