Giter VIP home page Giter VIP logo

ntwitter's Introduction

DEPRECATED

This repository has been unmaintained for two years, is deprecated on npm, and should not be considered an authoritative source of anything. Sorry for the inconvenience.

Asynchronous Twitter client API for node.js

ntwitter is an improved version of jdub's node-twitter, which in turn was inspired by, and uses some code from, technoweenie's twitter-node.

Installation

You can install ntwitter and its dependencies with npm: npm install ntwitter.

Getting started

This library is, for the most part, the same API as node-twitter. Much of the documentation below is straight from node-twitter - credit goes to jdub for putting all this together in the first place.

The most significant API change involves error handling in callbacks. Callbacks now receive the error as a separate parameter, rather than as part of the data. This is consistent with node's standard library. Callbacks should now look something like this:

function (err, result) {
  if (err) return callback(err);

  // Do something with 'result' here
}

Where callback is the parent function's callback. (Or any other function you want to call on error.)

Setup API

The keys listed below can be obtained from dev.twitter.com after setting up a new App.

var twitter = require('ntwitter');

var twit = new twitter({
  consumer_key: 'Twitter',
  consumer_secret: 'API',
  access_token_key: 'keys',
  access_token_secret: 'go here'
});

REST API

Interaction with other parts of Twitter is accomplished through their RESTful API. The best documentation for this exists at dev.twitter.com. Convenience methods exist for many of the available methods, but some may be more up-to-date than others. If your Twitter interaction is very important, double-check the parameters in the code with Twitter's current documentation.

Note that all functions may be chained:

twit
  .verifyCredentials(function (err, data) {
    console.log(data);
  })
  .updateStatus('Test tweet from ntwitter/' + twitter.VERSION,
    function (err, data) {
      console.log(data);
    }
  );

Search API

twit.search('nodejs OR #node', {}, function(err, data) {
  console.log(data);
});

Streaming API

The stream() callback receives a Stream-like EventEmitter.

Here is an example of how to call the statuses/sample method:

twit.stream('statuses/sample', function(stream) {
  stream.on('data', function (data) {
    console.log(data);
  });
});

Here is an example of how to call the 'statuses/filter' method with a bounding box over San Fransisco and New York City ( see streaming api for more details on locations ):

twit.stream('statuses/filter', {'locations':'-122.75,36.8,-121.75,37.8,-74,40,-73,41'}, function(stream) {
  stream.on('data', function (data) {
    console.log(data);
  });
});

ntwitter also supports user and site streams:

twit.stream('user', {track:'nodejs'}, function(stream) {
  stream.on('data', function (data) {
    console.log(data);
  });
  stream.on('end', function (response) {
    // Handle a disconnection
  });
  stream.on('destroy', function (response) {
    // Handle a 'silent' disconnection from Twitter, no end/error event fired
  });
  // Disconnect stream after five seconds
  setTimeout(stream.destroy, 5000);
});

Contributors

Lots of people contribute to this project. You should too!

TODO

ntwitter's People

Contributors

ahinni avatar avianflu avatar benatkin avatar blakmatrix avatar boatmeme avatar bobrik avatar clintandrewhall avatar crabbot avatar dylanvee avatar fatshotty avatar fent avatar francois2metz avatar hdachev avatar impronunciable avatar jamescodes avatar jdub avatar jmalonzo avatar linuxlewis avatar martindale avatar mef avatar mheap avatar mrdnk avatar nulltask avatar ryantenney avatar secos avatar siygle avatar tcr avatar technoweenie avatar tim-smart avatar yinshanyang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ntwitter's Issues

Search example in README.md fails.

The search example in README.md fails when you try to run it.

twit.search('nodejs OR #node', function(err, data) {
  console.log(data);
});

It should, instead be:

twit.search('nodejs OR #node', {}, function(err, data) {
  console.log(data);
});

Twitter.prototype.search starting on line 155 of twitter.js will set params to null without the empty object, and will then attempt to run that null through the util.merge function with the query parameters. This results in the following error:

TypeError: Cannot set property 'q' of null
  at Object.merge (/path/to/node_modules/ntwitter/lib/utils.js:9:18)
  at Twitter.search (/path/to/node_modules/ntwitter/lib/twitter.js:167:18)
  at Object.<anonymous> (/path/to/scratch.js:55:8)

Reply to a tweet

I see in the readme there is an example of how to publish a status update.

Is there any way to have a tweet look like a reply to a tweet from your stream, instead of just a plan status update?

Streams seem to stop working after a few hours

