Skip to content

Instantly share code, notes, and snippets.

@deniszgonjanin
Created September 3, 2013 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save deniszgonjanin/6428777 to your computer and use it in GitHub Desktop.
Save deniszgonjanin/6428777 to your computer and use it in GitHub Desktop.
Thin CanLII API wrapper for python (http://developer.canlii.org/docs)
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)
@deniszgonjanin
Copy link
Author

Usage:

c = CanLII(your_api_key)
c.caseBrowse()
c.caseBrowse(database = u'nssc', limit=100, offset=2)
c.caseBrowse(database = u'nssc', case="2013nssc267")

c.legislationBrowse()
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment