Writing to Freebase with (Iron)Python

My mom was in town visiting this weekend, and when I went to demo Freebase for her, I asked her to name a famous person. She suggested Pope John Paul II, so we looked him up. Most of the information that you would expect Freebase to know about him, it did. But there was one glaring omission: it didn’t know that he was Catholic.

Since Freebase knows 263 people who have ever been a professional Pope, I decided that it would take a little too long to update them each by hand. And so I set out to scribble up a few lines of Python to use the Freebase API libs for Python.

(Note for IronPython users: For help with the minor tricky steps of getting the Freebase API libraries working with IronPython, see my write-up over on Stack Overflow.)

Since I intended to write data to Freebase, I needed an authenticated account:

import freebase.api as fb

mss = fb.HTTPMetawebSession("sandbox.freebase.com")
# use be "www.freebase.com" once you've tested against sandbox

mss.username = "user"
mss.password = "password"
mss.login()

Next I needed to get a list of all of known Popes. For that, I used the following MQL Query:

query = [{‘profession’: {‘id’:‘/en/pope’},
          ‘type’:’/people/person’,
          ‘name’: None,
          ‘id’:None,
          ‘limit’:500}]

results = mss.mqlread(query)

This gave me an array of 263 results consisting of the English names and Freebase IDs of everyone who has ever had the profession of Pope. (I could have said ‘profession’: ‘Pope’ as the very first line of the query, but I chose to use the ID to make sure that there isn’t some other kind of Pope that I don’t know about.)

Next came the updates themselves. One safety feature built in to MQL is that you can only update one link per MQL Write, so I had to issue one update for each of the Popes returned from the read above:

for r in results:
    writequery = {
                     'id': r.id,
                     '/people/person/religion':
                          {
                          'connect':'insert',
                          'id':'/en/catholicism'
                          }
                 }
    mss.mqlwrite(writequery)

Each write took slightly under a second, and since I was running this interactively, the mqlwrite() call displayed the update status for each call. For most of the Popes, it had no idea about their religion, and so it replied:

{'/people/person/religion': {'connect': 'inserted', 'id': '/en/catholicism'},
 'id': '/en/pope_sisinnius'}

But for those that it did know:

{'/people/person/religion': {'connect': 'present', 'id': '/en/catholicism'},
 'id': '/en/pope_sisinnius'}

Post a Comment

Your email is never shared. Required fields are marked *

*
*