Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created November 6, 2012 12:40
Show Gist options
  • Save ishiduca/4024388 to your computer and use it in GitHub Desktop.
Save ishiduca/4024388 to your computer and use it in GitHub Desktop.
FriendFeed-Realtime - inspired from AnyEvent::FriendFeed::Realtime

FriendFeed-Realtime

subscribe to FriendFeed Real-time API. inspired from AnyEvent::FriendFeed::Realtime

Usage

var FriendFeedRealtime = require('friendfeed-realtime');

var client = new FriendFeedRealtime({
    username:   'your_username'   // optional
  , remote_key: 'your_remote_key' // optional https://friendfeed.com/account/api
  , request:    '/feed/home'      // or "/feed/NICKNAME/friends", "/search?q=friendfeed"
  , on_error:   function (err) {
        console.error(err);
        process.exit(1);
    }
  , on_entry: function (entry) {
        console.log("%s: %s", entry.from.name, entry.body);
    }
});

SeeAlso

var FriendFeedRealtime = require('friendfeed-realtime');
var util = require('util');
var fs = require('fs');
var config_json = './config.json';
var config = JSON.parse(fs.readFileSync(config_json, 'utf8'));
config.request = '/feed/home';
config.on_entry = function (entry) {
util.log(entry.from.name + ": " + entry.body);
};
var client = new FriendFeedRealtime(config);
var http = require('http');
var querystring = require('querystring');
function FriendFeedRealtime (args) {
args || (args = {});
var that = this;
('username remote_key request on_entry on_error').split(' ')
.forEach(function (key) {
(key in args) && (that[key] = args[key]);
});
this.request || (this.request = '/feed/home');
this.on_error || (this.on_error = function (err) {
console.error(err); process.exit(1);
});
this.on_entry || (this.on_entry = function (res) {
console.log("%s: %s", res.from.name, res.body);
});
this.long_poll({updates: 1});
}
(function (fp) {
fp.api = function (opt) {
var _api = {
port: 80
, host: 'friendfeed-api.com'
, path: [ ('/v2/updates' + this.request)
, querystring.stringify(opt) ].join('?')
};
this.username && this.remote_key &&
(_api.auth = [ this.username, this.remote_key ].join(':'));
// if (this['set-cookies']) {
// _api.headers['cookie'] = this['set-cookies'];
// }
return _api;
};
fp.long_poll = function (opt) {
var that = this;
var _data = '';
var req = http.get(this.api(opt), function (res) {
req = null;
/^2/.test(res.statusCode) ||
that.on_error({message: "response status: "+ res.statusCode});
// if (res.headers['set-cookie']) {
// that['set-cookie'] = res.headers['set-cookie'];
// }
res.setEncoding('utf8');
res.on('data', function (chunk) { _data += chunk });
res.on('end', function () {
var data;
try {
data = JSON.parse(_data);
} catch (e) { that.on_error(e) }
data.errorCode && that.on_error(data.errorCode);
data.entries.forEach(function (entry) {
that.on_entry(entry);
});
data.realtime && that.long_poll({cursor: data.realtime.cursor});
});
});
req.on('error', this.on_error);
req.end();
};
})(FriendFeedRealtime.prototype);
module.exports = FriendFeedRealtime;
module.exports.version = '0.0.0-alpha';
{
"name": "friendfeed-realtime",
"version": "0.0.0-alpha",
"description": "subscribe to Friendfeed Real-time API. (inspired from AnyEvent::FriendFeed::Realtime)",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"friendfeed",
"long-poll"
],
"author": "[email protected]",
"license": "BSD"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment