Skip to content

Instantly share code, notes, and snippets.

@messa
Created May 25, 2012 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save messa/2786466 to your computer and use it in GitHub Desktop.
Save messa/2786466 to your computer and use it in GitHub Desktop.
Sklik API keyword stats example
#!/usr/bin/env python
"""
Command-line tool for creating Statistical report from Report template
using Sklik.cz API.
Sklik.cz API documentation: http://api.sklik.cz/
Contact: http://bit.ly/sklik-contact-form
"""
import codecs
from datetime import datetime
from pprint import pprint
import sys
import xmlrpclib
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
if len(sys.argv) not in (3, 5):
print "Usage: %s <username> <password>" % sys.argv[0]
print
print " List report templates and foreign accounts with read-write access."
print
print "Usage: %s <username> <password> <templateId> <reportUsername>" % sys.argv[0]
print
print "This script will print stats for keywords in <statusername>"
print "account (or in <username> account if <statusername> not given)."
exit(1)
username = sys.argv[1]
password = sys.argv[2]
if len(sys.argv) > 3:
templateId = int(sys.argv[3])
reportUsername = sys.argv[4]
else:
templateId = None
reportUsername = None
proxy = xmlrpclib.ServerProxy("https://api.sklik.cz/RPC2", allow_none=True)
# log in
print "Logging in..."
res = proxy.client.login(username, password)
if res["status"] != 200:
print "ERROR - Failed to login: %s" % res
exit(1)
session = res["session"]
res = proxy.client.getAttributes(session)
foreignAccounts = res["foreignAccounts"]
# if no templateId and reportUsername was given, print templates and possible
# foreign accounts
if not templateId:
print "Possible templates (<templateId> '<name>'):"
res = proxy.listReportTemplates(session)
for reportTemplate in res["reportTemplates"]:
print " %s '%s'" % (
reportTemplate["id"], reportTemplate["name"])
for foreignAccount in foreignAccounts:
res = proxy.listReportTemplates(session, foreignAccount["userId"])
for reportTemplate in res["reportTemplates"]:
print " %s '%s' (from user account %s)" % (
reportTemplate["id"], reportTemplate["name"], foreignAccount["username"])
print
print "Possible foreign accounts:"
for foreignAccount in foreignAccounts:
if foreignAccount["access"] == "rw":
print " %s" % (foreignAccount["username"])
exit(0)
# get userId of the account in which the report will be generated
if reportUsername == username:
reportUserId = None
else:
reportUserId = None
for foreignAccount in foreignAccounts:
if foreignAccount["username"] == reportUsername:
reportUserId = foreignAccount["userId"]
if not reportUserId:
raise Exception("Username %s not found in foreign accounts of %s"
% (reportUsername, username))
# create report
print "Creating report from template %s in user account %s..." % (templateId, reportUsername)
res = proxy.report.createFromTemplate(session, templateId, reportUserId)
reportId = res["reportId"]
print "Report id is %s." % reportId
#!/usr/bin/env python
"""
Command-line tool for retrieving Statistical report from Sklik.cz API.
Report must be previously created on the web or from report template using
Sklik.cz API.
Sklik.cz API documentation: http://api.sklik.cz/
Contact: http://bit.ly/sklik-contact-form
"""
import codecs
from datetime import datetime
from pprint import pprint
import sys
import xmlrpclib
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
if len(sys.argv) != 4:
print "Usage: %s <username> <password> <reportId>" % sys.argv[0]
exit(1)
username = sys.argv[1]
password = sys.argv[2]
reportId = int(sys.argv[3])
proxy = xmlrpclib.ServerProxy("https://api.sklik.cz/RPC2", allow_none=True)
# log in
print "Logging in..."
res = proxy.client.login(username, password)
if res["status"] != 200:
print "ERROR - Failed to login: %s" % res
exit(1)
session = res["session"]
print "Retrieving report..."
res = proxy.report.getAttributes(session, reportId)
if res["status"] == 200 and res.get("report"):
pprint(res["report"])
else:
pprint(res)
exit(1)
#!/usr/bin/env python
"""
Command-line tool for retrieving keyword stats from Sklik.cz API.
This is an alternative to generating and retrieving Statistical report.
Sklik.cz API documentation: http://api.sklik.cz/
Contact: http://bit.ly/sklik-contact-form
"""
import codecs
from datetime import datetime
import sys
import xmlrpclib
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
if len(sys.argv) not in (3, 4):
print "Usage: %s <username> <password> [<statUsername>]" % sys.argv[0]
print
print "This script will print stats for keywords in <statUsername>"
print "account (or in <username> account if <statUsername> not given)."
exit(1)
username = sys.argv[1]
password = sys.argv[2]
if len(sys.argv) > 3:
statUsername = sys.argv[3]
else:
statUsername = username
dateFrom = datetime.now()
dateTo = datetime.now() # stats for today
proxy = xmlrpclib.ServerProxy("https://api.sklik.cz/RPC2", allow_none=True)
# log in
print "Logging in..."
res = proxy.client.login(username, password)
if res["status"] != 200:
print "ERROR - Failed to login: %s" % res
exit(1)
session = res["session"]
# get userId of the account for which the stats will be retrieved
if statUsername == username:
statUserId = None
else:
statUserId = None
res = proxy.client.getAttributes(session)
for foreignAccount in res["foreignAccounts"]:
if foreignAccount["username"] == statUsername:
statUserId = foreignAccount["userId"]
if not statUserId:
raise Exception("Username %s not found in foreign accounts of %s"
% (statUsername, username))
keywordIds = list()
# populate the keywordIds list
print "Listing campaigns..."
res = proxy.listCampaigns(session, statUserId)
for campaign in res["campaigns"]:
if campaign["removed"]:
continue
print "Listing groups in campaign %s '%s'" % (campaign["id"], campaign["name"])
res = proxy.listGroups(session, campaign["id"])
for group in res["groups"]:
if group["removed"]:
continue
print "Listing keywords in group %s '%s'" % (group["id"], group["name"])
res = proxy.listKeywords(session, group["id"])
for keyword in res["keywords"]:
keywordIds.append(keyword["id"])
print "Retrieved %s keyword ids." % len(keywordIds)
# get stats for all keywords in keywordIds
step = 100
for start in range(0, len(keywordIds), step):
stop = min(start+step, len(keywordIds))
print "Retrieving stats for keywords %s-%s/%s..." % (start+1, stop, len(keywordIds))
res = proxy.keywords.stats(session, keywordIds[start:stop], dateFrom, dateTo)
for keywordStats in res["keywordStats"]:
print "keywordId: %10s impressions: %7s clicks: %6s " % (
keywordStats["keywordId"],
keywordStats["stats"]["impressions"],
keywordStats["stats"]["clicks"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment