Search code examples
pythonapiappendklout

Python: Appending API Calls to Spreadsheet


I'll start out by saying I'm very new to Python and programming in general but am very hands on in my learning style.

I would like to use Python to:

  1. Gather an entire column of a spreadsheet into a list
  2. Call to Klout's API to (a) Get the Klout User ID and (b) Get the Klout Score
  3. Append those two variables in columns in the same spreadsheet

I have an API key, the spreadsheet data, Python, and the klout Python scripts

Thanks for your help!

UPDATE

Thanks to Lonely for his help with getting me this far. Now I just need to write my score results to a spreadsheet.

from xlrd import open_workbook
from klout import *
k=Klout('my_API_key')
book=open_workbook('path_to_file')
sheet0=book.sheet_by_index(0)
List1=sheet0.col_values(0)
for screen_name in List1:
    kloutId = k.identity.klout(screenName=screen_name).get('id')
    score = k.user.score(kloutId=kloutId).get('score')
    print screen_name, score

UPDATE 2

Have successfully put Twitter Screennames back into a new spreadsheet. Can't seem to get scores to display correctly. It's also stopping at 30 (which happens to be the request per second limit for Klout). Here's what I have right now.

from xlrd import open_workbook
import xlwt
from klout import *
k=Klout('My_API_Key')
book=open_workbook('Path_to_My_File')
sheet0=book.sheet_by_index(0)
List1=sheet0.col_values(0)
for screen_name in List1:
    kloutId = k.identity.klout(screenName=screen_name).get('id')
    score = k.user.score(kloutId=kloutId).get('score')
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')
i = -1
for n in List1:
    i = i+1
    sheet.write(i,0,n)
b = 0
score1 = int(score)
for x in xrange(score1):
    b = b+1
    sheet.write(b,1,x)

wbk.save("KScores.xls")

FINAL WORKING VERSION

With a ton of help from a personal contact who I credit most of the writing of this script too I now have a completed .py script.

from xlrd import open_workbook
import xlwt
from time import sleep
from klout import *

klout_con = Klout('API_KEY_HERE', secure=True)
book = open_workbook('PATH_TO_YOUR_FILE_HERE')
sheet0 = book.sheet_by_index(0)
List1 = sheet0.col_values(0)

wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')

row = 0
for screen_name in List1:
    klout_id = klout_con.identity.klout(screenName=screen_name).get('id')
    score = klout_con.user.score(kloutId=klout_id).get('score')
    sheet.write(row, 0, screen_name)
    sheet.write(row, 1, score)
    row += 1
    print screen_name
    sleep(1)
    wbk.save('KScores.xls') 

Thanks to the community and Adam who both helped me put this thing together. Was an excellent starter project.


Solution

  • To read excel... use XLRD

    Take all id's in list format.

    Read each one of them by using iteration like for i in list with Klout_APi like ...

    score = k.user.score(kloutId=kloutId).get('score')
    

    However a sample data would have been great...