Skip to content

Commit

Permalink
Merge pull request #152 from ArchiveTeam/topic/viewer_json_api
Browse files Browse the repository at this point in the history
viewer: Add JSON interface to search endpoint
  • Loading branch information
hannahwhy committed Feb 12, 2015
2 parents d79ed98 + 0a0258b commit 2f64496
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions viewer/archivebotviewer/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def __init__(self, database, debug=False, prefix='/'):
U(prefix + r'jobs/(\w?)', JobsHandler, name='jobs'),
U(prefix + r'job/([\w-]+)', JobHandler, name='job'),
U(prefix + r'costs', CostLeaderboardHandler, name='costs'),

U(prefix + r'api/v1/search.json', ApiSearchHandler, name='api-search'),
)

static_path = os.path.join(
Expand All @@ -39,21 +41,38 @@ def __init__(self, database, debug=False, prefix='/'):
class BaseHandler(tornado.web.RequestHandler):
pass

class SearchHandler(BaseHandler):
def do_search(self):
'''
Runs a search against the viewer's database, using the q query
parameter as the criteria.
class IndexHandler(BaseHandler):
def get(self):
search_results = tuple(self._search() or ())

self.render('index.html', search_results=search_results)

def _search(self):
If the query is omitted or there are no matches, returns ().
'''
query = self.get_argument('q', None)

if not query:
return
return ()

return (self.application.database.search(query) or ())

class IndexHandler(SearchHandler):
def get(self):
self.render('index.html', search_results=self.do_search())

class ApiSearchHandler(SearchHandler):
def get(self):
results = self.do_search()

return self.application.database.search(query)
def make_result(result):
return dict(
result_type=result[0],
job_id=result[1],
domain=result[2],
url=result[3]
)

self.write(dict(results=[make_result(r) for r in results]))

class ItemsHandler(BaseHandler):
def get(self):
Expand Down

0 comments on commit 2f64496

Please sign in to comment.