Sunday Feb 5
Jul
18/09
WordZe API Python Script
Last Updated on Wednesday, 14 April 2010 09:56
Written by Cody Snider
Saturday, 18 July 2009 04:04

Can be run from the command line or in another application. Built for Python 2.4 with pyCURL

View Code PYTHON
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)

2 Comments
  1. CommentsCole   |  Thursday, 18 March 2010 at 9:03 am

    This is really useful, thanks Cody – Reading through other posts I see you encounter alot of the same issues as me!

    As an aside, how are you getting on with Wordze? I’ve been checking out a few services and this seems the best for API work. Is it good value?

  2. CommentsCody   |  Thursday, 18 March 2010 at 9:59 pm

    Thanks, Cole. I really appreciate the kind words.

    As of late, I’ve fallen back to the AdWords suggest tool. The real issue I’ve found with AdWords in the past is the lack of absolute traffic numbers, but the benefit of having a complete picture is priceless for niche terms. When making a term selection, it seems having a complete but relative count for all terms is better than having gaps in the data.

    Of all the services of this nature I’ve encountered in the past, WordZe seems the most useful. I have to admit it’s been some time since I’ve explored them (and may be a viable solution to a bigger problem with automation I’ve been dealing with lately). Have you had much luck with it? What other services have you tried?


Leave a Reply