This is a Python example for basic HTTP authentication on local installations but not compatible with the api.sketchengine.co.uk server.

Note: on the 8th line corp must be in the format ‘preloaded/’ or ‘/user//’, e.g. the BNC 2 corpus has the format ‘preloaded/bnc2’

#!/usr/bin/env python
import urllib, urllib2, cookielib
import simplejson
import Cookie

username = ''
password = ''
corp = 'bnc2'

root_url = 'https://beta.sketchengine.co.uk'

base_url = '%s/bonito/run.cgi/' % root_url
method = 'view'

# creating query string
attrs = dict(corpname=corp, q='', pagesize='1', format='json')
# query_list can be read from a file, ...
query_list = ['[lemma="test"]',
              '[lemma="drug"][lemma="test"]',
              '[lemma="blood"][lemma="test"]',
              '[lemma="test"][lemma="result"]'
             ]

# authentication
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({ 'username' : username,
                                'password' : password,
                                'submit' : 'ok',
                              })
data = opener.open('%s/login/' % root_url)
data = opener.open('%s/login/' % root_url, login_data)

for query in query_list:
    attrs['q'] = ['q' + query, 'r100']
    encoded_attrs = urllib.quote(simplejson.JSONEncoder().encode(attrs))
    url = base_url + method + '?json=%s' % encoded_attrs
    # json data receiving
    file = opener.open(url)
    data = file.read()
    file.close()

    # now, in the 'data' variable, there is a json string that can be parsed
    # for json syntax (e.g. by simplejson)
    json_obj = simplejson.loads(data)

    print query + '\t' + str(json_obj.get('concsize', '0'))