-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui.py
197 lines (166 loc) · 6.51 KB
/
ui.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
# -*- coding: utf-8 -*-
import web
import re
from web.contrib.template import render_mako
from count import entity_existence, prefixes, rev_prefixes
from show_counts import entity_found_in_items, entity_lookup, save_set, browse_set, get_dateline_url, item_stats
from getmetadata import titlelookup
from show_counts import get_entities_in_pid, entity_breakdown, get_ora_totals
from show_counts import get_top_dls
from datetime import datetime
from geolocate import get_gchart_map_for_pid
from urllib import unquote
import simplejson
from redis import Redis
urls = ("/", "usage",
"/geo/(.*)", "geo",
"/pid(/.*)?", "pid",
"/browse(/.*)?", "browse",
"/entity(/.*)?", "entity",
"/topten[/]?", "topten",
"/30dayschart[/]?", "chart",
)
app = web.application(urls, globals())
render = render_mako(
directories=['templates'],
input_encoding='utf-8',
output_encoding='utf-8',
)
r = Redis()
def determine_tagtype(tag):
if phrase in prefixes:
return "s" # set
elif phrase.count(":") == 1:
if phrase.startswith("uuid") or phrase.startswith("ora"):
return "i" # item
else:
return "e" # entity
else:
return "f" # unknown, try searching for the freetext as an entity
class topten:
def GET(self, format="html"):
options = web.input()
format = options.get("format") or format
if format in ['fragment','widget']:
return render.topten_widget(topten=get_top_dls(r))
elif format in ['html','htm']:
return render.topten_page(topten=get_top_dls(r))
else:
return simplejson.dumps(get_top_dls(r))
def POST(self):
params = web.input()
if "format" in params:
return self.GET(format=params.get("format","html"))
class chart:
def GET(self, format="html"):
options = web.input()
format = options.get("format") or format
report = simplejson.loads(r.get("analysis:current"))
report['humandate'] = datetime.strptime(report['now'].split(".")[0], "%Y-%m-%dT%H:%M:%S").strftime("%c")
if format in ['fragment', 'widget']:
return render.chart_widget(report=report)
elif format in ['html','htm']:
return render.chart_page(report=report)
else:
return simplejson.dumps(report)
def POST(self):
params = web.input()
if "format" in params:
return self.GET(format=params.get("format","html"))
class usage:
def GET(self):
report = simplejson.loads(r.get("analysis:current"))
return render.home(c = get_ora_totals(r), topten=get_top_dls(r), chart=report)
class entity:
def GET(self, id = None, format = "json"):
options = web.input()
id = id or options.get("entity")
if id:
if id.startswith("/"):
id = id[1:]
if id.find("%3A") != -1:
id = ":".join(id.split("%3A"))
format = options.get("format", format)
if id.count(":") == 1 and id.split(":")[0] in ['n','i','s','f','k','type','col','tt']:
tagtype = rev_prefixes.get(id.split(":")[0])
eb = entity_breakdown(id, r)
eb['eid'] = id
if format == "html":
return render.entity_view(entities={tagtype:eb})
return simplejson.dumps({tagtype:eb})
else:
results = {}
for tagtype in prefixes:
tag = entity_existence(id, tagtype, r)
if tag:
results[tagtype] = entity_breakdown(tag, r)
results[tagtype]['eid'] = tag
if format == "html":
return render.entity_view(entities=results)
return simplejson.dumps(results)
else:
return render.entity_form()
def POST(self, id):
params = web.input()
if "entity" in params:
return self.GET(id = params['entity'], format=params.get("format","html"))
class pid:
def GET(self, id = None, format = 'json'):
if id:
if id.startswith("/"):
id = id[1:]
id = unquote(id)
#if id.find("%3A") != -1:
# id = ":".join(id.split("%3A"))
params = {'pid':id}
params['label'] = titlelookup(id)
params['dlist'], params['dates'], params['total'] = item_stats(id, r)
if params['dlist']:
params['sparkline_url'] = get_dateline_url(params['dlist'], params['dates'])
else:
params['sparkline_url'] = "http://ora.ouls.ox.ac.uk/ora_logo.png"
params['entities'] = get_entities_in_pid(id, r)
options = web.input()
if 'geo' in options:
params['geousage_url'] = get_gchart_map_for_pid(id, r)
format = options.get('format', format)
if format == "html":
return render.item(**params)
return simplejson.dumps(params)
else:
return render.item_form()
def POST(self, id=None):
params = web.input()
if "pid" in params:
return self.GET(params['pid'], format=params.get('format', 'html'))
class browse:
def GET(self, id = None, format = "json", startswith=""):
options = web.input()
id = id or options.get("type")
if id:
if id.startswith("/"):
id = id[1:]
if id.find("%3A") != -1:
id = ":".join(id.split("%3A"))
format = options.get("format", format)
bset = browse_set(id, r, 0, startswith)
if format == "html":
return render.browse_set(bset=bset, startswith=startswith, settype=id)
return simplejson.dumps({'startswith':startswith,
'settype':id,
'bset':bset})
else:
return render.browse_form()
def POST(self, id):
params = web.input()
if "type" in params:
if "startswith" in params:
return self.GET(id = params['type'], format=params.get("format","html"), startswith=params.get("startswith",""))
return self.GET(id = params['type'], format=params.get("format","html"))
class geo:
def GET(self, id):
tagtype = determine_tagtype(id)
if tagtype == "s":
set_data = browse_set(id, r)
if __name__ == "__main__":
app.run()