Created
October 16, 2011 19:15
-
-
Save xyn9/1291290 to your computer and use it in GitHub Desktop.
twitter api 簡易ライブラリ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// twitter.js | |
// | |
// ==UserScript== | |
// @name simple twitter api | |
// @version 0.94 | |
// @include | |
// @require /oauth/oauth.js | |
// @require /oauth/sha1.js | |
// @description twitter api 簡易ライブラリ | |
// @homepage http://gist.github.com/xyn9 | |
// @author xyn9 <xyn9.mail@gmail.com> | |
// @lisence (CC) Attribution Share Alike; http://creativecommons.org/licenses/by-sa/2.1/jp | |
// ==UserScript== | |
// ------------------------------------------------------------ | |
function twitter(_PROFILE){ | |
/* | |
[input] | |
PROFILE = { | |
id: (account id) | |
, api_key: (api key) | |
, api_skey: (api secret) | |
, token: (access token) | |
, secret: (token secret) | |
}; | |
ref: | |
*/ | |
// | |
var _this_ = this; | |
// | |
var $http = new ActiveXObject('Microsoft.XMLHTTP'); | |
// | |
var $API = { | |
url: 'https://api.twitter.com' | |
, version: '1.1' | |
, | |
id: _PROFILE.id | |
, key: _PROFILE.api_key | |
, skey: _PROFILE.api_skey | |
, akeys: {token: _PROFILE.token, secret: _PROFILE.secret} | |
}; | |
// ------------------------------------------------------------ | |
function session(_options){ | |
// | |
_options.action = OAuth.addToURL( | |
( | |
$API.url +'/'+ ( | |
/^oauth/.test(_options.action) | |
? _options.action | |
: (_options.action.replace(/^#+/,$API.version) +'.json') | |
) | |
) | |
, _options.parameters | |
); | |
_options.parameters = { | |
oauth_consumer_key: $API.key | |
, oauth_token: $API.akeys.token | |
}; | |
// | |
OAuth.completeRequest(_options, { | |
oauth_signature_method: 'HMAC-SHA1' | |
, consumerSecret: $API.skey | |
, tokenSecret: $API.akeys.secret | |
}); | |
// | |
with( $http ){ | |
open(_options.method, _options.action, false); | |
setRequestHeader('Authorization', OAuth.getAuthorizationHeader('', _options.parameters)); | |
send(null); | |
} | |
// | |
return $http; | |
}; | |
// ------------------------------------------------------------ | |
this.show = function (_id, _detail){ | |
return session({ | |
method: 'GET' | |
, action: ('#/statuses/show/'+ _id) | |
, parameters: {'include_rts':1, 'trim_user':(! _detail)} | |
}); | |
}; | |
// ------------------------------------------------------------ | |
this.update = function (_stat, _opt){ | |
return session({ | |
method: 'POST' | |
, action: '#/statuses/update' | |
, parameters: { | |
'status': _stat | |
, 'in_reply_to_status_id': _opt.reply | |
, 'trim_user': (! _opt.user) | |
} | |
}); | |
}; | |
// | |
this.destroy = function (_id){ | |
return session({ | |
method: 'POST' | |
, action: ('#/statuses/destroy/'+ _id) | |
, parameters: {'trim_user': 1} | |
}); | |
}; | |
// | |
this.retweet = function (_id){ | |
return session({ | |
method: 'POST' | |
, action: ('#/statuses/retweet/'+ _id) | |
, parameters: {'trim_user': 1} | |
}); | |
}; | |
// | |
this.favorite = function (_id, _del){ | |
return session({ | |
method: 'POST' | |
, action: ('#/favorites/'+ (_del ? 'destroy' : 'create')) | |
, parameters: {'id': _id} | |
}); | |
}; | |
// ------------------------------------------------------------ | |
// | |
this.follow = function (_user, _del){ | |
return session({ | |
method: 'POST' | |
, action: ('#/friendships/'+ (_del ? 'destroy' : 'create')) | |
, parameters: {'screen_name': _user} | |
}); | |
}; | |
// | |
this.report_spam = function (_user){ | |
return session({ | |
method: 'POST' | |
, action: '#/users/report_spam' | |
, parameters: {'screen_name': _user} | |
}); | |
}; | |
// ------------------------------------------------------------ | |
// list | |
// | |
this.lists = { | |
// | |
show: function (_list, _opt, _method){ | |
return session({ | |
method: 'GET' | |
, action: ('#/lists/'+ (_method ? _method : 'statuses')) | |
, parameters: { | |
'owner_screen_name':$API.id | |
, 'slug': _list | |
, 'cursor': (_opt ? _opt.cursor : -1) | |
, 'count': (_opt ? _opt.count : 200) | |
} | |
}); | |
} | |
, | |
// | |
create: function (_list, _opt){ | |
return session({ | |
method: 'POST' | |
, action: '#/lists/create' | |
, parameters: { | |
'name': _list | |
, 'mode': _opt.mode | |
, 'decription': _opt.decription | |
} | |
}); | |
} | |
, | |
// | |
member: function (_list, _user, _del){ | |
return session({ | |
method: 'POST' | |
, action: ('#/lists/members/'+ (_del ? 'destroy' : 'create')) | |
, parameters: { | |
'owner_screen_name': $API.id | |
, 'slug': _list | |
, 'screen_name': _user | |
} | |
}) | |
} | |
// | |
}; | |
// | |
// ------------------------------------------------------------ | |
this.get_token = function (_pin){ | |
// | |
function parse_token(__action, __params){ | |
return ( | |
( | |
/^[23]0\d$/.test( | |
session({method:'GET', action: ('oauth/'+ __action), parameters: __params}) | |
.status | |
) | |
&& (/_token=([^=&]+)&\w+_secret=([^=&]+)/i).test($http.responseText) | |
) | |
? {token:RegExp.$1, secret:RegExp.$2} | |
: 0 | |
); | |
} | |
// | |
return ( | |
_pin | |
? ( | |
($API.akeys = parse_token('access_token', {'oauth_verifier': _pin})) | |
? $API.akeys | |
: $http.getResponseHeader('WWW-Authenticate') | |
) | |
: ( | |
($API.akeys = parse_token('request_token', {})) | |
? ($API.url +'/oauth/authorize?'+ $http.responseText) | |
: 0 | |
) | |
); | |
// | |
}; | |
// | |
return _this_; | |
// | |
} | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment