SHARE
TWEET

Untitled

a guest Apr 26th, 2018 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var sys    = require('sys'),
  2.     http   = require('http'),
  3.     ws     = require("./vendor/ws"),    
  4.     base64 = require('./vendor/base64'),
  5.     arrays = require('./vendor/arrays');
  6.  
  7. // Command line args
  8. var USERNAME = process.ARGV[2];
  9. var PASSWORD = process.ARGV[3];
  10. var KEYWORD  = process.ARGV[4] || "iphone";
  11.      
  12. if (!USERNAME || !PASSWORD)
  13.   return sys.puts("Usage: node server.js <twitter_username> <twitter_password> <keyword>");
  14.  
  15. // Authentication Headers for Twitter
  16. var headers = [];
  17. var auth = base64.encode(USERNAME + ':' + PASSWORD);
  18. headers['Authorization'] = "Basic " + auth;
  19. headers['Host'] = "stream.twitter.com";
  20.  
  21. var clients = [];
  22.  
  23. // Connection to Twitter's streaming API
  24. var twitter = http.createClient(80, "stream.twitter.com");
  25. var request = twitter.request("GET", "/1/statuses/filter.json?track=" + KEYWORD, headers);
  26. request.finish(function (response) {
  27.   response.setBodyEncoding("utf8");
  28.   response.addListener("body", function (chunk) {
  29.     // Send response to all connected clients
  30.     clients.each(function(c) {
  31.       c.send(chunk);
  32.     });
  33.   });
  34. });
  35.  
  36. // Websocket TCP server
  37. ws.createServer(function (websocket) {
  38.   clients.push(websocket);
  39.  
  40.   websocket.addListener("connect", function (resource) {
  41.     // emitted after handshake
  42.     sys.debug("connect: " + resource);
  43.   }).addListener("close", function () {
  44.     // emitted when server or client closes connection
  45.     clients.remove(websocket);
  46.     sys.debug("close");
  47.   });
  48. }).listen(8080);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top