diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..5bbe43d --- /dev/null +++ b/MANIFEST @@ -0,0 +1,4 @@ +# file GENERATED by distutils, do NOT edit +README +alchemyapi.py +setup.py diff --git a/README b/README new file mode 100644 index 0000000..2153593 --- /dev/null +++ b/README @@ -0,0 +1,38 @@ +# alchemyapi_python # + +A sdk for AlchemyAPI using Python + + +## AlchemyAPI ## + +AlchemyAPI offers artificial intelligence as a service. We teach computers to learn how to read and see, and apply our technology to text analysis and image recognition through a cloud-based API. Our customers use AlchemyAPI to transform their unstructured content such as blog posts, news articles, social media posts and images into much more useful structured data. + +AlchemyAPI is a tech startup located in downtown Denver, Colorado. As the world’s most popular text analysis service, AlchemyAPI serves over 3.5 billion monthly API requests to over 35,000 developers. To enable our services, we use artificial intelligence, machine learning, neural networks, natural language processing and massive-scale web crawling. Our technology powers use cases in a variety of industry verticals, including social media monitoring, business intelligence, content recommendations, financial trading and targeted advertising. + +More information at: http://www.alchemyapi.com + + + +## API Key ## + +To use AlchemyAPI, you'll need to obtain an API key and attach that key to all requests. If you do not already have a key, please visit: http://www.alchemyapi.com/api/register.html + + + +## Getting Started with the Python SDK ## + +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. + +It is also possible to pass the API key into the constructor: + + from alchemyapi import AlchemyAPI + client = AlchemyAPI(YOUR_API_KEY) + response = client.entities('text', demo_text, { 'sentiment':1 }) \ No newline at end of file diff --git a/README.md b/README.md index 84254e2..63bf3da 100644 --- a/README.md +++ b/README.md @@ -37,5 +37,8 @@ To get started and run the example, simply: Just replace YOUR_API_KEY with your 40 character API key from AlchemyAPI, and you should be good to go. - - +It is also possible to pass the API key into the constructor: + + from alchemyapi import AlchemyAPI + client = AlchemyAPI(YOUR_API_KEY) + response = client.entities('text', demo_text, { 'sentiment':1 }) diff --git a/alchemyapi.py b/alchemyapi.py index 34ded5b..a8a9878 100644 --- a/alchemyapi.py +++ b/alchemyapi.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright 2013 AlchemyAPI +# Copyright 2013 AlchemyAPI # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,22 +17,13 @@ from __future__ import print_function import requests +import sys try: - from urllib.request import urlopen - from urllib.parse import urlparse from urllib.parse import urlencode except ImportError: - from urlparse import urlparse - from urllib2 import urlopen from urllib import urlencode -try: - import json -except ImportError: - # Older versions of Python (i.e. 2.4) require simplejson instead of json - import simplejson as json - if __name__ == '__main__': """ @@ -47,7 +38,6 @@ none """ - import sys if len(sys.argv) == 2 and sys.argv[1]: if len(sys.argv[1]) == 40: # write the key to the file @@ -62,6 +52,10 @@ 'The key appears to invalid. Please make sure to use the 40 character key assigned by AlchemyAPI') +class APIKeyException(Exception): + pass + + class AlchemyAPI: # Setup the endpoints ENDPOINTS = {} @@ -125,7 +119,7 @@ class AlchemyAPI: ENDPOINTS['imagetagging']['image'] = '/image/ImageGetRankedImageKeywords' ENDPOINTS['facetagging'] = {} ENDPOINTS['facetagging']['url'] = '/url/URLGetRankedImageFaceTags' - ENDPOINTS['facetagging']['image'] = '/image/ImageGetRankedImageFaceTags' + ENDPOINTS['facetagging']['image'] = '/image/ImageGetRankedImageFaceTags' ENDPOINTS['taxonomy'] = {} ENDPOINTS['taxonomy']['url'] = '/url/URLGetRankedTaxonomy' ENDPOINTS['taxonomy']['html'] = '/html/HTMLGetRankedTaxonomy' @@ -136,53 +130,36 @@ class AlchemyAPI: s = requests.Session() - def __init__(self): - """ + def __init__(self, apikey=None): + """ Initializes the SDK so it can send requests to AlchemyAPI for analysis. It loads the API key from api_key.txt and configures the endpoints. """ - import sys - try: - # Open the key file and read the key - f = open("api_key.txt", "r") - key = f.read().strip() - - 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') - 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.') - 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') - print( - 'If you do not have an API Key from AlchemyAPI, please register for one at: http://www.alchemyapi.com/api/register.html') - - # create a blank key file - open('api_key.txt', 'a').close() - sys.exit(0) - except Exception as e: - print(e) + if not apikey: + try: + # Open the key file and read the key + with open("api_key.txt", "r"): + apikey = f.read().strip() + except IOError: + # The file doesn't exist, so show the message and create the file. + raise APIKeyException( + 'The api_key.txt file could not be read. To create one, ' + 'please run: python alchemyapi.py YOUR_KEY_HERE') + if not apikey: + raise APIKeyException( + 'No apikey key found. If you want to use the api_key.txt file ' + 'to store the key, please run: python alchemyapi.py YOUR_KEY_HERE') + elif len(apikey) != 40: + raise APIKeyException( + 'It appears that the api key is invalid. It should be exactly ' + '40 characters in length') + self.apikey = apikey def entities(self, flavor, data, options={}): """ Extracts the entities for text, a URL or HTML. - For an overview, please refer to: http://www.alchemyapi.com/products/features/entity-extraction/ + For an overview, please refer to: http://www.alchemyapi.com/products/features/entity-extraction/ For the docs, please refer to: http://www.alchemyapi.com/api/entity-extraction/ INPUT: @@ -192,15 +169,15 @@ def entities(self, flavor, data, options={}): Available Options: disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default) - linkedData -> include linked data on disambiguated entities. 0: disabled, 1: enabled (default) + linkedData -> include linked data on disambiguated entities. 0: disabled, 1: enabled (default) coreference -> resolve coreferences (i.e. the pronouns that correspond to named entities). 0: disabled, 1: enabled (default) quotations -> extract quotations by entities. 0: disabled (default), 1: enabled. sentiment -> analyze sentiment for each entity. 0: disabled (default), 1: enabled. Requires 1 additional API transction if enabled. - showSourceText -> 0: disabled (default), 1: enabled + showSourceText -> 0: disabled (default), 1: enabled maxRetrieve -> the maximum number of entities to retrieve (default: 50) OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -229,7 +206,7 @@ def keywords(self, flavor, data, options={}): maxRetrieve -> the max number of keywords returned (default: 50) OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -244,7 +221,7 @@ def concepts(self, flavor, data, options={}): """ Tags the concepts for text, a URL or HTML. For an overview, please refer to: http://www.alchemyapi.com/products/features/concept-tagging/ - For the docs, please refer to: http://www.alchemyapi.com/api/concept-tagging/ + For the docs, please refer to: http://www.alchemyapi.com/api/concept-tagging/ Available Options: maxRetrieve -> the maximum number of concepts to retrieve (default: 8) @@ -252,7 +229,7 @@ def concepts(self, flavor, data, options={}): showSourceText -> 0:disabled (default), 1: enabled OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -278,7 +255,7 @@ def sentiment(self, flavor, data, options={}): showSourceText -> 0: disabled (default), 1: enabled OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -302,10 +279,10 @@ def sentiment_targeted(self, flavor, data, target, options={}): options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. Available Options: - showSourceText -> 0: disabled, 1: enabled + showSourceText -> 0: disabled, 1: enabled OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure the target is valid @@ -337,7 +314,7 @@ def text(self, flavor, data, options={}): extractLinks -> include links, 0: disabled (default), 1: enabled. OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -351,7 +328,7 @@ def text(self, flavor, data, options={}): def text_raw(self, flavor, data, options={}): """ Extracts the raw text (includes ads, navigation, etc.) for a URL or HTML. - For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/ + For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/ For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/ INPUT: @@ -363,7 +340,7 @@ def text_raw(self, flavor, data, options={}): none OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -389,7 +366,7 @@ def author(self, flavor, data, options={}): none OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -403,7 +380,7 @@ def author(self, flavor, data, options={}): def language(self, flavor, data, options={}): """ Detects the language for text, a URL or HTML. - For an overview, please refer to: http://www.alchemyapi.com/api/language-detection/ + For an overview, please refer to: http://www.alchemyapi.com/api/language-detection/ For the docs, please refer to: http://www.alchemyapi.com/products/features/language-detection/ INPUT: @@ -415,7 +392,7 @@ def language(self, flavor, data, options={}): none OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -429,7 +406,7 @@ def language(self, flavor, data, options={}): def title(self, flavor, data, options={}): """ Extracts the title for a URL or HTML. - For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/ + For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/ For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/ INPUT: @@ -438,10 +415,10 @@ def title(self, flavor, data, options={}): options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. Available Options: - useMetadata -> utilize title info embedded in meta data, 0: disabled, 1: enabled (default) + useMetadata -> utilize title info embedded in meta data, 0: disabled, 1: enabled (default) OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -455,7 +432,7 @@ def title(self, flavor, data, options={}): def relations(self, flavor, data, options={}): """ Extracts the relations for text, a URL or HTML. - For an overview, please refer to: http://www.alchemyapi.com/products/features/relation-extraction/ + For an overview, please refer to: http://www.alchemyapi.com/products/features/relation-extraction/ For the docs, please refer to: http://www.alchemyapi.com/api/relation-extraction/ INPUT: @@ -471,12 +448,12 @@ def relations(self, flavor, data, options={}): sentimentExcludeEntities -> exclude full entity name in sentiment analysis. 0: disabled, 1: enabled (default) disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default) linkedData -> include linked data with disambiguated entities. 0: disabled, 1: enabled (default). - coreference -> resolve entity coreferences. 0: disabled, 1: enabled (default) + coreference -> resolve entity coreferences. 0: disabled, 1: enabled (default) showSourceText -> 0: disabled (default), 1: enabled. maxRetrieve -> the maximum number of relations to extract (default: 50, max: 100) OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -502,7 +479,7 @@ def category(self, flavor, data, options={}): showSourceText -> 0: disabled (default), 1: enabled OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -517,7 +494,7 @@ def category(self, flavor, data, options={}): def feeds(self, flavor, data, options={}): """ Detects the RSS/ATOM feeds for a URL or HTML. - For an overview, please refer to: http://www.alchemyapi.com/products/features/feed-detection/ + For an overview, please refer to: http://www.alchemyapi.com/products/features/feed-detection/ For the docs, please refer to: http://www.alchemyapi.com/api/feed-detection/ INPUT: @@ -529,7 +506,7 @@ def feeds(self, flavor, data, options={}): none OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -555,7 +532,7 @@ def microformats(self, flavor, data, options={}): none OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Make sure this request supports this flavor @@ -573,15 +550,15 @@ def imageExtraction(self, flavor, data, options={}): INPUT: flavor -> which version of the call (url only currently). data -> URL to analyze - options -> various parameters that can be used to adjust how the API works, + options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. Available Options: - extractMode -> + extractMode -> trust-metadata : (less CPU intensive, less accurate) always-infer : (more CPU intensive, more accurate) OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ if flavor not in AlchemyAPI.ENDPOINTS['image']: return {'status': 'ERROR', 'statusInfo': 'image extraction for ' + flavor + ' not available'} @@ -599,26 +576,26 @@ def taxonomy(self, flavor, data, options={}): Available Options: - showSourceText -> + showSourceText -> include the original 'source text' the taxonomy categories were extracted from within the API response Possible values: 1 - enabled - 0 - disabled (default) + 0 - disabled (default) sourceText -> where to obtain the text that will be processed by this API call. AlchemyAPI supports multiple modes of text extraction: - web page cleaning (removes ads, navigation links, etc.), raw text extraction - (processes all web page text, including ads / nav links), visual constraint queries, and XPath queries. + web page cleaning (removes ads, navigation links, etc.), raw text extraction + (processes all web page text, including ads / nav links), visual constraint queries, and XPath queries. Possible values: cleaned_or_raw : cleaning enabled, fallback to raw when cleaning produces no text (default) cleaned : operate on 'cleaned' web page text (web page cleaning enabled) raw : operate on raw web page text (web page cleaning disabled) - cquery : operate on the results of a visual constraints query + cquery : operate on the results of a visual constraints query Note: The 'cquery' http argument must also be set to a valid visual constraints query. - xpath : operate on the results of an XPath query + xpath : operate on the results of an XPath query Note: The 'xpath' http argument must also be set to a valid XPath query. cquery -> @@ -631,7 +608,7 @@ def taxonomy(self, flavor, data, options={}): rel-tag output base http url (must be uri-argument encoded) OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ if flavor not in AlchemyAPI.ENDPOINTS['taxonomy']: @@ -649,11 +626,11 @@ def combined(self, flavor, data, options={}): options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. Available Options: - extract -> + extract -> Possible values: page-image, entity, keyword, title, author, taxonomy, concept default : entity, keyword, taxonomy, concept - disambiguate -> + disambiguate -> disambiguate detected entities Possible values: 1 : enabled (default) @@ -671,7 +648,7 @@ def combined(self, flavor, data, options={}): 1 : enabled (default) 0 : disabled - quotations -> + quotations -> enable quotations extraction Possible values: 1 : enabled @@ -683,7 +660,7 @@ def combined(self, flavor, data, options={}): 1 : enabled 0 : disabled (default) - showSourceText -> + showSourceText -> include the original 'source text' the entities were extracted from within the API response Possible values: 1 : enabled @@ -693,12 +670,12 @@ def combined(self, flavor, data, options={}): maximum number of named entities to extract default : 50 - baseUrl -> + baseUrl -> rel-tag output base http url OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ if flavor not in AlchemyAPI.ENDPOINTS['combined']: return {'status': 'ERROR', 'statusInfo': 'combined for ' + flavor + ' not available'} @@ -743,14 +720,14 @@ def faceTagging(self, flavor, data, options={}): def __analyze(self, endpoint, params, post_data=bytearray()): """ - HTTP Request wrapper that is called by the endpoint functions. This function is not intended to be called through an external interface. - It makes the call, then converts the returned JSON string into a Python object. + HTTP Request wrapper that is called by the endpoint functions. This function is not intended to be called through an external interface. + It makes the call, then converts the returned JSON string into a Python object. INPUT: url -> the full URI encoded url OUTPUT: - The response, already converted from JSON to a Python object. + The response, already converted from JSON to a Python object. """ # Add the API Key and set the output mode to JSON diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..df3ce2a --- /dev/null +++ b/setup.py @@ -0,0 +1,9 @@ +from distutils.core import setup + +setup(name='AlchemyAPI', + version='1.1', + description='AlchemyAPI Python bindings', + author='AlchemyAPI', + url='https://github.com/AlchemyAPI/alchemyapi_python', + author_email='support@alchemyapi.com', + py_modules=['alchemyapi'])