dimanche 15 mars 2015

setInterval refuses to run my function after recent edit to my code... why?

To give you a grasp of what I mean in my title.


Take a look at this code which is before the setInterval stopped working.



"use strict"
// DerpLib
var MM = module.parent,
DerpLib = MM.parent,
_ = require('underscore'),
utils = DerpLib.MM.libary.load('utils'),
http = require('http'),
parseString = require('xml2js').parseString,
url = require('url'),
db = DerpLib.MM.plugin.load('database');

var anime = function(){
_.each(db.get('','animedb'), function(site){
var ann = function(){
if(site.lastbuilddate+site.delay < new Date()){
http.get({hostname:site.host, port:80, path:site.path}, function(res) {
if(res.statusCode === 200) { //200 is success
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
try{
var font = function(color, size, face) {
color = color || 'E0E0E0';
size = size || '12';
face = face || '2';
return '<f x'+String(size)+String(color)+'="'+String(face)+'">';
}
parseString(body, function(err, result) {
if(err) {
console.log('There was an error '+String(err));
}
else if(undefined === result.rss){
console.log('nothing found >_>');
}
else if(undefined === result.rss.channel[0].item){
console.log('nothing found >_>');
}
else if(result.rss.channel[0].item.length >= 1) {
var colors = {
normal: '788ab8'
}
//list episodes
var eps = result.rss.channel[0].item;
var list = [], counter = 0;
for (var epid in eps) {
epid = parseInt(epid, 10);
var eptime = (new Date(eps[epid].pubDate[0])*1000)/1000;
if(eptime > site.lastbuilddate){
counter = counter+1;
if(counter < 6){
list.push(font(colors['normal'])+eps[epid].title[0] +' - ['+ utils.secondsToString((new Date() - (eptime+site.delay))/1000, 1)+' ago.]</f>');
}
}
};
var extra;
if(list.length > 0){
if(counter > 5){
extra = 'There are '+Math.floor(counter-5)+' other episodes Refresh the page to see them.';
}else{
extra = 'There are no other episodes available Refresh the page to see new episodes.';
}
_.each(site.chats, function(chat){
DerpLib.getRoom(chat).message(_.flatten(['Anime Announcement from '+site.host,'Top '+(counter < 5 ? counter : 5)+' Episodes added in the past '+utils.secondsToString((new Date() - (site.lastbuilddate+site.delay))/1000, 1),list,font(colors['normal'])+extra+'</f>']), 'no color');
});
}
site.lastbuilddate = (new Date(result.rss.channel[0].lastBuildDate[0])*1000)/1000;
}
});
}catch(e){
console.log('nothing found and an error occured \r '+e);
}
});
}
}).on('error', function(e) {
console.log(' An error occured '+String(e));
});
}
}
var run = setInterval(ann, site.interval*60000);
});
};

anime();


The above code works and excuse me for saying this but at this point. I'm going to say "I have no idea why". Because i really have no idea why setInterval picks and chooses when to work. I talked to a friend who had more knowledge than me in javascript and time based functions and he said that there are no "conditions" required for setInterval to run.


Well from the code i'm about to show you I am pretty sure you will agree with me when I say "there is no way there are no conditions".



"use strict"
// DerpLib
var MM = module.parent,
DerpLib = MM.parent,
_ = require('underscore'),
utils = DerpLib.MM.libary.load('utils'),
http = require('http'),
parseString = require('xml2js').parseString,
url = require('url'),
db = DerpLib.MM.plugin.load('database');

var anime = function(){
_.each(db.get('','animedb'), function(site){
var ann = function(){
if(site.lastbuilddate+site.delay < new Date()){
http.get({hostname:site.host, port:80, path:site.path}, function(res) {
if(res.statusCode === 200) { //200 is success
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
try{
var font = function(color, size, face) {
color = color || 'E0E0E0';
size = size || '12';
face = face || '2';
return '<f x'+String(size)+String(color)+'="'+String(face)+'">';
}
parseString(body, function(err, result) {
if(err) {
console.log('There was an error '+String(err));
}
else if(undefined === result.rss){
console.log('nothing found >_>');
}
else if(undefined === result.rss.channel[0].item){
console.log('nothing found >_>');
}
else if(result.rss.channel[0].item.length >= 1) {
var colors = {
normal: '788ab8'
}
//list episodes
var ctitle = result.rss.channel[0].title;
var eps = result.rss.channel[0].item;
var list = [], counter = 0;
for (var epid in eps) {
epid = parseInt(epid, 10);
var eptime = (new Date(eps[epid].pubDate[0])*1000)/1000;
if(eptime > site.lastbuilddate){
counter = counter+1;
if(counter < 6){
var url = eps[epid].link.split('//')[1];
var keyword = '';
var words = url.substr(0, url.length-1).split('/').join('-').split('-');
for (var wid in words) {
keyword += words[wid].charAt(0);
}
http.get({hostname:'dev.ilp.moe', port:80, path:'/surl/yourls-api.php?username=usernameremovedforsecurity&password=passwordremovedforsecurity&format=json&action=shorturl&url='+url+'&title='+ctitle+' - '+eps[epid].title[0]+'&keyword='+keyword}, function(r) {
if(r.statusCode === 200) { //200 is success
var b = '';
r.on('data', function(c) {
b += c;
});
r.on('end', function() {
list.push(font(colors['normal'])+eps[epid].title[0] +' - ['+ utils.secondsToString((new Date() - (eptime+site.delay))/1000, 1)+' ago.] - http://ift.tt/1GJHWOd'+keyword+'</f>');
}
}
});
}
}
};
var extra;
if(list.length > 0){
if(counter > 5){
extra = 'There are '+Math.floor(counter-5)+' other episodes Refresh the page to see them.';
}else{
extra = 'There are no other episodes available Refresh the page to see new episodes.';
}
_.each(site.chats, function(chat){
DerpLib.getRoom(chat).message(_.flatten(['Anime Announcement from '+site.host,'Top '+(counter < 5 ? counter : 5)+' Episodes added in the past '+utils.secondsToString((new Date() - (site.lastbuilddate+site.delay))/1000, 1),list,font(colors['normal'])+extra+'</f>']), 'no color');
});
}
site.lastbuilddate = (new Date(result.rss.channel[0].lastBuildDate[0])*1000)/1000;
}
});
}catch(e){
console.log('nothing found and an error occured \r '+e);
}
});
}
}).on('error', function(e) {
console.log(' An error occured '+String(e));
});
}
}
var run = setInterval(ann, site.interval*60000);
});
};

anime();


The above code is an anime announcement for chat rooms owned by anime sites owners.


Here is the json DB that is being loaded.



{"0":{"lastbuilddate":1426441081000,"delay":0,"host":"www.animerush.tv","path":"/rss.xml","chats":["animerushtv"],"interval":15},"1":{"lastbuilddate":1424068119000,"delay":28800000,"host":"dubbedanime.tv","path":"/feed/","chats":["dubbed-anime-tv"],"interval":15},"2":{"lastbuilddate":1426415086000,"delay":32400000,"host":"bestanimes.tv","path":"/feed/","chats":["bestanimestv"],"interval":15},"3":{"lastbuilddate":1426434866000,"delay":0,"host":"www.theanime.tv","path":"/feed/","chats":["timecapsule"],"interval":15}}


The recent edit to my code was supposed to implement Shortened links for each episode released using the links provided in the rss feeds from the sites in the database.


The domain http://ilp.moe is my domain.


I have console logged everywhere and tested as much as I possibly could. At this point I do not understand why the edit is making code that used to be executed by setInterval no longer be executed.


Aucun commentaire:

Enregistrer un commentaire