NodeJS Gnip module
Connect to Gnip streaming API and manage rules.
You must have a Gnip account with any data source available, like Twitter Power Track.Currenly, this module only supports JSON activity stream format, so you must enable data normalization in your admin panel.
Gnip.Stream
This class is an EventEmitter and allows you to connect to the stream and start receiving data.Constructor options
timeout
backfillMinutes
partition
parser
Matching tag IDs are sent to us as big integers which can't be reliably parsed by the native JSON library in Node.js. More info on this issue can be found at StackOverflow
API methods
start()
end()
Events
ready
data
error
object
tweet
delete
end
Gnip.Rules
This class allows you to manage an unlimited number of tracking rules.Constructor options
user
password
url
https://gnip-api.twitter.com/rules/${streamType}/accounts/${account}/publishers/twitter/${label}.json
batchSize
parser
parser
option allowed in the Gnip Stream constructor, you can pass a custom parser handler/library for incoming JSON data. This is optional, and defaults to the json-bigint library. More details.cacheFile
Gnip.Rules
uses a file for caching the current state of the rules configuration, the default path is in the directory of the package. This optional configuration allows you to change the path as the default one may cause problems in applications where node_modules
is in a read-only filesystems (e.g. AWS Lambda).API methods
getAll(callback)
update(rules: Array, callback)
Rules are sent in batches of
options.batchSize
, so you can pass an unlimited number of rules.The current tracking rules are stored in a local JSON file so you can update the existing rules efficiently without having to remove them all. The callback receives an object as the 2nd argument and contains the number of added and deleted rules.
clearCache(callback)
The following methods uses Gnip API directly and ignores the local cache. Avoid usage if you are working with too many rules!
live.update(rules: Array, callback)
live.add(rules: Array, callback)
live.remove(rules: Array, callback)
live.getAll(callback)
live.getByIds(ids: Array, callback)
live.removeAll(callback)
Gnip.Search
This class is an EventEmitter and allows you to connect to either the 30 day or full archive search API and start receiving data.Constructor options
user
password
url
https://gnip-api.twitter.com/search/30day/accounts/{ACCOUNT_NAME}/{LABEL}.json
`
-
query`
Rule to match tweets.fromDate
toDate
maxResults
tag
bucket
rateLimiter
const RateLimiter = require('limiter').RateLimiter;
// Allow 60 requests per minute
const limiter = new RateLimiter(60, 'minute');
const stream = new Gnip.Search({
rateLimiter : limiter,
...
});
API methods
start()
end()
Events
ready
error
object
tweet
end
Gnip.Usage
This class allows you to track activity consumption across Gnip products.Constructor options
url
user
password
const usage = new Gnip.Usage({
url : 'https://gnip-api.twitter.com/metrics/usage/accounts/{ACCOUNT_NAME}.json',
user : 'xxx',
password : 'xxx'
});
API Methods
get(callback)
get(parameters: object, callback)
```js usage.get({ bucket:'day', fromDate:'201612010000', toDate:'201612100000' },function( err, body ) {
...
});
````Installation
npm install gnip
Example Usage
```js
const Gnip = require('gnip');const stream = new Gnip.Stream({ url : 'https://gnip-stream.twitter.com/stream/powertrack/accounts/xxx/publishers/twitter/prod.json', user : 'xxx', password : 'xxx', backfillMinutes: 5 // optional }); stream.on('ready', function() { console.log('Stream ready!'); }); stream.on('tweet', function(tweet) { console.log(tweet); }); stream.on('error', function(err) { console.error(err); });
const rules = new Gnip.Rules({ url : 'https://gnip-api.twitter.com/rules/powertrack/accounts/xxx/publishers/twitter/prod.json', user : 'xxx', password : 'xxx', batchSize: 1234 // not required, defaults to 5000 });
const newRules = '#hashtag', 'keyword', '@user', {value: 'keyword as object'}, {value: '@demianr85', tag: 'rule tag'} ;
rules.update(newRules, function(err) { if (err) throw err; stream.start(); });
const search = new Gnip.Search({ url : 'https://gnip-stream.twitter.com/stream/powertrack/accounts/xxx/publishers/twitter/prod.json', user : 'xxx', password : 'xxx', query : '@user' });
search.on('tweet', function(tweet) { console.log(tweet); });
search.on('error', function(err) { console.error(err); });
search.on('end', function(err) { if( err )
console.error(err);
});// search counts usage const counts = new Gnip.Search({ url : 'https://gnip-stream.twitter.com/stream/powertrack/accounts/xxx/publishers/twitter/prod/counts.json', user : 'xxx', password : 'xxx', query : '@user', bucket: 'day' });
counts.on('object', function(object) { console.log(object.results); counts.end(); }); ``` More details and tests soon...