Skip to content

Instantly share code, notes, and snippets.

@xyn9
Created October 16, 2011 18:58
Show Gist options
  • Save xyn9/1291265 to your computer and use it in GitHub Desktop.
Save xyn9/1291265 to your computer and use it in GitHub Desktop.
はてなハイク API 簡易ライブラリ
// haiku.js
//
// ==UserScript==
// @name simple hatena haiku api
// @version 0.92
// @include
// @require /oauth/oauth.js
// @require /oauth/sha1.js
// @description はてなハイク API 簡易ライブラリ
// @homepage http://xyn9.github.com/gist/
// @author xyn9 <xyn9.mail@gmail.com>
// @lisence (CC) Attribution Share Alike; http://creativecommons.org/licenses/by-sa/2.1/jp
// ==UserScript==
// ------------------------------------------------------------
function haiku(_PROFILE){
/*
[input]
PROFILE = {
id: (account id)
, api_key: (api key)
, api_skey: (api secret)
, token: (access token)
, secret: (token secret)
};
ref: http://developer.hatena.ne.jp/ja/documents/auth/apis/oauth/consumer
*/
//
var _this_ = this;
//
var $http = new ActiveXObject('Microsoft.XMLHTTP');
//
var $API = {
url: 'http://h.hatena.ne.jp/api'
, token_url: 'https://www.hatena.com'
, oa_url: 'https://www.hatena.ne.jp'
,
id: _PROFILE.id
, key: _PROFILE.api_key
, skey: _PROFILE.api_skey
, akeys: {token: _PROFILE.token, secret: _PROFILE.secret}
, scope: 'read_public,write_public'
};
// ------------------------------------------------------------
function session(_options, _fmt){
//
_options.action = OAuth.addToURL(
(
/^oauth/.test(_options.action)
? ($API.token_url +'/'+ _options.action)
: ($API.url +'/'+ _options.action +'.'+ (_fmt ? _fmt : 'xml'))
)
, _options.parameters
);
_options.parameters = {oauth_consumer_key: $API.key};
//
var sign_param = {
oauth_signature_method: 'HMAC-SHA1'
, consumerSecret: $API.skey
};
if( $API.akeys.token ){ _options.parameters.oauth_token = $API.akeys.token; }
if( $API.akeys.secret ){ sign_param.tokenSecret = $API.akeys.secret; }
//
OAuth.completeRequest(_options, sign_param);
//
with( $http ){
open(_options.method, _options.action, false);
setRequestHeader('Authorization', OAuth.getAuthorizationHeader('', _options.parameters));
send(null);
}
//
return $http;
};
// ------------------------------------------------------------
this.show = function (_id, _fmt){
return session({
method: 'GET'
, action: ('statuses/show/'+ _id)
, parameters: {}
}, _fmt);
};
// ------------------------------------------------------------
this.update = function (_stat, _opt, _fmt){
return session({
method: 'POST'
, action: 'statuses/update'
, parameters: {
'status': _stat
, 'keyword': _opt.keyword
, 'in_reply_to_status_id': _opt.reply
}
}, _fmt);
};
//
this.destroy = function (_id, _fmt){
return session({
method: 'POST'
, action: ('statuses/destroy/'+ _id)
, parameters: {}
}, _fmt);
};
//
this.favorite = function (_id, _del, _fmt){
return session({
method: 'POST'
, action: ['favorites', (_del ? 'destroy' : 'create'), _id].join('/')
, parameters: {}
}, _fmt);
};
// ------------------------------------------------------------
this.keywords = {
//
show: function (_keyword, _rel, _fmt){
return session({
method: 'GET'
, action: 'statuses/keywords'
, parameters: {
'word': _keyword
, 'without_related_keywords': (_rel ? 0 : 1)
}
}, _fmt);
}
,
//
create: function (_keyword, _fmt, _del){
return session({
method: 'POST'
, action: ('keywords/'+ (_del ? 'destroy' : 'create'))
, parameters: {
'word': _keyword
, 'without_related_keywords': 1
}
}, _fmt);
}
,
//
destroy: function (_keyword, _fmt){
return _this_.keywords.create(_keyword, _fmt, true);
}
};
// ------------------------------------------------------------
this.get_token = function (_verify){
//
function parse_token(__action, __params){
return (
(
/^[23]0\d$/.test(
session({method:'POST', action: ('oauth/'+ __action), parameters: __params}, 'auth')
.status
)
&& (/oauth_token=([^&]+)&oauth_token_secret=([^&]+)/i).test($http.responseText)
)
? {'token':decodeURIComponent(RegExp.$1), 'secret':decodeURIComponent(RegExp.$2)}
: 0
);
}
//
return (
_verify
? (
($API.akeys = parse_token('token', {'oauth_verifier':_verify}))
? $API.akeys
: $http.getResponseHeader('WWW-Authenticate')
)
: (
($API.akeys = parse_token('initiate', {'scope':$API.scope, 'oauth_callback':'oob'}))
? ($API.oa_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