Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

clarifai

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clarifai - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

301

index.js
var _ = require('lodash')
var needle = require('needle')
var baseUrl = 'https://api.clarifai.com'
var tokenPath = '/v1/token/'
var tagPath = '/v1/tag/'
var feedbackPath = '/v1/feedback/'
var infoPath = '/v1/info/'
var _array = function(str) {
if (_.isArray(str)) {
return str
} else {
return [str]
}
}
var _encodeTagUrls = function (urls, lang) {
var data = ''
for (var url of _array(urls)) {
data += encodeURI('url=' + url) + '&'
}
if (lang) {
data += 'language=' + lang
}
return data
}
var _parseErr = function(err, resp, body, cb) {
if(err) {
cb(err)
} else if((resp.statusCode !== 200 && resp.statusCode !== 201) || body.status_code === 'PARTIAL_ERROR') {
cb(body)
} else {
cb(err, body)
}
}
var _request = function(verb, type, data, options, _this, cb) {
var url = 'https://api.clarifai.com'
switch(type) {
case 'feedback':
url += '/v1/feedback/'
break
case 'info':
url += '/v1/info/'
break
case 'tag':
url += '/v1/tag/'
break
case 'token':
url += '/v1/token/'
break
default:
throw err('Request Type not defined')
}
needle.request(verb, url, data, options, function(err, resp, body) {
if (body.status_code === 'TOKEN_INVALID' || body.status_code === 'TOKEN_NONE') {
_this.getAccessToken(function(err) {
if(err) {
cb(err)
} else {
options = { headers: _this.headers() }
needle.request(verb, url, data, options, function(err, resp, body) {
_parseErr(err, resp, body, cb)
})
}
})
} else {
_parseErr(err, resp, body, cb)
}
})
}
var formatImageResults = function(body) {

@@ -72,7 +136,9 @@ var results = body.results

var _this = this
needle.post(baseUrl + tokenPath, {
var data = {
grant_type: 'client_credentials',
client_id: this.id,
client_secret: this.secret
}, function(err, resp, body) {
}
_request('post', 'token', data, null, this, function(err, body) {
_this.accessToken = body.access_token

@@ -88,228 +154,59 @@ _this.expiresIn = body.expires_in

Clarifai.prototype.addTags = function(docIds, tags, cb) {
if (!_.isArray(docIds)) {
urls = [docIds]
}
if (!_.isArray(tags)) {
urls = [tags]
}
var data = ''
data +='docids=' + docIds.join(',')
data +='&add_tags=' + tags.join(',')
data +='docids=' + _array(docIds).join(',')
data +='&add_tags=' + _array(tags).join(',')
var _this = this
needle.post(baseUrl + feedbackPath, data, this.options, function(err, resp, body) {
if (body.status_code === 'TOKEN_INVALID' || body.status_code === 'TOKEN_NONE') {
_this.getAccessToken(function(err) {
if(err) {
cb(err)
} else {
_this.addTags(docIds, tags, cb)
}
})
} else {
cb(err, body)
}
});
_request('post', 'feedback', data, this.options, this, cb)
}
Clarifai.prototype.removeTags = function(docIds, tags, cb) {
if (!_.isArray(docIds)) {
urls = [docIds]
}
if (!_.isArray(tags)) {
urls = [tags]
}
var data = ''
data +='docids=' + docIds.join(',')
data +='&remove_tags=' + tags.join(',')
data +='docids=' + _array(docIds).join(',')
data +='&remove_tags=' + _array(tags).join(',')
var _this = this
needle.post(baseUrl + feedbackPath, data, this.options, function(err, resp, body) {
if (body.status_code === 'TOKEN_INVALID' || body.status_code === 'TOKEN_NONE') {
_this.getAccessToken(function(err) {
if(err) {
cb(err)
} else {
_this.removeTags(docIds, tags, cb)
}
})
} else {
cb(err, body)
}
});
_request('post', 'feedback', data, this.options, this, cb)
}
Clarifai.prototype.addSimilarDocIds = function(docIds, otherIds, cb) {
if (!_.isArray(docIds)) {
urls = [docIds]
}
if (!_.isArray(otherIds)) {
urls = [otherIds]
}
var data = ''
data +='docids=' + docIds.join(',')
data +='&similar_docids=' + otherIds.join(',')
data +='docids=' + _array(docIds).join(',')
data +='&similar_docids=' + _array(otherIds).join(',')
var _this = this
needle.post(baseUrl + feedbackPath, data, this.options, function(err, resp, body) {
if (body.status_code === 'TOKEN_INVALID' || body.status_code === 'TOKEN_NONE') {
_this.getAccessToken(function(err) {
if(err) {
cb(err)
} else {
_this.addSimilarDocIds(docIds, otherIds, cb)
}
})
} else {
cb(err, body)
}
});
_request('post', 'feedback', data, this.options, this, cb)
}
Clarifai.prototype.addDissimilarDocIds = function(docIds, otherIds, cb) {
if (!_.isArray(docIds)) {
urls = [docIds]
}
if (!_.isArray(otherIds)) {
urls = [otherIds]
}
var data = ''
data +='docids=' + docIds.join(',')
data +='&dissimilar_docids=' + otherIds.join(',')
data +='docids=' + _array(docIds).join(',')
data +='&dissimilar_docids=' + _array(otherIds).join(',')
var _this = this
needle.post(baseUrl + feedbackPath, data, this.options, function(err, resp, body) {
if (body.status_code === 'TOKEN_INVALID' || body.status_code === 'TOKEN_NONE') {
_this.getAccessToken(function(err) {
if(err) {
cb(err)
} else {
_this.addDissimilarDocIds(docIds, otherIds, cb)
}
})
} else {
cb(err, body)
}
});
_request('post', 'feedback', data, this.options, this, cb)
}
Clarifai.prototype.associateSearchTerms = function(docIds, terms, cb) {
if (!_.isArray(docIds)) {
urls = [docIds]
}
if (!_.isArray(terms)) {
urls = [terms]
}
var data = ''
data +='docids=' + docIds.join(',')
data +='&search_click=' + terms.join(',')
data +='docids=' + _array(docIds).join(',')
data +='&search_click=' + _array(terms).join(',')
var _this = this
needle.post(baseUrl + feedbackPath, data, this.options, function(err, resp, body) {
if (body.status_code === 'TOKEN_INVALID' || body.status_code === 'TOKEN_NONE') {
_this.getAccessToken(function(err) {
if(err) {
cb(err)
} else {
_this.associateSearchTerms(docIds, terms, cb)
}
})
} else {
cb(err, body)
}
});
_request('post', 'feedback', data, this.options, this, cb)
}
Clarifai.prototype.getAPIDetails = function(cb) {
var _this = this
needle.get(baseUrl + infoPath, this.options, function(err, resp, body) {
if (body.status_code === 'TOKEN_INVALID' || body.status_code === 'TOKEN_NONE') {
_this.getAccessToken(function(err) {
if(err) {
cb(err)
} else {
_this.getAPIDetails(cb)
}
})
} else {
cb(err, body.results)
}
});
_request('get', 'info', null, this.options, this, function(err, body) {
cb(err, body.results)
})
}
Clarifai.prototype.tagImagesFromUrls = function(urls, cb, lang) {
var data = ''
Clarifai.prototype.tagFromUrls = function(type, urls, cb, lang) {
var data = _encodeTagUrls(urls, lang)
if (!_.isArray(urls)) {
urls = [urls]
}
for (var url of urls) {
data += encodeURI('url=' + url) + '&'
}
if (lang) {
data += 'language=' + lang
}
var _this = this
needle.post(baseUrl + tagPath, data, this.options, function(err, resp, body) {
if (body.status_code === 'TOKEN_INVALID' || body.status_code === 'TOKEN_NONE') {
_this.getAccessToken(function(err) {
if(err) {
cb(err)
} else {
_this.tagImagesFromUrls(urls, cb, lang)
}
})
} else {
_request('post', 'tag', data, this.options, this, function(err, body) {
if(err) {
cb(err)
} else if (type === 'image') {
cb(err, formatImageResults(body))
}
});
}
Clarifai.prototype.tagVideosFromUrls = function(urls, cb, lang) {
var data = ''
if (!_.isArray(urls)) {
urls = [urls]
}
for (var url of urls) {
data += encodeURI('url=' + url) + '&'
}
if (lang) {
data += 'language=' + lang
}
var _this = this
needle.post(baseUrl + tagPath, data, this.options, function(err, resp, body) {
if (body.status_code === 'TOKEN_INVALID' || body.status_code === 'TOKEN_NONE') {
_this.getAccessToken(function(err) {
if(err) {
cb(err)
} else {
_this.tagVideosFromUrls(urls, cb, lang)
}
})
} else {
} else{
cb(err, formatVideoResults(body))
}
});
})
}

@@ -316,0 +213,0 @@

{
"name": "clarifai",
"version": "0.2.1",
"version": "0.3.0",
"dependencies": {

@@ -5,0 +5,0 @@ "lodash": {

{
"name": "clarifai",
"version": "0.2.1",
"version": "0.3.0",
"description": "A Node integration library for clarifai",

@@ -5,0 +5,0 @@ "main": "index.js",

[![Circle CI](https://circleci.com/gh/ctartist621/clarifai/tree/master.svg?style=svg&circle-token=e15ad7ff3e856e1b86fb002868fb4c9e98d4e22e)](https://circleci.com/gh/ctartist621/clarifai/tree/master) [![npm version](https://badge.fury.io/js/clarifai.svg)](https://badge.fury.io/js/clarifai) [![Codacy Badge](https://api.codacy.com/project/badge/grade/fd34502ca14c4c0ba556e90a81d77991)](https://www.codacy.com/app/ctartist621/clarifai)
# clarifai
clarifai api wrapper
A Node integration library for clarifai

@@ -33,3 +33,3 @@

##Authentication
## Authentication
Once initialized, the library will take care of renewing the access token as it expires. If you need to OAuth for an access token, you can simply call:

@@ -42,7 +42,7 @@ ```

##Tagging
## Tagging
###Image Tagging via url
### Image Tagging via url
```
client.tagImagesFromUrls(url(s), function(err, results) {
client.tagFromUrls('image', url, function(err, results) {
// Callback code here

@@ -55,71 +55,114 @@ }, [language])

```
{ docId: 15512461224882630000,
{
docId: 15512461224882630000,
docIdStr: '31fdb2316ff87fb5d747554ba5267313',
tags:
[ { class: 'train',
conceptId: 'ai_HLmqFqBf',
probability: 0.9989112019538879 },
{ class: 'railway',
conceptId: 'ai_fvlBqXZR',
probability: 0.9975532293319702 },
{ class: 'transportation system',
conceptId: 'ai_Xxjc3MhT',
probability: 0.9959157705307007 },
{ class: 'station',
conceptId: 'ai_6kTjGfF6',
probability: 0.9925730228424072 },
{ class: 'train',
conceptId: 'ai_RRXLczch',
probability: 0.9925559759140015 },
{ class: 'travel',
conceptId: 'ai_VRmbGVWh',
probability: 0.9878921508789062 },
{ class: 'tube',
conceptId: 'ai_SHNDcmJ3',
probability: 0.9816359281539917 },
{ class: 'commuter',
conceptId: 'ai_jlb9q33b',
probability: 0.9712483286857605 },
{ class: 'railway',
conceptId: 'ai_46lGZ4Gm',
probability: 0.9690325260162354 },
{ class: 'traffic',
conceptId: 'ai_tr0MBp64',
probability: 0.9687051773071289 },
{ class: 'blur',
conceptId: 'ai_l4WckcJN',
probability: 0.9667078256607056 },
{ class: 'platform',
conceptId: 'ai_2gkfMDsM',
probability: 0.9624242782592773 },
{ class: 'urban',
conceptId: 'ai_CpFBRWzD',
probability: 0.960752010345459 },
{ class: 'no person',
conceptId: 'ai_786Zr311',
probability: 0.9586490392684937 },
{ class: 'business',
conceptId: 'ai_6lhccv44',
probability: 0.9572030305862427 },
{ class: 'track',
conceptId: 'ai_971KsJkn',
probability: 0.9494642019271851 },
{ class: 'city',
conceptId: 'ai_WBQfVV0p',
probability: 0.940894365310669 },
{ class: 'fast',
conceptId: 'ai_dSCKh8xv',
probability: 0.9399334192276001 },
{ class: 'road',
conceptId: 'ai_TZ3C79C6',
probability: 0.9312160611152649 },
{ class: 'terminal',
conceptId: 'ai_VSVscs9k',
probability: 0.9230834245681763 } ] }
tags: [
{
class: 'train',
conceptId: 'ai_HLmqFqBf',
probability: 0.9989112019538879
},
{
class: 'railway',
conceptId: 'ai_fvlBqXZR',
probability: 0.9975532293319702
},
{
class: 'transportation system',
conceptId: 'ai_Xxjc3MhT',
probability: 0.9959157705307007
},
{
class: 'station',
conceptId: 'ai_6kTjGfF6',
probability: 0.9925730228424072
},
{
class: 'train',
conceptId: 'ai_RRXLczch',
probability: 0.9925559759140015
},
{
class: 'travel',
conceptId: 'ai_VRmbGVWh',
probability: 0.9878921508789062
},
{
class: 'tube',
conceptId: 'ai_SHNDcmJ3',
probability: 0.9816359281539917
},
{
class: 'commuter',
conceptId: 'ai_jlb9q33b',
probability: 0.9712483286857605
},
{
class: 'railway',
conceptId: 'ai_46lGZ4Gm',
probability: 0.9690325260162354
},
{
class: 'traffic',
conceptId: 'ai_tr0MBp64',
probability: 0.9687051773071289
},
{
class: 'blur',
conceptId: 'ai_l4WckcJN',
probability: 0.9667078256607056
},
{
class: 'platform',
conceptId: 'ai_2gkfMDsM',
probability: 0.9624242782592773
},
{
class: 'urban',
conceptId: 'ai_CpFBRWzD',
probability: 0.960752010345459
},
{
class: 'no person',
conceptId: 'ai_786Zr311',
probability: 0.9586490392684937
},
{
class: 'business',
conceptId: 'ai_6lhccv44',
probability: 0.9572030305862427
},
{
class: 'track',
conceptId: 'ai_971KsJkn',
probability: 0.9494642019271851
},
{
class: 'city',
conceptId: 'ai_WBQfVV0p',
probability: 0.940894365310669
},
{
class: 'fast',
conceptId: 'ai_dSCKh8xv',
probability: 0.9399334192276001
},
{
class: 'road',
conceptId: 'ai_TZ3C79C6',
probability: 0.9312160611152649
},
{
class: 'terminal',
conceptId: 'ai_VSVscs9k',
probability: 0.9230834245681763
}
]
}
```
If one image was passed in, the results object will be the single object representing the results for that image. If more than one was passed in, it will be an array of results objects.
###Video Tagging via url
### Video Tagging via url
```
client.tagVideosFromUrls(url(s), function(err, results) {
client.tagFromUrls('video', url, function(err, results) {
// Callback code here

@@ -208,3 +251,3 @@ }, [language])

##Feedback
## Feedback
For all the feedback functions, any input parameters can be a single string, or an array of strings.

@@ -266,3 +309,3 @@

##API Info
## API Info
Retrieves information about the API.

@@ -269,0 +312,0 @@ ```

@@ -26,3 +26,3 @@ var should = require('chai').should(),

var url = 'http://www.clarifai.com/img/metro-north.jpg'
client.tagImagesFromUrls(url, function(err, resp) {
client.tagFromUrls('image', url, function(err, resp) {
should.not.exist(err)

@@ -41,3 +41,3 @@ resp.should.have.property('docId')

var url = 'http://www.clarifai.com/img/metro-north.jpg'
client.tagImagesFromUrls(url, function(err, resp) {
client.tagFromUrls('image', url, function(err, resp) {
should.not.exist(err)

@@ -60,3 +60,3 @@ resp.should.have.property('docId')

client.tagImagesFromUrls(urls, function(err, resp) {
client.tagFromUrls('image', urls, function(err, resp) {
should.not.exist(err)

@@ -75,7 +75,7 @@ resp.should.have.length(2)

describe.skip('#Videos', function() {
describe('#Videos', function() {
it('should tag a video from a url', function(done) {
this.timeout(20000);
var url = 'https://archive.org/download/test-mpeg/test-mpeg.mpg'
client.tagVideosFromUrls(url, function(err, resp) {
var url = 'http://html5videoformatconverter.com/data/images/happyfit2.mp4'
client.tagFromUrls('video', url, function(err, resp) {
should.not.exist(err)

@@ -95,5 +95,5 @@ resp.should.have.property('docId')

it('should tag an video from a url in another language', function(done) {
var url = 'https://archive.org/download/test-mpeg/test-mpeg.mpg'
var url = 'http://html5videoformatconverter.com/data/images/happyfit2.mp4'
this.timeout(20000);
client.tagVideosFromUrls(url, function(err, resp) {
client.tagFromUrls('video', url, function(err, resp) {
should.not.exist(err)

@@ -112,9 +112,9 @@ resp.should.have.property('docId')

it('should tag multiple videos from a set of urls', function(done) {
it.skip('should tag multiple videos from a set of urls', function(done) {
this.timeout(60000);
var urls = [
'https://archive.org/download/test-mpeg/test-mpeg.mpg',
'https://archive.org/download/test-mpeg/test-mpeg.mpg',
'http://html5videoformatconverter.com/data/images/happyfit2.mp4',
'http://html5videoformatconverter.com/data/images/happyfit2.mp4',
]
client.tagVideosFromUrls(urls, function(err, resp) {
client.tagFromUrls('video', urls, function(err, resp) {
should.not.exist(err)

@@ -121,0 +121,0 @@ resp.should.have.length(2)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc