danbooru
Version:
danbooru api wrapper
129 lines (128 loc) • 4.74 kB
JavaScript
// Generated by LiveScript 1.4.0
(function(){
var https, request, deepExtend, argsJs, search, baseUrl, exports, slice$ = [].slice;
https = require('https');
request = require('request');
deepExtend = require('deep-extend');
argsJs = require('args-js');
search = require('./search');
baseUrl = 'https://danbooru.donmai.us/';
module.exports = function(it){
return importAll$(it, it());
}(
exports = (function(){
exports.displayName = 'exports';
var danbooruErrors, parsePath, doRequest, optionalArgs, prototype = exports.prototype, constructor = exports;
import$(prototype, search);
function exports(params, key){
var this$ = this instanceof ctor$ ? this : new ctor$;
params == null && (params = {});
if (typeof params === 'string') {
this$.defaultParameters = {
login: params
};
if (key != null && typeof key === 'string') {
this$.defaultParameters.api_key = key;
}
} else {
this$.defaultParameters = deepExtend({}, params);
}
return this$;
} function ctor$(){} ctor$.prototype = prototype;
danbooruErrors = {
204: '204 No Content: Request was successful',
403: '403 Forbidden: Access denied',
404: '404 Not Found: Not found',
420: '420 Invalid Record: Record could not be saved',
421: '421 User Throttled: User is throttled, try again later',
422: '422 Locked: The resource is locked and cannot be modified',
423: '423 Already Exists: Resource already exists',
424: '424 Invalid Parameters: The given parameters were invalid',
500: '500 Internal Server Error: Some unknown error occurred on the server',
503: '503 Service Unavailable: Server cannot currently handle the request, try again later'
};
parsePath = function(it){
return baseUrl + "" + (/^\/?(.*?)(?:\.(?:json|xml)|)$/.exec(it))[1] + ".json";
};
doRequest = function(self, method, body, path, params, callback){
(function(){
var data, dataName, uri, ref$;
data = deepExtend({}, this.defaultParameters, params);
dataName = body ? 'form' : 'qs';
uri = parsePath(path);
request((ref$ = {
uri: uri,
method: method
}, ref$[dataName] = data, ref$.json = true, ref$), function(e, response, body){
if (response.statusCode !== 200) {
e == null && (e = new Error(danbooruErrors[response.statusCode]));
}
return callback(e, body);
});
}.call(self));
};
optionalArgs = function(){
var ref$, path, params, callback;
ref$ = argsJs([
{
path: argsJs.STRING | argsJs.Optional,
_default: ''
}, {
params: argsJs.OBJECT | argsJs.Optional,
_default: {}
}, {
callback: argsJs.FUNCTION | argsJs.Optional,
_default: function(){}
}
], arguments), path = ref$.path, params = ref$.params, callback = ref$.callback;
return [path, params, callback];
};
prototype.get = function(path, params, callback){
return doRequest.apply(null, [this, 'GET', false].concat(slice$.call(optionalArgs.apply(this, arguments))));
};
prototype.post = function(path, params, callback){
return doRequest.apply(null, [this, 'POST', true].concat(slice$.call(optionalArgs.apply(this, arguments))));
};
prototype.put = function(path, params, callback){
return doRequest.apply(null, [this, 'PUT', true].concat(slice$.call(optionalArgs.apply(this, arguments))));
};
prototype['delete'] = function(path, params, callback){
return doRequest.apply(null, [this, 'DELETE', true].concat(slice$.call(optionalArgs.apply(this, arguments))));
};
prototype.request = function(options, callback){
callback == null && (callback = function(){});
options = (function(){
switch (typeof options) {
case 'object':
return deepExtend({}, options);
case 'string':
return {
uri: options
};
case 'function':
callback = options;
// fallthrough
default:
return {
uri: ''
};
}
}());
options.baseUrl = baseUrl;
if (options.url != null && options.uri == null) {
options.uri = options.url;
}
return request(options, callback);
};
return exports;
}()));
function importAll$(obj, src){
for (var key in src) obj[key] = src[key];
return obj;
}
function import$(obj, src){
var own = {}.hasOwnProperty;
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
return obj;
}
}).call(this);