Created
September 3, 2013 19:58
-
-
Save deniszgonjanin/6428777 to your computer and use it in GitHub Desktop.
Thin CanLII API wrapper for python (http://developer.canlii.org/docs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib, urllib2, json | |
class CanLIIException(Exception): | |
def __str__(self): | |
return repr(self.args) | |
class CanLII(object): | |
def __init__(self, api_key, language = 'en'): | |
self.address = "http://api.canlii.org/v1/" | |
self.language = language | |
self.api_key = api_key | |
def call(self, action, database = None, case = None, limit = 100, offset = 0): | |
d = {'resultCount': limit, 'offset': offset, 'api_key': self.api_key} | |
params = urllib.urlencode(dict((k,v) for k, v in d.iteritems() if v is not None)) | |
if database is None: | |
path = "%s/%s" % (action, self.language) | |
elif case is None: | |
path = "%s/%s/%s" % (action, self.language, database) | |
else: | |
path = "%s/%s/%s/%s" % (action, self.language, database, case) | |
url = urllib.basejoin(self.address, path) | |
r = urllib2.urlopen(url, params) | |
response = json.loads(r.read()) | |
#canlii api returns code 200 OK even if there were errors. | |
if type(response) is list and'error' in response[0].keys(): | |
raise CanLIIException(response[0]['message']) | |
return response | |
def caseBrowse(self, **kwargs): | |
return self.call('caseBrowse', **kwargs) | |
def legislationBrowse(self, **kwargs): | |
return self.call('legislationBrowse', **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: