Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Piwik cvs exporter
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import csv
from optparse import OptionParser
import urllib
import httplib
import urlparse
try:
import simplejson
except ImportError:
print "You must install the python-simplejson to use this script."
print "On Debian/Ubuntu, you can install it with this command:"
print
print "sudo apt-get install python-simplejson"
sys.exit(1)
HEADERS = ['entry_sum_visit_length',
'bounce_rate',
'entry_bounce_count',
'sum_time_spent',
'entry_nb_visits',
'exit_rate',
'label',
'idsubdatatable',
'avg_time_on_page',
'nb_visits',
'exit_nb_visits',
'nb_hits',
'entry_nb_actions',
'nb_uniq_visitors',
'exit_nb_uniq_visitors',
'url',
'entry_nb_uniq_visitors']
class PiwikAPI:
def __init__(self, url, token_auth):
self.url = url
self.token_auth = token_auth
(scheme, netloc, path, query, fragment) = urlparse.urlsplit(self.url)
self.host = netloc
def call(self, method, params = {}, format = 'json'):
args = {'module' : 'API',
'method' : method,
'format' : format,
'token_auth' : self.token_auth}
args.update(params)
conn = httplib.HTTPConnection(self.host)
conn.request('GET', u"%s?%s" % (self.url, urllib.urlencode(args)), headers={'User-Agent' : 'Django Piwik'})
result = conn.getresponse()
data = None
if result.status == 200:
data = result.read()
conn.close()
if data is not None and format == 'json':
return simplejson.loads(data)
return data
def getAllSites(self):
return self.call('SitesManager.getSitesWithAtLeastViewAccess')
def getSiteFromId(self, id):
return self.call('SitesManager.getSiteFromId', params = {'idSite' : id})
def getJavascriptTag(self, id, piwikUrl = '', actionName = ''):
result = self.call('SitesManager.getJavascriptTag', params = {'idSite' : id})
if result:
return result['value']
return None
def main():
parser = OptionParser()
parser.add_option("-u", "--url", dest="url",
help="Piwik url")
parser.add_option("-k", "--key", dest="key",
help="Piwik key")
parser.add_option("-p", "--period", dest="period",
help="Period of the export (year, month, day)")
parser.add_option("-d", "--date", dest="date",
help="The date to export in the format YYYY-MM-DD")
parser.add_option("-o", "--output", dest="filename",
help="write report to FILE", metavar="FILE")
(opt, args) = parser.parse_args()
if not opt.url or not opt.key or not opt.filename:
parser.print_help()
print "incorrect number of arguments"
return sys.exit(1)
if opt.filename == '-':
csv_file = sys.stdout
else:
csv_file = open(opt.filename, 'w')
piwik = PiwikAPI(opt.url, opt.key)
result = piwik.call('Actions.getPageUrls',
params = {'idSite': 1,
'period' : opt.period,
'date': opt.date})
dw = csv.DictWriter(csv_file,
fieldnames=HEADERS)
# Stay compatbile with python 2.6
try:
dw.writeheader()
except AttributeError:
headers = []
for h in HEADERS:
headers.append((h,h))
dw.writerow(dict(headers))
for r in result:
dw.writerow(r)
if __name__ == "__main__":
main()
@avoine

This comment has been minimized.

Copy link
Owner Author

avoine commented Dec 8, 2011

Usage:

This script use a the API Key to dump the statistics for a piwik site in a csv formated file.

python piwik_export.py -u http://piwik.koumbit.net/ -k123456789abcdefghijkl -o my_file.csv -p day -d 2011-01-30

Options:
-h, --help show this help message and exit
-u URL, --url=URL Piwik url
-k KEY, --key=KEY Piwik key
-p PERIOD, --period=PERIOD Period of the export (year, month, day)
-d DATE, --date=DATE The date to export in the format YYYY-MM-DD
-o FILE, --output=FILE write report to FILE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.