I've been testing it locally and on nodejitsu, but after a few hours, the stream seems to stop working. Is this because i'm not handling stream disconnections when a user disconnects by closing their tab/window ?

This is the code i'm using...

    twitter
        .stream('statuses/filter', {track: 'diablo'}, function(stream) {
            stream.on('data', function (data) {
                var encoded = strencode(data);
                var tweet = JSON.parse(encoded);
                var array = { 
                    "id": tweet.id,
                    "screen_name": tweet.user.screen_name,
                    "text" : tweet.text, 
                    "profile_image_url" : tweet.user.profile_image_url 
                };
                io.sockets.send(strencode(array));
            });
            stream.on('end', function (response) {
              // Handle a disconnection
            });
            stream.on('destroy', function (response) {
              // Handle a 'silent' disconnection from Twitter, no end/error event fired
            });
        });

Is follow supported for Stream?

When I do a stream with follow I get an "Uncaught, unspecified 'error' event." Example:

tweet.stream 'statuses/filter',
    track: "test"
    follow: "joe"
, (stream) ->
    stream.on 'data', (data) ->
        client_data = {
            content : data.text
            img     : data.user.profile_image_url_https
            user    : data.user.screen_name
            tags    : []
        }

Is this not legal? If I just do the track it works great. Locations work too, but not follow.

Allow access to response headers, especially in errors

This is most valuable for rate limit errors. Currently, the error object when a client is rate limited is:

{ [Error: HTTP Error 400: Bad Request]
  statusCode: 400,
  data: '{"error":"Rate limit exceeded. Clients may not make more than 350 requests per hour.","request":"\\/1\\/lists.json?screen_name=JFSIII&cursor=-1"}' }

However, this doesn't inform the client when their limiting will end. That information is available via the X-RateLimit-Reset header. The value is the UNIX timestamp when the limit will be lifted.

Even successful connections have useful information. Most notably X-RateLimit-Remaining which informs the client how many calls are left before they get limited.

I'm just getting started with this library, so I don't have any suggestions for the preferred way to make them available. I only know that they are a requirement for clients. They are so important, I feel like I must have missed their inclusion. If so, I apologize for the noisy ticket and ask that you point me to the documentation or provide some example code.

Filtering by "track" and "locations" isn't filtering properly

I've got the following settings:

twitter.stream('statuses/filter', { 

    track      : "pizza",
    locations  : '-150.75,0, -60,90'

}, function(stream) {

    stream.on('data', function (tweet) {
        // do stuff
    }); 

});

Is the error between my keyboard and monitor, or is something else going on?

memory leaks

I use nodejs-0.5.10 and here is my example.

As you may see, rss memory grows constantly. Just like heap memory. There may be nodejs bug, but may be your.

Also I have example with asyncjs queue for twitter requests (works faster), same effect there.

What do you think?

Trend endpoint deprecated?

I think Twitter Trend API change its endpoint and deprecate old one, getTrends and getCurrentTrends should change to trends/:woeid to get trends information.

Sometimes tweets are emitted as 'error' events

Sometimes I receive the tweets through the 'error' events emitted by ntwitter's stream object:

stream.on('error', function (err) {
if (err.text !== undefined) {
console.log("twitter error: "+inspect(err)); <-- a full, working tweet is printed here!
}
});

It's a bit random (it can happen or not, even if I don't touch my settings/code/tokens)
my guess it's some kind of http code misinterpreted by ntwitter as "error", but still valid? maybe a "url changed" or "redirection" http code?

I don't have much time right now to dig inside ntwitter internals and figure out what is happening, but if I can get more details I'll post them here.

my config: node 0.6 / ntwitter 0.3.0

App crash randomly on... event.emit

Hi there,

I've got this very simple app :

var twitter = require('ntwitter');
var credentials = require('./credentials.js');
var http = require('http');
var port = process.env.PORT || 5000;

var httpServer = http.createServer(function (request, response) {
  request.addListener('end', function () {
            clientFiles.serve(request, response);
        });
}).listen(port);

var io = require('socket.io').listen(httpServer);

var new_tweet = {};

var t = new twitter({
    consumer_key: credentials.consumer_key,
    consumer_secret: credentials.consumer_secret,
    access_token_key: credentials.access_token_key,
    access_token_secret: credentials.access_token_secret
});
io.sockets.on('connection', function (socket) {
    t.stream(
        'statuses/filter',
        { track: ['awesome', 'cool', 'rad', 'gnarly', 'groovy'] },
        function(stream) {
            stream.on('data', function(tweet) {
                //console.log(tweet.text);
                new_tweet = tweet.text;
                socket.emit('new_tweet', new_tweet);
            });
        }
    );
});

