diff --git a/README.md b/README.md index 84254e2..d699a30 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # alchemyapi_python # -A sdk for AlchemyAPI using Python +A sdk for AlchemyAPI using Python. + +Modified to use an enviroment variable ("ALCHEMY_API_KEY") for authentication with YOUR_API_KEY. ## AlchemyAPI ## @@ -31,11 +33,3 @@ To get started and run the example, simply: git clone https://github.com/AlchemyAPI/alchemyapi_python.git cd alchemyapi_python - python alchemyapi.py YOUR_API_KEY - python example.py - - -Just replace YOUR_API_KEY with your 40 character API key from AlchemyAPI, and you should be good to go. - - - diff --git a/alchemyapi.py b/alchemyapi.py index 34ded5b..1a40242 100644 --- a/alchemyapi.py +++ b/alchemyapi.py @@ -17,6 +17,7 @@ from __future__ import print_function import requests +import os try: from urllib.request import urlopen @@ -33,35 +34,6 @@ # Older versions of Python (i.e. 2.4) require simplejson instead of json import simplejson as json - -if __name__ == '__main__': - """ - Writes the API key to api_key.txt file. It will create the file if it doesn't exist. - This function is intended to be called from the Python command line using: python alchemyapi YOUR_API_KEY - If you don't have an API key yet, register for one at: http://www.alchemyapi.com/api/register.html - - INPUT: - argv[1] -> Your API key from AlchemyAPI. Should be 40 hex characters - - OUTPUT: - none - """ - - import sys - if len(sys.argv) == 2 and sys.argv[1]: - if len(sys.argv[1]) == 40: - # write the key to the file - f = open('api_key.txt', 'w') - f.write(sys.argv[1]) - f.close() - print('Key: ' + sys.argv[1] + ' was written to api_key.txt') - print( - 'You are now ready to start using AlchemyAPI. For an example, run: python example.py') - else: - print( - 'The key appears to invalid. Please make sure to use the 40 character key assigned by AlchemyAPI') - - class AlchemyAPI: # Setup the endpoints ENDPOINTS = {} @@ -143,33 +115,30 @@ def __init__(self): """ import sys - try: - # Open the key file and read the key - f = open("api_key.txt", "r") - key = f.read().strip() + try: + # Read the key file from the enviroment + key = os.environ['ALCHEMY_API_KEY'] if key == '': # The key file should't be blank print( - 'The api_key.txt file appears to be blank, please run: python alchemyapi.py YOUR_KEY_HERE') + 'The API key appears to be blank, please run: python alchemyapi.py YOUR_KEY_HERE') print( 'If you do not have an API Key from AlchemyAPI, please register for one at: http://www.alchemyapi.com/api/register.html') sys.exit(0) elif len(key) != 40: # Keys should be exactly 40 characters long print( - 'It appears that the key in api_key.txt is invalid. Please make sure the file only includes the API key, and it is the correct one.') + 'It appears that API key is invalid. Please make sure the file only includes the API key, and it is the correct one.') sys.exit(0) else: # setup the key self.apikey = key - # Close file - f.close() except IOError: # The file doesn't exist, so show the message and create the file. print( - 'API Key not found! Please run: python alchemyapi.py YOUR_KEY_HERE') + 'API Key not found! Please add your API key to the ALCHEMY_API_KEY env variable') print( 'If you do not have an API Key from AlchemyAPI, please register for one at: http://www.alchemyapi.com/api/register.html') diff --git a/alchemyrap.py b/alchemyrap.py new file mode 100644 index 0000000..71cf623 --- /dev/null +++ b/alchemyrap.py @@ -0,0 +1,63 @@ +from retrying import retry +from alchemyapi_python import alchemyapi +alchemy = alchemyapi.AlchemyAPI() + +def full_enrich(flavor, data): + """ + INPUT: + flavor -> which version of the call, i.e. text, url or html. + data -> the data to analyze, either the text, the url or html code. + OUTPUT: dictonary containing alchemy api responce + """ + d = {} + d['document_sentiment'] = document_sentiment(flavor, data) + d['entities_sentiment'] = entities(flavor, data, sentiment=0) + d['concepts'] = concepts(flavor, data) + d['keywords_sentiment'] = keywords(flavor, data, sentiment=0) + d['category'] = category(flavor, data) + d['taxonomy'] = taxonomy(flavor, data) + d['author'] = author(flavor, data) + return(d) + +# retry settings +sman = 2 + +@retry(stop_max_attempt_number=sman) +def entities(flavor, data, sentiment=0): + data = alchemy.entities(flavor=flavor, data=data, options={'sentiment': sentiment}) + return data['entities'] + +@retry(stop_max_attempt_number=sman) +def concepts(flavor, data): + data = alchemy.concepts(flavor, data) + if 'concepts' in data: + return data['concepts'] + +@retry(stop_max_attempt_number=sman) +def keywords(flavor, data, sentiment=0): + data = alchemy.keywords(flavor, data, {'sentiment': sentiment}) + if 'keywords' in data: + return data['keywords'] + +@retry(stop_max_attempt_number=sman) +def category(flavor, data): + data = alchemy.category(flavor, data) + return data + +@retry(stop_max_attempt_number=sman) +def taxonomy(flavor, data): + data = alchemy.taxonomy(flavor, data) + if 'taxonomy' in data: + return data['taxonomy'] + +@retry(stop_max_attempt_number=sman) +def author(flavor, data): + data = alchemy.author(flavor, data) + if 'author' in data: + return data['author'] + +@retry(stop_max_attempt_number=sman) +def document_sentiment(flavor, data): + data = alchemy.sentiment(flavor, data) + if 'docSentiment' in data: + return data['docSentiment']