Skip to content

Commit 3ee382b

Browse files
added category to alchemy language and updated alchemy vision
1 parent 0abef3a commit 3ee382b

File tree

5 files changed

+41
-53
lines changed

5 files changed

+41
-53
lines changed

lib/alchemy_endpoints.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
"html" : "/html/HTMLGetText"
4747
},
4848

49+
"category": {
50+
"url" : "/url/URLGetCategory",
51+
"text" : "/text/TextGetCategory"
52+
},
53+
54+
"publication_date": {
55+
"url" : "/url/URLGetPubDate",
56+
"html" : "/html/HTMLGetPubDate"
57+
},
58+
4959
"text_raw": {
5060
"url" : "/url/URLGetRawText",
5161
"html" : "/html/HTMLGetRawText"
@@ -80,7 +90,7 @@
8090

8191
"image_link": {
8292
"url" : "/url/URLGetImage",
83-
"html" : "/html/HTMLGetImage"
93+
"html" : "/url/HTMLGetImage"
8494
},
8595

8696
"image_keywords": {

services/alchemy_data_news/v1.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
var extend = require('extend');
2020
var requestFactory = require('../../lib/requestwrapper');
21-
var endpoints = require('../../lib/alchemy_endpoints.json');
22-
var helper = require('../../lib/helper');
2321

2422
function errorFormatter(cb) {
2523
return function(err, result, response) {
@@ -38,32 +36,6 @@ function errorFormatter(cb) {
3836
};
3937
}
4038

41-
function createRequest(method) {
42-
return function(_params, callback ) {
43-
var params = _params || {};
44-
var accepted_formats = Object.keys(endpoints[method]);
45-
var format = helper.getFormat(params, accepted_formats);
46-
47-
if (format === null) {
48-
callback(new Error('Missing required parameters: ' +
49-
accepted_formats.join(', ') +
50-
' needs to be specified'));
51-
return;
52-
}
53-
54-
var parameters = {
55-
options: {
56-
url: endpoints[method][format],
57-
method: 'POST',
58-
json: true,
59-
form: extend({outputMode: 'json'}, params) // change default output to json
60-
},
61-
defaultOptions: this._options
62-
};
63-
return requestFactory(parameters, errorFormatter(callback));
64-
};
65-
}
66-
6739
function AlchemyDataNews(options) {
6840
// Default URL
6941
var serviceDefaults = {

services/alchemy_language/v1.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,15 @@ AlchemyLanguage.prototype.relations = createRequest('relations');
130130
*/
131131
AlchemyLanguage.prototype.category = createRequest('category');
132132

133+
/**
134+
* Categorizes the text for text, a URL or HTML.
135+
*/
136+
AlchemyLanguage.prototype.publicationDate = createRequest('publication_date');
137+
133138
/**
134139
* Detects the RSS/ATOM feeds for a URL or HTML.
135140
*/
136-
AlchemyLanguage.prototype.feeds = createRequest('category');
141+
AlchemyLanguage.prototype.feeds = createRequest('feeds');
137142

138143
/**
139144
* Parses the microformats for a URL or HTML.

services/alchemy_vision/v1.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,13 @@ function AlchemyVision(options) {
8181
/**
8282
* Extracts images from a URL or html
8383
*/
84-
AlchemyVision.prototype.imageLinks = function(_params, callback ) {
84+
AlchemyVision.prototype.getImageLinks = function(_params, callback ) {
8585
var params = _params || {};
8686
var accepted_formats = Object.keys(endpoints['image_link']);
8787
var format = helper.getFormat(params, accepted_formats);
8888

8989
if (format === null) {
90-
callback(new Error('Missing required parameters: ' +
91-
accepted_formats.join(', ') + ' needs to be specified'));
90+
callback(new Error('Missing required parameters: ' + accepted_formats.join(', ') + ' needs to be specified'));
9291
return;
9392
}
9493

@@ -97,7 +96,7 @@ AlchemyVision.prototype.imageLinks = function(_params, callback ) {
9796
url: endpoints['image_link'][format],
9897
method: 'POST',
9998
json: true,
100-
qs: extend({outputMode: 'json'}, params) // change default output to json
99+
qs: extend({}, params,{outputMode: 'json'}) // change default output to json
101100
},
102101
defaultOptions: this._options
103102
};
@@ -109,44 +108,46 @@ AlchemyVision.prototype.imageLinks = function(_params, callback ) {
109108
* Tags image with keywords
110109
*/
111110
AlchemyVision.prototype.getImageKeywords = function(_params, callback) {
112-
var params = _params || {};
111+
var params = extend({}, _params);
113112
var accepted_formats = Object.keys(endpoints['image_keywords']);
114113
var format = helper.getFormat(params, accepted_formats);
115-
var image = null;
114+
116115
if (format === null) {
117-
callback(new Error('Missing required parameters: ' +
118-
accepted_formats.join(', ') + ' needs to be specified'));
116+
callback(new Error('Missing required parameters: ' + accepted_formats.join(', ') + ' needs to be specified'));
119117
return;
120118
}
121119

120+
var parameters = {
121+
options: {
122+
url: endpoints['image_keywords'][format],
123+
method: 'POST',
124+
json: true,
125+
qs: {outputMode: 'json'} // change default output to json
126+
},
127+
defaultOptions: this._options
128+
};
122129

123130
if (typeof(params.image) !== 'undefined') {
131+
124132
// check if image is stream or string
125-
if (typeof(params.image) !== 'string' && !isStream(params.image)) {
133+
if ((typeof(params.image) !== 'string') && !isStream(params.image)) {
126134
callback(new Error('Invalid arguments: image needs to be a stream or base64'));
127135
return;
128136
}
129137

130138
if (isStream(params.image)) {
131139
params.imagePostMode = 'raw';
132-
image = params.image;
133-
delete params.image;
140+
// handle raw images
141+
parameters.options.body = params.image;
142+
134143
} else {
135144
params.imagePostMode = 'not-raw';
145+
parameters.options.formData = params;
136146
}
147+
} else {
148+
parameters.options.formData = params;
137149
}
138150

139-
var parameters = {
140-
options: {
141-
url: endpoints['image_links'][format],
142-
method: 'POST',
143-
json: true,
144-
body: image,
145-
qs: extend({outputMode: 'json'}, params) // change default output to json
146-
},
147-
defaultOptions: this._options
148-
};
149-
150151
return requestFactory(parameters, errorFormatter(callback));
151152
};
152153

@@ -156,7 +157,7 @@ AlchemyVision.prototype.getImageKeywords = function(_params, callback) {
156157
AlchemyVision.prototype.recognizeFaces = function(_params, callback) {
157158
var params = _params || {};
158159

159-
if (typeof(params.image) !== 'string')
160+
if (params.image && typeof(params.image) !== 'string')
160161
params.imagePostMode = 'raw';
161162

162163
return createRequest('image_recognition').call(this, params, callback);

test/resources/obama.jpg

11.1 KB
Loading

0 commit comments

Comments
 (0)