0

I know that I'm dyslexic and I don't have a PhD but I always find Google APIs incomprehensible.

I just need an example on the following: Get the '/music/genre' list and then get the subgenres ('/music/genre/subgenre') of the e.g. first genre. And then print the /common/topic/alias if any for each of them.

My code:

import json
import requests


query = json.dumps( [{'id': None, 'name': None, 'type': '/music/genre'}])
r = requests.get('https://www.googleapis.com/freebase/v1/mqlread?query=%s' % query )


for i in r.json()['result'][:1]:
    print requests.get('https://www.googleapis.com/freebase/v1/mqlread?query=%s' % json.dumps([i]) ).text

However the later print returns:

{"result": [
    {
        "id": "/en/classic_rock", 
        "type": "/music/genre", 
        "name": "Classic rock"
    }
]}

If I try to get the topic "/en/blues" as the example here with:

print requests.get('https://www.googleapis.com/freebase/v1/topic/en/blues').text

It returns a list related to /en/blues query.

I also tried the following to get directly the subgenre list:

for i in r.json()['result'][:1]:
    i['type'] = '/music/genre/subgenre'
    print requests.get('https://www.googleapis.com/freebase/v1/mqlread?query=%s' % json.dumps([i]) ).text

But returns an empty array.

Diolor
  • 13,181
  • 30
  • 111
  • 179

1 Answers1

0

You should work out you queries in the query editor before bothering with Python or another programming language. This query will give you a starting point:

[{
  "id": null,
  "name": null,
  "type": "/music/genre",
  "subgenre": [{
    "id": null,
    "name": null,
    "subgenre": [{
      "id": null,
      "name": null,
      "limit": 10
    }]
  }],
  "limit": 10
}]

The query does two levels of subgenres, which you actually wouldn't need programmatically because you'd presumably iterate over all instances of type /music/genre, capturing all parent & sub genres.

Actually, since there are only 283 genres, you could do this all in a single query, even though it's generally considered bad form to do such heavyweight queries:

[{
  "id": null,
  "name": null,
  "type": "/music/genre",
  "subgenre": [{
    "id": null,
    "name": null,
    "limit": 300
  }],
  "limit": 300
}]

will give you all 283 parent genres with all their children genres (ie all the parent-child node pairs or arcs).

Tom Morris
  • 10,490
  • 32
  • 53
  • Great! I played around and saw I can get the aliases with [this query](http://tinyurl.com/mzxzydc). Just one question. Since now the type is `"/common/topic"`, different than `"type": "/music/genre"` does it require a join action or something similar to get the aliases? – Diolor Dec 29 '13 at 15:46
  • Ok I got it. Just for the sake of completeness of the answer I put link of the query http://tinyurl.com/men8taa – Diolor Dec 29 '13 at 16:55
  • 1
    Just to keep everything in one place, no, you don't need to do any joins. It's a graph database with all properties available at each node. The only thing you may need to do is use a fully qualified property name (ie /common/topic/alias) if it's not a property of the type that you used earlier in your query. One other note, this will give you names/labels in English only. If you want other languages (which you might consider a form of alias), you can use `name : [{}]` – Tom Morris Dec 30 '13 at 15:49