WordZe API Python Script
by Cody on Jul.18, 2009, under Python, SEO
Can be run from the command line or in another application. Built for Python 2.4 with pyCURL
import sys, pycurl, re, time, string, urllib
from datetime import date
import xml.dom.minidom
class WordZe:
def __init__(self, key='ADD YOUR KEY HERE'):
self.apiKey = key
self.caretakerObj = WordZeCaretaker()
self.userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; es-ES; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)'
def singleKeywordLookup(self, keyList = []):
if len(keyList) == 0:
raise Error('You need to pass a list to the singleKeywordLookup to perform a request.')
else:
# TODO: DB CHECK FOR VALUE PULLED WITHIN LAST 30 DAYS
keyString = string.join(keyList, ',')
requestObj = pycurl.Curl()
requestObj.setopt(pycurl.USERAGENT, self.userAgent)
requestObj.setopt(pycurl.FOLLOWLOCATION, 1)
requestObj.setopt(pycurl.WRITEFUNCTION, self.caretakerObj.remotecallback)
requestObj.setopt(pycurl.HTTPGET, 1)
keyString = str(keyString).replace(' ','+')
requestObj.setopt(pycurl.URL, str('http://api.wordze.com/KeywordSingle?ApiKey=' + str(self.apiKey) + '&Query=' + str(keyString)))
requestObj.perform()
requestObj.close()
return self.caretakerObj.getDict()
class WordZeCaretaker:
def __init__(self):
self.contents = ''
def getDict(self):
docMod = xml.dom.minidom.parseString(self.contents)
return self.handleKeywords(docMod)
def handleKeywords(self, docMod):
returnDict = []
keywords = docMod.getElementsByTagName("Keyword")
for keyword in keywords:
keyDict = {
'keyword':self.getText(keyword.childNodes),
'count':int(keyword.attributes['Count'].value),
'estimated':int(keyword.attributes['Estimated'].value)
}
returnDict.append(keyDict)
# TODO: DB INSERT
return returnDict
def getText(self,nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc
def remotecallback(self, buf):
self.contents = self.contents + buf
if __name__ == '__main__':
lookupObj = WordZe()
keywords = raw_input('Comma sep, no spaces list of terms: ')
termList = str(keywords).split(',')
cleanList = []
for term in termList:
cleanList.append(str(term))
lookupObj.singleKeywordLookup(cleanList)
No comments for this entry yet...