fix gifbot bugs

This commit is contained in:
Alex Grintsvayg 2016-09-08 17:50:23 -04:00
parent 598da3405d
commit c1938a3fb5

View file

@ -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,8 +139,13 @@ 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) {
if(error) {
console.error("Could not create output directory", error);
slackbot.postMessage(channel, 'Unable to fetch URL [' + url + ']. Output directory missing.');
} else {
jsonrpc_call('get', {'name': url, 'download_directory': output_dir}, function(response)
{ {
var result = response.result; var result = response.result;
if (!result) if (!result)
@ -143,9 +164,12 @@ function fetch_url(url, channel)
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})
cache[channel][url] = new Date().getTime() / 1000;
}).catch(function(err) }).catch(function(err)
{ {
console.error(err.message); console.error(err.message);
}); });
}); });
} }
});
}