fix gifbot bugs
This commit is contained in:
parent
598da3405d
commit
c1938a3fb5
1 changed files with 52 additions and 28 deletions
|
@ -1,11 +1,27 @@
|
||||||
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
|
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
|
||||||
|
var path = require('path');
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
var slackbot;
|
var slackbot;
|
||||||
var imgur;
|
var imgur;
|
||||||
|
|
||||||
var cache = {};
|
var cache = {};
|
||||||
var cache_timeout = 3600; // 1h
|
var cache_timeout = 3600; // 1h
|
||||||
var output_dir = 'files';
|
var output_dir = path.resolve(path.dirname(require.main.filename), 'files');
|
||||||
|
|
||||||
|
//function will check if a directory exists, and create it if it doesn't
|
||||||
|
function checkDirectory(directory, callback) {
|
||||||
|
fs.stat(directory, function(err, stats) {
|
||||||
|
//Check if error defined and the error code is "not exists"
|
||||||
|
if (err && err.errno === 34) {
|
||||||
|
//Create the directory, call the callback.
|
||||||
|
fs.mkdir(directory, callback);
|
||||||
|
} else {
|
||||||
|
//just in case there was a different error:
|
||||||
|
callback(err)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init: init,
|
init: init,
|
||||||
|
@ -31,12 +47,14 @@ function jsonrpc_call(method, params, callback) {
|
||||||
});
|
});
|
||||||
|
|
||||||
xhr.open('POST', 'http://localhost:5279/lbryapi', true);
|
xhr.open('POST', 'http://localhost:5279/lbryapi', true);
|
||||||
xhr.send(JSON.stringify({
|
payload = {
|
||||||
'jsonrpc': '2.0',
|
'jsonrpc': '2.0',
|
||||||
'method': method,
|
'method': method,
|
||||||
'params': [params],
|
'params': [params],
|
||||||
'id': 0
|
'id': 0
|
||||||
}));
|
};
|
||||||
|
console.log('JSONRPC', payload);
|
||||||
|
xhr.send(JSON.stringify(payload));
|
||||||
}
|
}
|
||||||
|
|
||||||
function handle_msg(msg, channel)
|
function handle_msg(msg, channel)
|
||||||
|
@ -77,8 +95,7 @@ function check_url(url, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
var meta_version = resolved.ver ? resolved.ver : '0.0.1';
|
var meta_version = resolved.ver ? resolved.ver : '0.0.1';
|
||||||
var field_name = (meta_version == '0.0.1' || meta_version == '0.0.2') ?
|
var field_name = (meta_version == '0.0.1' || meta_version == '0.0.2') ? 'content-type' : 'content_type';
|
||||||
'content-type' : 'content_type';
|
|
||||||
var content_type = resolved[field_name];
|
var content_type = resolved[field_name];
|
||||||
callback(content_type == 'image/gif');
|
callback(content_type == 'image/gif');
|
||||||
});
|
});
|
||||||
|
@ -104,7 +121,6 @@ function handle_url(url, channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cache[channel][url] = now;
|
|
||||||
|
|
||||||
check_url(url, function(valid)
|
check_url(url, function(valid)
|
||||||
{
|
{
|
||||||
|
@ -123,29 +139,37 @@ function handle_url(url, channel)
|
||||||
|
|
||||||
function fetch_url(url, channel)
|
function fetch_url(url, channel)
|
||||||
{
|
{
|
||||||
jsonrpc_call('get', {'name': url, 'download_directory': output_dir},
|
checkDirectory(output_dir, function(error)
|
||||||
function(response)
|
|
||||||
{
|
{
|
||||||
var result = response.result;
|
if(error) {
|
||||||
if (!result)
|
console.error("Could not create output directory", error);
|
||||||
{
|
slackbot.postMessage(channel, 'Unable to fetch URL [' + url + ']. Output directory missing.');
|
||||||
console.warn('Failed to fetch', url);
|
} else {
|
||||||
console.warn(response);
|
jsonrpc_call('get', {'name': url, 'download_directory': output_dir}, function(response)
|
||||||
slackbot.postMessage(channel, 'Unable to fetch URL [' + url + ']. Insufficient funds?');
|
{
|
||||||
return;
|
var result = response.result;
|
||||||
}
|
if (!result)
|
||||||
|
{
|
||||||
|
console.warn('Failed to fetch', url);
|
||||||
|
console.warn(response);
|
||||||
|
slackbot.postMessage(channel, 'Unable to fetch URL [' + url + ']. Insufficient funds?');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var filename = result.path;
|
var filename = result.path;
|
||||||
console.log('Uploading', filename);
|
console.log('Uploading', filename);
|
||||||
imgur.uploadFile(filename).then(function(uploaded)
|
imgur.uploadFile(filename).then(function(uploaded)
|
||||||
{
|
{
|
||||||
var link = uploaded.data.link;
|
var link = uploaded.data.link;
|
||||||
console.log(link);
|
console.log(link);
|
||||||
var attachments = [{image_url: link, title: url}];
|
var attachments = [{image_url: link, title: url}];
|
||||||
slackbot.postMessage(channel, null, {attachments: attachments})
|
slackbot.postMessage(channel, null, {attachments: attachments})
|
||||||
}).catch(function(err)
|
cache[channel][url] = new Date().getTime() / 1000;
|
||||||
{
|
}).catch(function(err)
|
||||||
console.error(err.message);
|
{
|
||||||
});
|
console.error(err.message);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue