-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
76 lines (60 loc) · 2.34 KB
/
api.py
File metadata and controls
76 lines (60 loc) · 2.34 KB
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
#!/usr/bin/env python
#
# JSON APIs for genesearch app
import json
import webapp2
import cse_api_client
from google.appengine.ext import db
# models
class QueryLogEntry(db.Model):
query = db.StringProperty()
date = db.DateTimeProperty(auto_now_add=True)
# NOTE(mwytock): These are really deltas representing the labeling actions
class Label(db.Model):
timestamp = db.DateTimeProperty(auto_now_add=True)
url = db.StringProperty()
add = db.StringListProperty()
remove = db.StringListProperty()
mode = db.CategoryProperty(choices=("page", "site"))
# handlers
class LogApi(webapp2.RequestHandler):
def post(self):
log_entry = QueryLogEntry(query=self.request.get("q"))
log_entry.put()
class AllQueries(webapp2.RequestHandler):
def get(self):
self.response.headers["Content-type"] = "text/text"
q = db.Query(QueryLogEntry).order("-date")
seen_queries = {}
for log_entry in q.run(limit=10000):
if log_entry.query in seen_queries:
continue
self.response.out.write(log_entry.query + "\n")
seen_queries[log_entry.query] = True
class RecentApi(webapp2.RequestHandler):
def get(self):
q = db.Query(QueryLogEntry).order("-date")
seen_queries = {}
recent = []
for log_entry in q.run(limit=1000):
if log_entry.query in seen_queries:
continue
recent.append({"query": log_entry.query})
seen_queries[log_entry.query] = True
if (len(seen_queries) > 30) :
break
self.response.headers["Cache-control"] = "no-store"
self.response.headers["Content-type"] = "application/json"
self.response.out.write(json.dumps({"recent": recent}))
class LabelApi(webapp2.RequestHandler):
def post(self):
label = Label(url=self.request.get("url"),
add=self.request.get_all("add"),
remove=self.request.get_all("remove"),
mode=self.request.get("mode"))
label.put()
cse_api_client.add_remove_labels(label)
app = webapp2.WSGIApplication([("/api/log", LogApi),
("/api/recent", RecentApi),
("/api/allQueries", AllQueries),
("/api/label", LabelApi)])