-
-
Save david-torres/1325168 to your computer and use it in GitHub Desktop.
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) |
Thanks, fixed. I had to look up the nuance regarding what it means to do it this way and why I should.
removed unneeded urlencode, pass params directly to requests
import requests
import json
from urllib import urlencodeclass 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 indentdef __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
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)
:)