/guid/9202a8c04000641f8000000005c79b18 rename
author:
contributor:
published:
updated:
Jun 24, 2009 11:24:15PM UTC
source uri:
Summary
# NOTE: metaweb.py is deprecated. Use the freebase-python library instead.# Full information is at ...
Content
# NOTE: metaweb.py is deprecated. Use the freebase-python library instead.# Full information is at http://code.google.com/p/freebase-python/#========================================================================# Copyright (c) 2007, Metaweb Technologies, Inc.# All rights reserved.# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions# are met:# * Redistributions of source code must retain the above copyright# notice, this list of conditions and the following disclaimer.# * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following # disclaimer in the documentation and/or other materials provided# with the distribution.# # THIS SOFTWARE IS PROVIDED BY METAWEB TECHNOLOGIES ``AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL METAWEB TECHNOLOGIES BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.# ========================================================================## This is the full "metaweb.py" module from the Metaweb API documentation## In the documentation, each function is presented as a separate# example. This is the whole file.## If you find any errors or have suggestions for improving this module,# send them to the Freebase developers mailing list: developers@freebase.com# You can subscribe to the mailing list at http://lists.freebase.com/#import httplib import urllib # URL encoding import urllib2 # Higher-level URL content fetching import simplejson # JSON serialization and parsing import cookielib # Cookie handling import os## When experimenting, use the sandbox.freebase.com service.# Every Monday, sandbox.freebase.com is erased and it is updated # with a fresh copy of data from www.freebase.com. This makes# it an ideal place to experiment.#host = 'sandbox.freebase.com' # The Metaweb host readservice = '/api/service/mqlread' # Path to mqlread service loginservice = '/api/account/login' # Path to login service writeservice = '/api/service/mqlwrite' # Path to mqlwrite service uploadservice = '/api/service/upload' # Path to upload service searchservice = '/api/service/search' # Path to search servicecredentials = None # default credential from login()escape = False # default escape, set to 'html' for HTML escaping permission = None # default permission used when creating new objects debug = False # default debug setting # Install a CookieProcessorcookiefile = os.path.join(os.environ["HOME"], ".metaweb.cookies.txt")cookiejar = cookielib.LWPCookieJar()if os.path.isfile(cookiefile): cookiejar.load(cookiefile)urllib2.install_opener( urllib2.build_opener( urllib2.HTTPCookieProcessor(cookiejar)))# If anything goes wrong when talking to a Metaweb service, we raise MQLError.class MQLError(Exception): def __init__(self, value): # This is the exception constructor method self.value = value def __str__(self): # Convert error object to a string return repr(self.value)# Submit the MQL query q and return the result as a Python object.# If authentication credentials are supplied, use them in a cookie.# Raises MQLError if the query was invalid. Raises urllib2.HTTPError if# mqlread returns an HTTP status code other than 200 (which should not happen).def read(q, credentials=credentials, escape=escape): # Put the query in an envelope envelope = {'query':q} # Add escape if needed if escape != 'html': envelope['escape'] = False if not escape else escape # Encode the result encoded = urllib.urlencode({'query': simplejson.dumps(envelope)}) # Build the URL and create a Request object for it url = 'http://%s%s' % (host, readservice) req = urllib2.Request(url) # The body of the POST request is encoded URL parameters req.add_header('Content-type', 'application/x-www-form-urlencoded') # Send our authentication credentials, if any, as a cookie. # The need for mqlread authentication is a temporary restriction. if credentials: req.add_header('Cookie', credentials) # Use the encoded envelope as the value of the q parameter in the body # of the request. Specifying a body automatically makes this a POST. req.add_data(encoded) # Now upen the URL and and parse its JSON content f = urllib2.urlopen(req) # Open...
Created by:
sgmils
Sep 7, 2007
Last edited by:
nitromaster101
Jun 24, 2009
Recent Discussions about None
There is no discussion about this document.
Start the Discussion »