Skip to content

Instantly share code, notes, and snippets.

@from1to9
Created April 9, 2012 13:51
Show Gist options
  • Save from1to9/2343549 to your computer and use it in GitHub Desktop.
Save from1to9/2343549 to your computer and use it in GitHub Desktop.
Fanfou API for node.js
/*
code by from1to9
http://lyy.me
this code requires node-oauth at: https://github.com/ciaranj/node-oauth
useage:
callback could be a object which includes two functions, like
callback = { success : function() { //your code },
failed : function() { //your code }
}
you can also use a function for callback, it will run for "success".
example:
var fanfouOAuth = require('./fanfou').fanfouOAuth;
var consumer_key = 'xxx';
var consumer_secret = 'xxx';
var access_token = 'xxx';
var access_token_secret = 'xxx';
fanfou = new fanfouOAuth( access_token, access_token_secret, consumer_key, consumer_secret);
//show user details
fanfou.users_show("from1to9",
function(data) {
console.log(data);
});
//send a message
fanfou.update("from1to9");
//send a msg and direct_messages
fanfou.update("hello world").direct_messages_new("from1to9", "hello!");
//getOAuthRequestToken
fanfou.getOAuthRequestToken({
success: function (oauth_token, oauth_token_secret, verification_url, results) {
console.log('oauth_token: ' + oauth_token);
console.log('oauth_token_secret: ' + oauth_token_secret);
console.log('verify_url: ' + verification_url);
},
failed: function (e) {
console.log(e);
}
});
//getOAuthAccessToken
fanfou.getOAuthAccessToken( oauth_token, oauth_token_secret, {
success: function (access_token, access_token_secret, results) {
console.log('access_token: ' + access_token);
console.log('access_token_secret: ' + access_token_secret);
},
failed: function (e) {
console.log(e);
}
});
funcitons:
fanfouOAuth.getOAuthRequestToken ( function(oauth_token, oauth_token_secret, verification_url){} )
fanfouOAuth.update ( message, function(data){})
fanfouOAuth.reply_to ( message, in_reply_to_status_id, in_reply_to_user_id, funcitons(data){})
fanfouOAuth.repost
fanfouOAuth.replies
fanfouOAuth.mentions
fanfouOAuth.direct_messages_new
fanfouOAuth.friends_ids
fanfouOAuth.followers_ids
fanfouOAuth.users_show
other functions could call fanfouOAuth._call ( action, params, callback_function(){} )
if you need to use CALLBACK_URL for authorize, please change the code
*/
var OAuth = require('oauth').OAuth;
const ACCESS_TOKEN_URL = 'http://fanfou.com/oauth/access_token';
const AUTHORIZE_URL = 'http://fanfou.com/oauth/authorize';
const REQUEST_TOKEN_URL = 'http://fanfou.com/oauth/request_token';
const API_URL_BASE = 'http://api.fanfou.com/';
//change CALLBACK_URL here.
const CALLBACK_URL = '';
var api = {
"public_timeline": {"url": "statuses/public_timeline", "method": "GET"},
"friends_timeline": {"url": "statuses/friends_timeline", "method": "GET"},
"replies": {"url": "statuses/replies", "method": "GET"},
"mentions": {"url": "statuses/mentions", "method": "GET"},
"show": {"url": "statuses/show", "method": "GET"},
"user_timeline": {"url": "statuses/user_timeline", "method": "GET"},
"update": {"url": "statuses/update", "method": "POST"},
"destroy": {"url": "statuses/destroy", "method": "POST"},
"statuses_friends": {"url": "statuses/friends", "method": "POST"},
"statuses_followers": {"url": "statuses/followers", "method": "POST"},
//photo
"photo_upload": {"url": "photos/upload", "method": "POST"},
//private msg
"direct_messages_sent": {"url": "direct_messages/sent", "method": "GET"},
"direct_messages_inbox": {"url": "direct_messages/inbox", "method": "GET"},
"direct_messages_new": {"url": "direct_messages/new", "method": "POST"},
"direct_messages_destroy": {"url": "direct_messages/destroy", "method": "POST"},
//friends
"friendships_create": {"url": "friendships/create", "method": "POST"},
"friendships_destroy": {"url": "friendships/destroy", "method": "POST"},
"friendships_exists": {"url": "friendships/exists", "method": "GET"},
"friends_ids": {"url": "friends/ids", "method": "GET"},
"followers_ids": {"url": "followers/ids", "method": "GET"},
//user
"users_show": {"url": "users/show", "method": "GET"},
"users_followers": {"url": "users/followers", "method": "GET"},
"users_friends": {"url": "users/friends", "method": "GET"},
//notification
"notifications_follow": {"url": "notifications/follow", "method": "POST"},
"notifications_leave": {"url": "notifications/leave", "method": "POST"}
};
function _checkarg(arg) {
var cb = { success : function() {},
failed : function() {}
};
if (typeof arg === 'function') {
cb.success = arg;
}
else if (typeof arg === 'object'){
if (typeof arg.success === 'function') {
cb.success = arg.success;
}
if (typeof arg.failed === 'function') {
cb.failed = arg.failed;
}
}
return cb;
};
exports.fanfouOAuth = function ( access_token, access_token_secret, consumer_key, consumer_secret ) {
this._consumer_key = consumer_key;
this._consumer_secret = consumer_secret;
this._access_token = access_token;
this._access_token_secret = access_token_secret;
};
exports.fanfouOAuth.prototype._getAPI = function (action) {
if ( api[action] !== undefined ) {
return { "url" : API_URL_BASE + api[action]["url"] + ".json", "method" : api[action]["method"] };
}
else {
return { "url" : "", "method" : "" };
}
};
exports.fanfouOAuth.prototype._call = function (action, params, cb) {
var api = this._getAPI(action);
var arg = arguments[arguments.length - 1];
callback = _checkarg(arg);
if ( api["method"] == "GET" ) {
var query_str = '';
for ( var key in params) {
query_str += key + "=" + params[key] + "&";
}
var req_url = api["url"] + "?" + query_str.slice(0, -1);
}
else {
var req_url = api["url"];
}
this._params = params;
this._method = api["method"];
if (this._method === undefined) {
this._method = "POST";
}
if (req_url === undefined) {
return false;
}
this.oa = new OAuth(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL,
this._consumer_key, this._consumer_secret,
"1.0", CALLBACK_URL, "HMAC-SHA1" );
if (this._method == "POST") {
this.oa.post(req_url, this._access_token, this._access_token_secret, this._params, function(error, data, resp){
if (error) {
callback.failed(error);
}
else {
callback.success(data);
}
});
}
else if (this._method == "GET") {
this.oa.get(req_url, this._access_token, this._access_token_secret, function(error, data, resp){
if (error) {
callback.failed(error);
}
else {
callback.success(data);
}
});
}
else {
return false;
}
return this;
};
exports.fanfouOAuth.prototype.update = function (message, callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
// if (source == undefined || source == '') {
// var params = {
// "status": message
// };
// }
// else {
var params = {
"status": message //,
// "source": source
};
// }
return this._call("update", params, _callback);
};
exports.fanfouOAuth.prototype.reply_to = function ( message, in_reply_to_status_id, in_reply_to_user_id, callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
//if (source == undefined)
// source = "";
var params = {
"status": message,
//"source": source,
"in_reply_to_status_id": in_reply_to_status_id,
"in_reply_to_user_id": in_reply_to_user_id
};
return this._call("update", params, _callback);
};
exports.fanfouOAuth.prototype.repost = function (message, repost_status_id, callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
//if (source == undefined)
// source = "";
var params = {
"status": message,
//"source": source,
"repost_status_id": repost_status_id
};
return this._call("update", params, _callback);
};
//format= html; mode= default/lite
exports.fanfouOAuth.prototype.replies = function (since_id, page, mode, format, count, callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
var params = {};
if (format != undefined && format == "html")
params["format"] = "html";
if ( since_id != undefined )
params["since_id"] = since_id;
if ( typeof page == "number" )
params["page"] = page;
if ( typeof count == "number" )
params["count"] = count;
if ( mode != undefined ) {
params["mode"] = mode;
}
else {
params["mode"] = "lite";
}
return this._call("replies", params, _callback);
};
//format= html; mode= default/lite
exports.fanfouOAuth.prototype.mentions = function (since_id, count, page, mode, format, callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
var params = {};
if (format != undefined && format == "html")
params["format"] = "html";
if ( since_id != undefined )
params["since_id"] = since_id;
if ( typeof page == "number" )
params["page"] = page;
if ( typeof count == "number" )
params["count"] = count;
if ( mode != undefined ) {
params["mode"] = mode;
}
else {
params["mode"] = "lite";
}
return this._call("mentions", params, _callback);
};
//format= html; mode= default/lite
exports.fanfouOAuth.prototype.direct_messages_new = function (user, text, in_reply_to_id, callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
var params = {
"user": user,
"text": text,
"in_reply_to_id": in_reply_to_id,
"mode": "lite"
};
return this._call("direct_messages_new", params, _callback);
};
exports.fanfouOAuth.prototype.friends_ids = function (id, page, count, callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
if (typeof page != "number")
page = 1;
if (typeof count != "number")
count = 60;
var params = {
"id": id,
"page": page,
"count": count
};
return this._call("friends_ids", params, _callback);
};
exports.fanfouOAuth.prototype.followers_ids = function (id, page, count, callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
if (typeof page != "number")
page = 1;
if (typeof count != "number")
count = 60;
var params = {
"id": id,
"page": page,
"count": count
};
return this._call("followers_ids", params, _callback);
};
exports.fanfouOAuth.prototype.users_show = function (id, callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
if (typeof id == "string") {
var params = {
"id": id,
"mode": "lite"
};
}
else {
var params = {
"mode": "lite"
};
}
return this._call("users_show", params, _callback);
};
exports.fanfouOAuth.prototype.getOAuthRequestToken = function (callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
this.oa = new OAuth(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL,
this._consumer_key, this._consumer_secret,
"1.0", CALLBACK_URL, "HMAC-SHA1" );
this.oa.getOAuthRequestToken( function(error, oauth_token, oauth_token_secret, results) {
if (error) {
_callback.failed(error);
}
else {
verification_url = AUTHORIZE_URL + '?oauth_token=' + oauth_token;
if (CALLBACK_URL != '') {
verification_url += "&callback=" + CALLBACK_URL;
}
_callback.success(oauth_token, oauth_token_secret, verification_url, results);
}
});
return this;
};
exports.fanfouOAuth.prototype.getOAuthAccessToken = function (oauth_token, oauth_token_secret, callback) {
var arg = arguments[arguments.length - 1];
_callback = _checkarg(arg);
this.oa = new OAuth(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL,
this._consumer_key, this._consumer_secret,
"1.0", CALLBACK_URL, "HMAC-SHA1" );
this.oa.getOAuthAccessToken(oauth_token, oauth_token_secret, oauth_token, function(error, access_token, access_token_secret, results) {
if (error) {
_callback.failed(error);
}
else {
_callback.success(access_token, access_token_secret, results);
}
});
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment