From 959ba2a84d2cf4de0bdc401275e992b6e1c4ecd7 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sun, 14 Aug 2016 01:40:14 +0200 Subject: [PATCH] optimized dlfunc --- src/main.js | 59 ++++++++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/src/main.js b/src/main.js index 4f40b5c..4214a44 100644 --- a/src/main.js +++ b/src/main.js @@ -1,8 +1,9 @@ var cfg = require('../../cfg.json'); var fs = require('fs'); -var request = require('request'); +var https = require("https"); var mysql = require('mysql'); var bot = require('coffea')(); +const path = require('path'); cfg.server.forEach((e,i,a) => { bot.add({ @@ -22,62 +23,46 @@ cfg.server.forEach((e,i,a) => { bot.on('motd', (e) => { console.log("motd von "+e.network+" erhalten"); bot.write('MODE f0ck +B', e.network, (c)=>{}); // Botflag - //if(g_server[e.network].channels.length > 0) { - // bot.join( JSON.parse( g_server[e.network].channels ) ); - //} }); bot.on('message', (e) => { var orig = e.message; - - if(e.channel.getName().toString() == "#f0ck") { - - if(orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi)) { - var tmp = orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); + if(e.channel.getName().toString() == "#f0ck") { // (debug) only in channel #f0ck + if(orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi)) { // shitpostcatcher + var tmp = orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links tmp.forEach((entry,i,a) => { - dl(entry, "./b/test.png", (cb) => { + dl(entry, "./b/"+path.parse(entry).base, (cb) => { // download item e.reply(cb); }); }); } - else if(orig.match(/^\.test/)) { + else if(orig.match(/^\.user/)) { // (debug) get userinfos var tmp = getUser(e.user.getNick(), e.network); - - setTimeout(()=>{ - e.reply(tmp); - }, 2000); + setTimeout(()=>{ e.reply(tmp); }, 2000); } - } }); var getUser = (u, n) => { - bot.whois(u, n, (fn)=>{ }); - bot.getUser(u, n); - + bot.whois(u, n, (fn)=>{ }); // send whois var start = Date.now(); - while (Date.now() < start + 3000) {} - + while(Date.now() < start + 2000) {} // block script for catch whois return bot.getUser(u, n); }; var dl = (url, dest, cb) => { var file = fs.createWriteStream(dest); - var sendReq = request.get(url); - sendReq.on('response', (response) => { - if(response.statusCode !== 200) return cb('Response status was ' + response.statusCode); + var request = https.get(url, (response) => { + response.pipe(file); + file.on('finish', () => { + file.close(); + cb('downloaded '+dest); + }); + file.on('error', (err) => { + fs.unlink(dest); + file.close(); + cb(err.message); + }); }); - sendReq.on('error', (err) => { - fs.unlink(dest); - if(cb) return cb(err.message); - }); - sendReq.pipe(file); - file.on('finish', () => { - file.close(cb); - }); - file.on('error', (err) => { - fs.unlink(dest); - if(cb) return cb(err.message); - }); -}; \ No newline at end of file +} \ No newline at end of file