Skip to content

Instantly share code, notes, and snippets.

@xyn9
Last active December 22, 2015 23:09
Show Gist options
  • Save xyn9/6544661 to your computer and use it in GitHub Desktop.
Save xyn9/6544661 to your computer and use it in GitHub Desktop.
tumblr API 簡易ライブラリ
// tumblr.js
//
// ==UserScript==
// @name simple tumblr api
// @version 0.91
// @include
// @require /oauth/sha1.js
// @require /oauth/oauth.js
// @description tumblr API 簡易ライブラリ
// @homepage http://gist.github.com/xyn9
// @author xyn9 <xyn9.mail+github@gmail.com>
// @lisence (CC) Attribution Share Alike; http://creativecommons.org/licenses/by-sa/2.1/jp
// ==UserScript==
// ------------------------------------------------------------
function tumblr(_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.tumblr.com/v2'
, auth_url: 'http://www.tumblr.com'
,
id: _PROFILE.id
, key: _PROFILE.api_key
, skey: _PROFILE.api_skey
, akeys: {token: _PROFILE.token, secret: _PROFILE.secret}
};
// ------------------------------------------------------------
//
this._get_url
= function get_url(_options){
return (
/^oauth/.test(_options.action)
? ($API.auth_url +'/'+ _options.action)
: (
$API.url
+ '/'
+ _options.action
.replace(/@([_a-z]+)/g, (function (_$0,_$1){ return $API[_$1]; }))
.replace(/#+/, ((_options.blog_id ? _options.blog_id : $API.id)+'.tumblr.com'))
)
);
}
//
this._get_oa_header
= function get_oa_header(_options){
//
_options.parameters.oauth_consumer_key = $API.key;
_options.parameters.oauth_token = $API.akeys.token ? $API.akeys.token : null;
//
OAuth.completeRequest(_options, {
oauth_signature_method: 'HMAC-SHA1'
, consumerSecret: $API.skey
, tokenSecret: ($API.akeys.secret ? $API.akeys.secret : null)
});
return OAuth.getAuthorizationHeader('', _options.parameters);
};
// ------------------------------------------------------------
function _session(_options){
//
var url = get_url(_options);
_options.action = OAuth.addToURL(url, _options.options);
//
if( (/^GET/i).test(_options.method) ){
url = _options.action;
delete _options.options;
}
//
$http.open(_options.method, url, false);
//
if(_options.auth || (! /[\?&]?api_key=[^&]+/.test(url))){
$http.setRequestHeader('Authorization', get_oa_header(_options));
}
if(_options.options && (/^POST/i).test(_options.method)){
$http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
_options.options = _options.action.replace(/^[^\?]+\?/,'');
}
//
$http.send(_options.options ? _options.options : null);
//
return $http;
};
//
function session(_options){
return (
_this_._session
? _this_._session(_options, _this_)
: _session(_options)
);
}
// ------------------------------------------------------------
//
this.blog = {
//
// ------------------------------------------------------------
info: function (_blog_id){
return session({
method: 'GET'
, action: 'blog/#/info?api_key=@key'
, parameters: {}
, blog_id: _blog_id
});
}
,
// ------------------------------------------------------------
posts: function (_blog_id, _options, _type){
return session({
method: 'GET'
, action: (
'blog/#/posts'+ (_type ? ('/'+_type) : '')
+ (/^(draft|queue)$/.test(_type) ? '' : '?api_key=@key')
)
, parameters: {}
, options: _options
, blog_id: _blog_id
});
}
,
// ------------------------------------------------------------
//
'_post': function (_blog_id, _options){
return session({
method: 'POST'
, action: 'blog/#/post'
, parameters: {}
, options: _options
, blog_id: _blog_id
});
}
,
//
'_edit': function (_blog_id, _options){
return session({
method: 'POST'
, action: 'blog/#/post/edit'
, parameters: {}
, options: _options
, blog_id: _blog_id
});
}
,
// ------------------------------------------------------------
//
'_delete': function (_blog_id, _options){
return session({
method: 'POST'
, action: 'blog/#/post/delete'
, parameters: {}
, options: _options
, blog_id: _blog_id
});
}
// ,
};
// ------------------------------------------------------------
//
this.user = {
//
};
// ------------------------------------------------------------
//
this.tagged = {
//
};
// ------------------------------------------------------------
//this.get_token = function (){ return ($API.url +'/console'); };
this.get_token = function (_verifier){
//
function parse_token(__action, __params){
return /^[23]0\d$/.test(
session({
method: 'POST'
, action: ('oauth/'+ __action)
, parameters: __params
}).status
);
}
//
if( _verifier ){
$API.akeys = {
token: _verifier.oauth_token
, secret: _verifier.oauth_token_secret
};
delete _verifier.oauth_token;
delete _verifier.oauth_token_secret;
return (
parse_token('access_token', _verifier)
? $http.responseText
: 0
);
}
//
return (
parse_token('request_token', {})
? ($API.auth_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