A special request from stan, here are the codes...
Python script to query Freebase [download]:
from django.utils import simplejson
from freebase.api import HTTPMetawebSession, MetawebError
mss = HTTPMetawebSession('www.freebase.com')
print 'Content-Type: application/x-suggestions+json; charset=UTF-8'
form = cgi.FieldStorage()
queryString = form['q'].value
try:
results = mss.search(queryString, 'false', '10', '0', 'html')
suggestions = [queryString]
terms = []
for result in results:
terms.append(result.name)
suggestions.append(terms)
print simplejson.dumps(suggestions)
except Exception:
print "<An error occured.>"
Additions to freebase-python API (session.py) [download]:
def search(self, _query, _strict, _limit, _start, _escape):
"""search Freebase"""
service = '/api/service/search'
self.log.info('%s: %s',
service,
Delayed(logformat, _query))
r = self._httpreq_json(service, form=dict(query=_query,strict=_strict,limit=_limit,start=_start,escape=_escape))
return self._mqlresult(r)
Please feel free to point out any areas that I can improve. I'm still trying to resolve the issue whereby the suggestions do not return quickly enough.
Note: The text is truncated at the end. I will post a link to the source codes later. My apologies. [Updated] Download links now available.
No comments:
Post a Comment