writing a data set to Metaweb

[ permalink ] [ download ]
import metaweb         # Use the metaweb module

USERNAME = 'username'  # Put your Freebase username and password here
PASSWORD = 'password'

# The ID for our US State Quarter type depends on our username
TYPEID = '/user/' + USERNAME + '/default_domain/us_state_quarter'

# Make sure we can log in before we go any further
credentials = metaweb.login(USERNAME, PASSWORD)

# We will be creating multiple quarters in a single MQL query.
# We start with an empty array and add MQL writes to it in the loop below
query = []

# Open our file of quarter data
f = open("quarters.txt", "r")

# Loop through lines of the file
for line in f:              
    # Break each line into fields
    fields = line.strip().split(',')           

    # This query creates a single quarter
    q = {'create':'unless_exists',             # Create a new object
         'id':None,                            # And return its id
         'type':["/common/topic", TYPEID],     # Make it a topic and a quarter
         'name': fields[0] + ' State Quarter', # The object's name
         'state': fields[0],                   # State name
         'release': fields[1],                 # Release date
         'statehood': fields[2],               # Statehood date
         'mintage': int(fields[3])}            # How many minted

    # Add this write to the array of writes
    query.append(q);
    
# Close the data file
f.close()

# Now send our one big query to Metaweb and get the result
result = metaweb.write(query, credentials)

# Display the id of the Metaweb object for each state
for r in result:
    print "%s %s: %s" % (r['create'], r['state'], r['id'])
hits counter