Created
November 4, 2011 18:09
-
-
Save vgoklani/1340044 to your computer and use it in GitHub Desktop.
quick & dirty way to call the Klout web service from Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import httplib2, json | |
__links__ = (r'http://developer.klout.com/iodocs') | |
KLOUT_API_KEY = '' # required! | |
''' | |
''' | |
class Klout(): | |
commands = { | |
'score' : r'klout', | |
'showUser' : r'users/show', | |
'getTopics' : r'users/topics', | |
'getInfluenced' : r'soi/influenced_by', | |
'getInfluencer' : r'soi/influencer_of' | |
} | |
def __init__(self, authenticated=True, format='JSON'): | |
if(authenticated == True): | |
self.api_key = KLOUT_API_KEY | |
self.authenticated = True | |
else: | |
self.authenticated = False | |
if(format == 'XML'): | |
self.format = '.xml' | |
else: | |
self.format = '.json' | |
self.http = http = httplib2.Http() | |
def buildURL(self, command, *users): | |
baseURL = r'http://' | |
baseURL += r'api.klout.com/1/' + Klout.commands[command] + self.format | |
baseURL += r'?users=' | |
for user in users: | |
baseURL += user + ',' | |
if(self.authenticated): | |
baseURL += r'&key=' + self.api_key | |
return baseURL | |
def callURL(self, url): | |
response, content = self.http.request(url) | |
return json.loads(content) | |
@staticmethod | |
def test(*usernames): | |
k = Klout() | |
for command in Klout.commands: | |
url = k.buildURL(command, *usernames) | |
resp = k.callURL(url) | |
for user in resp['users']: | |
print command + '\t' + str(url) | |
print json.dumps(user, indent=1) | |
print '\n', | |
@staticmethod | |
def main(): | |
Klout.test('billgates', 'BARACKOBAMA') | |
if __name__ == '__main__': | |
Klout.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment