Skip to content

Instantly share code, notes, and snippets.

@david-torres
Created October 29, 2011 22:22
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Simple Python interface for Clicky web analytics API
import requests
import json
class ClickyApi(object):
"""
A simple Python interface for the Clicky web analytics api.
Relies on the Requests library - python-requests.org
Usage:
YOUR_CLICKY_SITE_ID = '12345'
YOUR_CLICKY_SITE_KEY = 'qwerty'
clicky = ClickyApi(YOUR_CLICKY_SITE_ID, YOUR_CLICKY_SITE_KEY)
clicky.stats({
'type': 'pages',
'date': 'today'
})
"""
api_endpoint = 'http://api.getclicky.com/api/stats/4'
output_format = 'json'
site_id = ''
site_key = ''
def __init__(self, site_id, site_key, app=''):
self.site_id = site_id
self.site_key = site_key
self.app = app
def stats(self, params={}):
params.update({
'site_id': self.site_id,
'sitekey': self.site_key,
'output': self.output_format,
'app': self.app
})
r = requests.get(self.api_endpoint, params=params)
return json.loads(r.content)
@kennethreitz
Copy link

class ClickyApi(object):

:)

@david-torres
Copy link
Author

Thanks, fixed. I had to look up the nuance regarding what it means to do it this way and why I should.

@david-torres
Copy link
Author

removed unneeded urlencode, pass params directly to requests

@sanyog92
Copy link

import requests
import json
from urllib import urlencode

class ClickyApi:
...
... """
... A simple Python interface for the Clicky web analytics api.
... Relies on the Requests library - python-requests.org
...
... Usage:
... YOUR_CLICKY_SITE_ID = '101028657'
... YOUR_CLICKY_SITE_KEY = '7588ac343c560814'
...
... clicky = ClickyApi(YOUR_CLICKY_SITE_ID, YOUR_CLICKY_SITE_KEY)
...
... clicky.stats({
... 'type': 'pages',
... 'date': 'today'
... })
...
...
... """
... api_endpoint = 'http://api.getclicky.com/api/stats/4'
File "", line 20
api_endpoint = 'http://api.getclicky.com/api/stats/4'
^
IndentationError: unindent does not match any outer indentation level
output_format = 'json'
File "", line 1
output_format = 'json'
^
IndentationError: unexpected indent
site_id = '101028657'
File "", line 1
site_id = '101028657'
^
IndentationError: unexpected indent
site_key = '7588ac343c560814'
File "", line 1
site_key = '7588ac343c560814'
^
IndentationError: unexpected indent

def __init__(self, site_id, site_key, app=''):

File "", line 1
def init(self, site_id, site_key, app=''):
^
IndentationError: unexpected indent

    self.site_id =  101028657

File "", line 1
self.site_id = 101028657
^
IndentationError: unexpected indent

    self.site_key =  7588ac343c560814

File "", line 1
self.site_key = 7588ac343c560814
^
IndentationError: unexpected indent

    self.app = app

File "", line 1
self.app = app
^
IndentationError: unexpected indent

def stats(self, params={}):

File "", line 1
def stats(self, params={}):
^
IndentationError: unexpected indent

    params.update({

File "", line 1
params.update({
^
IndentationError: unexpected indent

        'site_id': self.site_id,

File "", line 1
'site_id': self.site_id,
^
IndentationError: unexpected indent

        'sitekey': self.site_key,

File "", line 1
'sitekey': self.site_key,
^
IndentationError: unexpected indent

        'output': self.output_format,

File "", line 1
'output': self.output_format,
^
IndentationError: unexpected indent

        'app': self.app

File "", line 1
'app': self.app
^
IndentationError: unexpected indent

    })

File "", line 1
})
^
IndentationError: unexpected indent

    r = requests.get(self.api_endpoint + '?' + urlencode(params))

File "", line 1
r = requests.get(self.api_endpoint + '?' + urlencode(params))
^
IndentationError: unexpected indent

    return json.loads(r.content)

File "", line 1
return json.loads(r.content)
^
IndentationError: unexpected indent

@tdiem
Copy link

tdiem commented Dec 28, 2020

FYI all, this still works beautifully. I've updated the original post to bring some clarity on a few items and make it easier to run out of box. All you need to do is install requests in the virtual environment that you are using and update the INSERT_YOUR_SITE_ID_HERE and INSERT_YOUR_CLICKY_SITE_KEY_HERE. Thanks David for the original post! The formatting is extremely wonky but

import json
import requests ## This needs to be pip installed

class ClickyApi(object):
api_endpoint = 'http://api.getclicky.com/api/stats/4'
output_format = 'json'
site_id = ''
site_key = ''

def __init__(self, site_id, site_key, app=''):
    self.site_id = site_id
    self.site_key = site_key
    self.app = app

def stats(self, params={}):
    params.update({
        'site_id': self.site_id,
        'sitekey': self.site_key,
        'output': self.output_format,
        'app': self.app
    })
    r = requests.get(self.api_endpoint, params=params)
    return json.loads(r.content)

YOUR_CLICKY_SITE_ID = INSERT_YOUR_SITE_ID_HERE # This is available on the Prefs Tab in Clicky
YOUR_CLICKY_SITE_KEY = INSERT_YOUR_CLICKY_SITE_KEY_HERE # This is available on the Prefs Tab in Clicky
clicky = ClickyApi(YOUR_CLICKY_SITE_ID, YOUR_CLICKY_SITE_KEY)
data = clicky.stats({
'type': 'pages',
'date': 'today'
})

print(data)

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