Skip to content

Commit

Permalink
Changed keyword limit to be a default call thing in the extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
mckk committed Nov 4, 2011
1 parent 5bcbd51 commit 5f5c35d
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions keyword_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@

class KeywordExtractor:

def __init__(self):
def __init__(self, limit=3):
config = ConfigParser.RawConfigParser()
config.read('config.conf')
self.key = config.get('alchemyapi','key')
self.stream = "http://access.alchemyapi.com/calls/url/URLGetRankedKeywords"
self.apikey_option = 'apikey=' + self.key
self.output_option = 'outputMode=' + 'json'
self.keyword_limit_option = 'maxRetrieve=' + '5'
self.keyword_limit_option = 'maxRetrieve={0}'.format(limit)

def getKeywordsByURL(self, url):
# Building Alchemy API call
call = self.stream + '?' + self.apikey_option + "&" + 'url=' + url + "&" + self.output_option + '&' + self.keyword_limit_option
data = urllib.urlopen(call)
keywords = []
try:
keywords = []
#Fetching data from Alchemy API
data = json.loads(data.read())
keyword_data = data['keywords']

#We only care about the keywords
for keyword_entry in keyword_data:
keywords.append(str(keyword_entry['text']))
return keywords
except ValueError:
print "Error in data from the keyword extractor"
print data
return keywords
return []

if __name__ == "__main__":
k = KeywordExtractor()
print k.getKeywordsByURL("http://bit.ly/oTPnyE")
print k.getKeywordsByURL("http://techcrunch.com/2011/10/19/dropbox-minimal-viable-product/")
k = KeywordExtractor()

0 comments on commit 5f5c35d

Please sign in to comment.