Frage

How do I reproduce the following MQL query with the Google API Client Library for Python( http://wiki.freebase.com/wiki/Google_API_Client_Libraries#Python)

https://api.freebase.com/api/service/mqlread?queries={"q1":[{"name~=":"*doubt*","name": None,"type": "/media_common/quotation","author": [{"name": "William Shakespeare"}]}], "q2":[{"name~=":"*law*","name": None,"type": "/media_common/quotation","author": [{"name": "William Shakespeare"}]}]}

The following single query works:

from apiclient import discovery
from apiclient import model
import json
from pprint import pprint

DEVELOPER_KEY = ''

model.JsonModel.alt_param = ""
freebase = discovery.build('freebase', 'v1', developerKey=DEVELOPER_KEY)
query = [{"name~=":"*doubt*","name": None,"type": "/media_common/quotation","author": [{"name": "William Shakespeare"}]}]
response = json.loads(freebase.mqlread(query=json.dumps(query)).execute())
pprint (response)

I do not see how I can change the parameter "query" to "queries"

War es hilfreich?

Lösung

The queries parameter is gone in the new API. You can instead use the lightly documented RPC interface in a manner similar to this:

import urllib
import urllib2
import json

url = 'https://www.googleapis.com/rpc'
requests = [{
  'method': 'freebase.text.get', 
  'apiVersion': 'v1', 
  'params': {
   'id': ['en','bob_dylan']
  }
},{
  'method': 'freebase.text.get', 
  'apiVersion': 'v1', 
  'params': {
    'id': ['en','blade_runner']
  }
}]
headers = { 'Content-Type': 'application/json' }
req = urllib2.Request(url, json.dumps(requests), headers)
response = urllib2.urlopen(req)
print response.read()

[Code snippet courtesy of Shawn Simister, Google dev rel]

Andere Tipps

In the next release (version 1.0 beta 9) of the Google API Python Client you'll be able to batch together multiple queries like this:

from apiclient import discovery, model
from apiclient.http import BatchHttpRequest
import json

DEVELOPER_KEY = open('DEVELOPER_KEY').read()
query1 = [{"name~=":"*doubt*","name":None,"type":"/media_common/quotation","author":[{"name":"William Shakespeare"}]}]
query2 = [{"name~=":"*law*","name":None,"type":"/media_common/quotation","author":[{"name":"William Shakespeare"}]}]

model.JsonModel.alt_param = ""
freebase = discovery.build('freebase', 'v1', developerKey=DEVELOPER_KEY)

def display_results(request_id, response):
    for topic in response['result']:
        print topic['name']

batch = BatchHttpRequest(callback=display_results)
batch.add(freebase.mqlread(query=json.dumps(query1)))
batch.add(freebase.mqlread(query=json.dumps(query2)))
batch.execute(http)

Using this technique you can combine multiple queries,even MQL queries with search queries and it will only create one HTTP request.

Assuming the discovery document includes the queries parameter, I believe it would be:

queries = {"q1":[{"name~=":"*doubt*","name": None,"type": "/media_common/quotation","author": [{"name": "William Shakespeare"}]}], "q2":[{"name~=":"*law*","name": None,"type": "/media_common/quotation","author": [{"name": "William Shakespeare"}]}]}
response = json.loads(freebase.mqlread(queries=json.dumps(queries)).execute())
pprint (response)

The clients use a discovery document to figure out which parameter each API method accepts. So just changing the name of the python parameter should do the trick.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top