From ba8e0f687cb7ee99a738356f941e5eef52e190cd Mon Sep 17 00:00:00 2001 From: Eunice Lee Date: Mon, 22 Jun 2015 11:00:25 -0400 Subject: [PATCH] initialized key on line 64. When placing alchemyapi.js into my project directory, it kept overwriting api_key.txt to empty until I initiatlized the variable key. --- alchemyapi.js | 201 +++++++++++++++++++++++++------------------------- 1 file changed, 100 insertions(+), 101 deletions(-) diff --git a/alchemyapi.js b/alchemyapi.js index b83cd3a..9fb8802 100644 --- a/alchemyapi.js +++ b/alchemyapi.js @@ -24,8 +24,8 @@ exports = module.exports = AlchemyAPI; -/** - * Checks if file is called directly, and then writes the API key to api_key.txt if it's included in the args +/** + * Checks if file is called directly, and then writes the API key to api_key.txt if it's included in the args * * Note: if you don't have an API key, register for one at: http://www.alchemyapi.com/api/register.html * @@ -34,7 +34,7 @@ exports = module.exports = AlchemyAPI; * * OUTPUT: * none -*/ +*/ if (require.main === module) { //file was called directly from command line to set the key if (process.argv[2]) { @@ -61,7 +61,7 @@ function AlchemyAPI() { //Load the key from api_key.txt try { - key = fs.readFileSync(__dirname + '/api_key.txt').toString().trim(); + var key = fs.readFileSync(__dirname + '/api_key.txt').toString().trim(); } catch(err) { //Create the file @@ -70,7 +70,7 @@ function AlchemyAPI() { console.log('If you do not have a key, register for one at: http://www.alchemyapi.com/api/register.html'); process.exit(1); } - + //Make sure the key formating looks good if (key.length != 40) { console.log('The API key in api_key.txt does not appear to be valid. Make sure to run: node alchemyapi.js YOUR_KEY_HERE'); @@ -83,9 +83,9 @@ function AlchemyAPI() { /** - * 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 Javascript 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 Javascript object. + * * INPUT: * url -> the full URI encoded url * params -> the call parameters, both required and optional @@ -93,7 +93,7 @@ function AlchemyAPI() { * callback -> the callback function * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ /* this.analyze = function (endpoint, params, sfile, callback) { @@ -128,8 +128,8 @@ function AlchemyAPI() { /** * HTTP Uploader - * It makes the call, then converts the returned JSON string into a Javascript object. - * + * It makes the call, then converts the returned JSON string into a Javascript object. + * * INPUT: * url -> the full URI encoded url * params -> the call parameters, both required and optional @@ -137,7 +137,7 @@ function AlchemyAPI() { * callback -> the callback function * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ this.analyze = function (endpoint, params, sfile, callback) { var urlKVPairs = []; @@ -202,8 +202,8 @@ function AlchemyAPI() { //Add the static variables -AlchemyAPI.HOST = 'access.alchemyapi.com'; -AlchemyAPI.BASE_URL = '/calls'; +AlchemyAPI.HOST = 'access.alchemyapi.com'; +AlchemyAPI.BASE_URL = '/calls'; //Setup the endpoints AlchemyAPI.ENDPOINTS = {}; @@ -273,30 +273,30 @@ AlchemyAPI.ENDPOINTS['image_keywords']['image'] = '/image/ImageGetRankedImageKey /** * 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: * 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. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * 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 Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.entities = function(flavor, data, options, callback) { options = options || {} - + if (!(flavor in AlchemyAPI.ENDPOINTS['entities'])) { callback({ status:'ERROR', statusInfo:'Entity extraction is not available for ' + flavor }); } else { @@ -311,13 +311,13 @@ AlchemyAPI.prototype.entities = function(flavor, data, options, callback) { * Extracts the keywords from text, a URL or HTML. * For an overview, please refer to: http://www.alchemyapi.com/products/features/keyword-extraction/ * For the docs, please refer to: http://www.alchemyapi.com/api/keyword-extraction/ - * + * * 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. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * keywordExtractMode -> normal (default), strict * sentiment -> analyze sentiment for each keyword. 0: disabled (default), 1: enabled. Requires 1 additional API transaction if enabled. @@ -325,7 +325,7 @@ AlchemyAPI.prototype.entities = function(flavor, data, options, callback) { * maxRetrieve -> the max number of keywords returned (default: 50) * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.keywords = function(flavor, data, options, callback) { options = options || {} @@ -343,13 +343,13 @@ AlchemyAPI.prototype.keywords = function(flavor, data, options, callback) { /** * 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/ + * * 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. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function + * callback -> the callback function for this async function * * Available Options: * maxRetrieve -> the maximum number of concepts to retrieve (default: 8) @@ -357,7 +357,7 @@ AlchemyAPI.prototype.keywords = function(flavor, data, options, callback) { * showSourceText -> 0:disabled (default), 1: enabled * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.concepts = function(flavor, data, options, callback) { options = options || {} @@ -376,22 +376,22 @@ AlchemyAPI.prototype.concepts = function(flavor, data, options, callback) { * Calculates the sentiment for text, a URL or HTML. * For an overview, please refer to: http://www.alchemyapi.com/products/features/sentiment-analysis/ * For the docs, please refer to: http://www.alchemyapi.com/api/sentiment-analysis/ - * + * * 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. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function + * callback -> the callback function for this async function * * Available Options: * showSourceText -> 0: disabled (default), 1: enabled * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.sentiment = function(flavor, data, options, callback) { options = options || {} - + if (!(flavor in AlchemyAPI.ENDPOINTS['sentiment'])) { callback({ status:'ERROR', statusInfo:'Sentiment analysis is not available for ' + flavor }); } else { @@ -406,27 +406,27 @@ AlchemyAPI.prototype.sentiment = function(flavor, data, options, callback) { * Calculates the targeted sentiment for text, a URL or HTML. * For an overview, please refer to: http://www.alchemyapi.com/products/features/sentiment-analysis/ * For the docs, please refer to: http://www.alchemyapi.com/api/sentiment-analysis/ - * + * * 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. * target -> the word or phrase to run sentiment analysis on. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * showSourceText -> 0: disabled, 1: enabled * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.sentiment_targeted = function(flavor, data, target, options, callback) { options = options || {} - + if (!(flavor in AlchemyAPI.ENDPOINTS['sentiment_targeted'])) { callback({ status:'ERROR', statusInfo:'Sentiment analysis is not available for ' + flavor }); } else if (!target) { - callback({ status:'ERROR', statusInfo:'target must not be null' }); + callback({ status:'ERROR', statusInfo:'target must not be null' }); } else { //Add the data to the options and analyze options[flavor] = data; @@ -440,19 +440,19 @@ AlchemyAPI.prototype.sentiment_targeted = function(flavor, data, target, options * Extracts the cleaned text (removes ads, navigation, etc.) for a URL or HTML. * 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: * flavor -> which version of the call, i.e. url or html. * data -> the data to analyze, either the url or html code. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * useMetadata -> utilize meta description data, 0: disabled, 1: enabled (default) * extractLinks -> include links, 0: disabled (default), 1: enabled. * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.text = function(flavor, data, options, callback) { options = options || {} @@ -469,20 +469,20 @@ AlchemyAPI.prototype.text = function(flavor, data, options, callback) { /** * 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: * flavor -> which version of the call, i.e. url or html. * data -> the data to analyze, either the url or html code. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * none * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.text_raw = function(flavor, data, options, callback) { options = options || {} @@ -501,22 +501,22 @@ AlchemyAPI.prototype.text_raw = function(flavor, data, options, callback) { * Extracts the author from a URL or HTML. * For an overview, please refer to: http://www.alchemyapi.com/products/features/author-extraction/ * For the docs, please refer to: http://www.alchemyapi.com/api/author-extraction/ - * + * * INPUT: * flavor -> which version of the call, i.e. url or html. * data -> the data to analyze, either the the url or html code. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function + * callback -> the callback function for this async function * * Availble Options: * none * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.author = function(flavor, data, options, callback) { options = options || {} - + if (!(flavor in AlchemyAPI.ENDPOINTS['author'])) { callback({ status:'ERROR', statusInfo:'Author extraction is not available for ' + flavor }); } else { @@ -529,24 +529,24 @@ AlchemyAPI.prototype.author = function(flavor, data, options, callback) { /** * 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: * 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. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function + * callback -> the callback function for this async function * * Available Options: * none * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.language = function(flavor, data, options, callback) { options = options || {} - + if (!(flavor in AlchemyAPI.ENDPOINTS['language'])) { callback({ status:'ERROR', statusInfo:'Language detection is not available for ' + flavor }); } else { @@ -559,20 +559,20 @@ AlchemyAPI.prototype.language = function(flavor, data, options, callback) { /** * 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: * flavor -> which version of the call, i.e. url or html. * data -> the data to analyze, either the url or html code. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * 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 Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.title = function(flavor, data, options, callback) { options = options || {} @@ -589,15 +589,15 @@ AlchemyAPI.prototype.title = function(flavor, data, options, callback) { /** * 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: * 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. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * sentiment -> 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. * keywords -> extract keywords from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. @@ -606,16 +606,16 @@ AlchemyAPI.prototype.title = function(flavor, data, options, callback) { * 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 Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.relations = function(flavor, data, options, callback) { options = options || {} - + if (!(flavor in AlchemyAPI.ENDPOINTS['relations'])) { callback({ status:'ERROR', statusInfo:'Relation extraction is not available for ' + flavor }); } else { @@ -630,13 +630,13 @@ AlchemyAPI.prototype.relations = function(flavor, data, options, callback) { * Categorizes the text for text, a URL or HTML. * For an overview, please refer to: http://www.alchemyapi.com/products/features/text-categorization/ * For the docs, please refer to: http://www.alchemyapi.com/api/text-categorization/ - * + * * 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. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * showSourceText -> 0: disabled (default), 1: enabled * @@ -658,24 +658,24 @@ AlchemyAPI.prototype.category = function(flavor, data, options, callback) { /** * 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: * flavor -> which version of the call, i.e. url or html. * data -> the data to analyze, either the the url or html code. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function + * callback -> the callback function for this async function * * Available Options: * none * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.feeds = function(flavor, data, options, callback) { options = options || {} - + if (!(flavor in AlchemyAPI.ENDPOINTS['feeds'])) { callback({ status:'ERROR', statusInfo:'Feed detection is not available for ' + flavor }); } else { @@ -690,18 +690,18 @@ AlchemyAPI.prototype.feeds = function(flavor, data, options, callback) { * Parses the microformats for a URL or HTML. * For an overview, please refer to: http://www.alchemyapi.com/products/features/microformats-parsing/ * For the docs, please refer to: http://www.alchemyapi.com/api/microformats-parsing/ - * + * * INPUT: * flavor -> which version of the call, i.e. url or html. * data -> the data to analyze, either the the url or html code. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * none * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.microformats = function(flavor, data, options, callback) { options = options || {} @@ -718,18 +718,18 @@ AlchemyAPI.prototype.microformats = function(flavor, data, options, callback) { /** * Categorized through the taxonomy call for text, HTML, or a URL. - * + * * INPUT: * flavor -> which version of the call (currently, only 'url' is supported) * data -> the data to analyze, either the the url or html code. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * showSourceText -> 0: disabled (default), 1: enabled. * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.taxonomy = function(flavor, data, options, callback) { options = options || {} @@ -742,16 +742,16 @@ AlchemyAPI.prototype.taxonomy = function(flavor, data, options, callback) { /** * Extracts the combined call for a URL. - * + * * INPUT: * flavor -> which version of the call (currently, only 'url' is supported) * data -> the data to analyze, either the the url or html code. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * extract -> VALUE,VALUE,VALUE,... (possible VALUEs: page-image,entity,keyword,title,author,taxonomy,concept,relation,doc-sentiment) - * extractMode -> (only applies when 'page-image' VALUE passed to 'extract' option) + * extractMode -> (only applies when 'page-image' VALUE passed to 'extract' option) * trust-metadata: less CPU-intensive, less accurate * always-infer: more CPU-intensive, more accurate * disambiguate -> whether to disambiguate detected entities, 0: disabled, 1: enabled (default) @@ -763,7 +763,7 @@ AlchemyAPI.prototype.taxonomy = function(flavor, data, options, callback) { * maxRetrieve -> maximum number of named entities to extract (default: 50) * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.combined = function(flavor, data, options, callback) { options = options || {} @@ -776,18 +776,18 @@ AlchemyAPI.prototype.combined = function(flavor, data, options, callback) { /** * Extracts images from a URL. - * + * * INPUT: * flavor -> which version of the call (currently, only 'url' is supported) * data -> the data to analyze, either the the url or html code. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * extractMode -> trust-metadata: less CPU-intensive and less accurate, always-infer: more CPU-intensive and more accurate * * OUTPUT: - * The response, already converted from JSON to a Javascript object. + * The response, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.image = function(flavor, data, options, callback) { options = options || {} @@ -800,19 +800,19 @@ AlchemyAPI.prototype.image = function(flavor, data, options, callback) { /** * Tags image with keywords - * + * * INPUT: * flavor -> which version of the call (currently, only 'url' or 'image' is supported) * data -> the URL to the data to analyze. * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. - * callback -> the callback function for this async function - * + * callback -> the callback function for this async function + * * Available Options: * extractMode -> trust-metadata: less CPU-intensive and less accurate, always-infer: more CPU-intensive and more accurate * imagePostMode -> not-raw: pass an unencoded image file with "image=URI_ENCODED_DATA"; raw: pass an unencoded image file using POST ('image' flavor only). * * OUTPUT: - * The response of keywords, already converted from JSON to a Javascript object. + * The response of keywords, already converted from JSON to a Javascript object. */ AlchemyAPI.prototype.image_keywords = function(flavor, data, options, callback) { @@ -828,4 +828,3 @@ AlchemyAPI.prototype.image_keywords = options, callback); } }; -