Well anyone can tried it, quiet simple and straightforward. The thing is that sometime it crashes, and I do not get why ??

The log error is :

11:55:32 web.1     | events.js:68
11:55:32 web.1     |         throw new Error("Uncaught, unspecified 'error' event.");
11:55:32 web.1     |               ^
11:55:32 web.1     | Error: Uncaught, unspecified 'error' event.
11:55:32 web.1     |     at EventEmitter.emit (events.js:68:15)
11:55:32 web.1     |     at ClientRequest.Twitter.stream (/usr/local/lib/node_modules/ntwitter/lib/twitter.js:251:14)
11:55:32 web.1     |     at ClientRequest.EventEmitter.emit (events.js:88:17)
11:55:32 web.1     |     at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1455:7)
11:55:32 web.1     |     at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)
11:55:32 web.1     |     at CleartextStream.socketOnData [as ondata] (http.js:1366:20)
11:55:32 web.1     |     at CleartextStream.CryptoStream._push (tls.js:495:27)
11:55:32 web.1     |     at SecurePair.cycle (tls.js:849:20)
11:55:32 web.1     |     at EncryptedStream.CryptoStream.write (tls.js:230:13)
11:55:32 web.1     |     at Socket.ondata (stream.js:38:26)
11:55:32 web.1     | process terminated
11:55:32 system    | sending SIGTERM to all processes

Here is the package.json file for those of you who would like to try :

{ 
    "name": "Twitter.stream.ringthebell.io",
    "description": "Twitter server allowing to fetch the twitter stream API and push to a javascript client",
    "version": "0.0.1",
    "dependencies": { 
        "socket.io" : "0.9.10",
        "ntwitter": "0.4.1"
    },
    "engines": {
        "node": "0.8.x",
        "npm": "1.1.49"
    }
}

Any help would be much much appreciate :)

Please not that using only console.log and removing the socket.io makes it work perfectly (didn't yet see a crash)

support Stream interface

hey this is really cool, but it would be even cooler if it supported the Stream interface.

like, you could change this:

twit.stream('user', {track:'nodejs'}, function(stream) {
    stream.on('data', function (data) {
        console.log(console.dir(data));
    });
    // Disconnect stream after five seconds
    setTimeout(stream.destroy, 5000);
});

to this:

twit.stream('user', {track:'nodejs'}).pipe(writable)

I'm also working on a toolkit for working with streams, https://github.com/dominictarr/event-stream that might have some useful examples.

Error: Uncaught, unspecified 'error' event.

Hi ladies, gentlemen

I have a weird problem I can't seem to be able to trace. Any help is appreciated.
This is the error I get:

events.js:50
throw new Error("Uncaught, unspecified 'error' event.");
^
Error: Uncaught, unspecified 'error' event.
at EventEmitter.emit (events.js:50:15)
at ClientRequest. (/root/node/test/node_modules/ntwitter/lib/twitter.js:251:14)
at ClientRequest.emit (events.js:67:17)
at HTTPParser.onIncoming (http.js:1258:11)
at HTTPParser.onHeadersComplete (http.js:102:31)
at CleartextStream.ondata (http.js:1147:24)
at CleartextStream._push (tls.js:367:27)
at SecurePair.cycle (tls.js:688:20)
at EncryptedStream.write (tls.js:122:13)
at Socket.ondata (stream.js:38:26)

It worked on localhost, for a while. On my server it doesn't. At all.

Thanks in advance

Fabian

String IDs (recommended by Twitter) break ntwitter

Took me a while to debug this: I was trying to follow (createFriendship) someone, and the API was responding with a 404. It turns out it's because I was passing an ID as a string -- as recommended by Twitter [1] -- which ntwitter was considering a screenname instead of an ID:

https://github.com/AvianFlu/ntwitter/blob/master/lib/twitter.js#L841-L844

What to do here? In my case, I only have the ID, not the screenname, and it's coming from a string (the cookie from "Sign in with Twitter" functionality). As a temporary workaround, I can parseInt() on it, but that's obviously not recommended.

Would be nice to support string IDs. What do you think? Thanks! And thanks for the great library and work in general. =)

[1] https://dev.twitter.com/docs/things-every-developer-should-know

Error: socket hang up

Hi girls 'n guys, I'm having some [Error: socket hang up] code: 'ECONNRESET' over here when trying to get the Favorites of about 280 Users at Heroku. Do you have a clue what's happening there? xD

Thanks :)

How to reply to a status id?

I can't figure out how to reply to a certain status. In the API, one of the optional parameters when updating a status is <in_reply_to_status_id> (I also tried <in_reply_to_status_id_str>). However, it's not replying to the status, only updating the status with the @username. Has anyone run into this issue too or found a solution?

updateStatus fails if the tweet contains an apostrophe

presumably all tweets that contain apostrophes fail. With the demo code from http://www.catonmat.net/blog/nodejs-modules-ntwitter/ i replace:

.updateStatus('Test tweet from ntwitter/' + twitter.VERSION,

with:

.updateStatus('test \' ',

and then i get:

Tweeting failed: Error: HTTP Error 401: Unauthorized, API message: {"errors":[{"message":"Could not authenticate you","code":32}]}

(note this is not verifyCredentials reporting this error, but updateStatus, and if i tweet without apostrophes, then it works)

I npm-installed ntwitter two days ago, so i'm using version 0.2.8 (on a Debian server).

For now i will avoid using apostrophes in my tweets :)

Streaming broken?

As of Nov 30th I am getting a 406 response on all stream search requests?

REST Get returns null to callback?

Why is line 105 of twitter.js:
callback(null, json);

Shouldn't is just be callback(json)? If you have a reason for this, you should probably outline it in the Readme, I had to read through the code to figure out why I was getting null results in my callback.

Minor issue with ntwitter stream requests

I am trying to stream location based twitter streams using ntwitter stream api. I have run into an issue where following sample code gives me a 404 error.

The stream url formed is: https://stream.twitter.com/1.1//statuses/filter.json, notice the // before statuses.

Twitter says, Unknown URL. See Twitter Streaming API documentation at http://dev.twitter.com/pages/streaming_api.

When I hard code the url to https://stream.twitter.com/1.1/statuses/filter.json, removing the extra /, everything runs fine.

I am not sure if this is really bug, but wanted to bring it your notice.

Object.keys called on non-object

Using this context:
NTwitter 0.2.7 on NodeJS 0.4.12 on 2.6.35.14-97.fc14.x86_64

NodeJS server crashes on

/usr/local/node/lib/node_modules/ntwitter/lib/twitter.js:204
Object.keys(params).forEach(function(item) {
^
TypeError: Object.keys called on non-object
at Function.keys (native)
at Twitter.stream (/usr/local/node/lib/node_modules/ntwitter/lib/twitter.js:204:10)
at s (**********************)
at Query. (/usr/local/node/lib/node_modules/mysql/lib/client.js:108:11)
at Query.emit (events.js:61:17)
at Query._handlePacket (/usr/local/node/lib/node_modules/mysql/lib/query.js:51:14)
at Client._handlePacket (/usr/local/node/lib/node_modules/mysql/lib/client.js:312:14)
at Parser. (native)
at Parser.emit (events.js:64:17)
at /usr/local/node/lib/node_modules/mysql/lib/parser.js:71:14

I "fixed it" using the old code...
if (Array.isArray(params)) {
params.forEach(function (item) {
params[item] = params[item].join(',');
});
}

Any idea of what's goin' on?

Update status with more than one twitter application

Hi, Sometimes I have the following error: [Error: HTTP Error 401: Unauthorized, API message: {"errors":[{"message":"Could not authenticate you","code":32}]}]
data: '{"errors":[{"message":"Could not authenticate you","code":32}]}',
statusCode: 401 } from time to time (sometimes it works correctly).I use 2 different twitter applications and call updateStatus method turn by turn in both.
I looked at #74, but it doesn't work if I even don't send characters: !'()*.

v0.5.0 updateStatus error

Hi,
When I updated code to v0.5.0 updateStatus function returns :

{"errors":[{"message":"Could not authenticate you","code":32}]}

Reverting back to 0.4 works . I assume it's connected with twitter API 1.1 ?

createList passes null params to utils.merge

The createList method optionally takes a 3rd params object but checks to see if it is actually a callback function. If it is a callback function then params is set null. Later on, this null params value is passed to utils.merge and results in an error.

Update capability in the track list in the stream

I'm using twitter-node for awhile but it discountinue ... So I decided to move my code to your lib :)

It work perfectly but I need feature that worked on twitter-node :p The track update list ie :

var TwitterNode = require('twitter-node').TwitterNode
  , sys         = require('sys')

var twit = new TwitterNode({
...
 track: ['baseball', 'football'],         // sports!
});
...   
// adds to the track array set above
twit.track('foosball');

We that you could update on the fly the track list from the client without needed to detroy the stream :)

"Easy there, Turbo." error occuring

Hi,

I am using ntwitter to stream statuses/filter, tracking only one small bounding box.

The stream can run well during days, sometimes weeks, but from time to time the following error starts to get fired almost continuously:

"Easy there, Turbo. Too many requests recently. Enhance your calm."

I get peaks of CPU consumption when this happens, and moreover data is not retrieved from twitter anymore.

This does not happen when a lot of activity is ongoing on twitter. Also, no 'limit' event is ever triggered.

I use immortal-ntwitter (https://github.com/horixon/immortal-ntwitter) to reconnect properly on errors, but when this one happens the only solution I have is to kill the node-js process and restart it (then this error never fires right back).

Does this problem occur to you guys also? Any suggestion on how it could be fixed?

Thanks,

m

limiting api calls

Hi, we hacked ntwitter a while back to set a hard threshold on the number of calls the module would make (so we could guarantee that we would stay within the api rate limit).

I'm too embarrassed by the code, and it was only done for one or two methods, so I want to ask for an opinion on how to do it correctly. What we ended up doing was modifying Twitter.prototype._getUsingCursor
like so:

var use_limit = params.limit || false
if (data.next_cursor_str === '0') {
callback(null, result);
} else if (use_limit && result.length >= use_limit) {
callback(null, result);
} else {
params.cursor = data.next_cursor_str;
self.get(url, params, fetch);
}

and we had to hack the methods we wanted to be able to add a use_limit key to the params:
Twitter.prototype.getFollowersIds = function(id, callback)

else if(typeof id==='object'){
...
params.limit = id.limit;
}

I'd do a pull request but I'm not convinced our code is clean enough and its a bit out dated now (but working fine otherwise)

Add support for paging

Twitter cannot return more than 500 results in a single call at present, and will pass in a paging URL to get the next set of results. ntwitter needs support to recurse through these and build the whole dataset.

Is this project dead. Add collaborators ?

It has been 5 month since the latest update and the library is not compatible with twitters 1.1 rest api. Plenty of good pull requests.

#95
#88
#79

But nobody is pulling.

So following the history of node-twitter. We are now on the 3 fork of the library.
https://github.com/technoweenie/twitter-node
https://github.com/jdub/node-twitter
https://github.com/AvianFlu/ntwitter
Do we need a 4. fork ?

I think the best option would be to get @AvianFlu to add some collaborator.

incorrect oauth requests for search streams

When I try to start a search stream with track=test,test'ing it fails.

track=test works fine
track=test,test'ing => 401 error

The response I receive is a 401 and I suspect that when POST 'ing the oauth is not done correctly.

Please let me know what extra information I can provide to further debug this issue?

Question on API limits for users/lookup

Question related to the API limits, It might something I'm just missing, not sure though:

If I do this:

     twit.showUser(ids, function(error, response) {
       console.log(response)
     }

Where {ids} is an Array, with length < 100, all is well.

When I do the same and IDs is > 100, it fails.

This is based on:
https://dev.twitter.com/docs/api/1.1/get/users/lookup

And specifically:

for up to 100 users per request

Is this somehow managed in ntwitter or do I need to manage this outside? if so, any recommendation on how to manage that?

showUser doesn't differentiate between screen_names and ids correctly

There's no real way to do this actually, since just numbers is also a valid twitter username: http://twitter.com/1337

Perhaps a change that lets you specify if you're using id's or screen_names?

The other fix, if you still wanted to remain optimistic that most usernames aren't numbers (or don't start with numbers), would be to use a regex to check if it's a line instead of parseInt, because parseInt('20string') == 20.

https://github.com/AvianFlu/ntwitter/blob/master/lib/twitter.js#L582

Restructure the library

I Worked with this library succesfully for a couple of projects. It has a nice API and the most important thing is that it works! I think is time for a restructuration and i want to contribute. We probably need to split the unique twitter.js file into several modules. I want to make a util.js file, divide the REST and streaming API and maybe a search api module. Maybe you want to make some search API calls without an API key.

This is just the beginning of a deep change, and i think it worth it. Would you?

remove console.log calls wrapping console.dir

I copied the code for the user streams using "track":

stream.on('data', function (data) {
    console.log(console.dir(data));
});

Besides the tweets this code also outputs some "undefined"s on my console.

I'm using node 0.6.6

Edit: I'm sorry for confusing you. The cause is of course that console.dir already outputs to console and returns undefined.

You should remove all occurrences of console.log(console.dir(foo)); with console.dir(foo); in the example codes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.