From 7f0f6275126c8886d4dd847a17e3f8cb5245b816 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Mon, 13 Jan 2020 11:42:51 +0100 Subject: [PATCH 01/11] Fixed bug --- libs/Help.js | 116 +++++++++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/libs/Help.js b/libs/Help.js index dad8f70..3cbe700 100644 --- a/libs/Help.js +++ b/libs/Help.js @@ -58,36 +58,36 @@ class Help { break } } else { - this.say(say, where, 'IRC Radio Bot :: Help') + Help.say(say, where, 'IRC Radio Bot :: Help') if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!artist') !== -1) { - this.say(say, where, '!artist {nom à sauvegarder si différent du morceau en cours}') + Help.say(say, where, '!artist {nom à sauvegarder si différent du morceau en cours}') } if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!song') !== -1) { - this.say(say, where, '!song {nom à sauvegarder si différent du morceau en cours}') + Help.say(say, where, '!song {nom à sauvegarder si différent du morceau en cours}') } if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!stats') !== -1) { - this.say(say, where, '!stats {artist/song}') + Help.say(say, where, '!stats {artist/song}') } if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!when') !== -1) { - this.say(say, where, '!when ') + Help.say(say, where, '!when ') } if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!list') !== -1) { - this.say(say, where, '!list ') + Help.say(say, where, '!list ') } if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!help') !== -1) { - this.say(say, where, '!help {command}') + Help.say(say, where, '!help {command}') } if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!notifications') !== -1) { - this.say(say, where, '!notifications ') + Help.say(say, where, '!notifications ') } if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!stream') !== -1) { - this.say(say, where, '!stream') + Help.say(say, where, '!stream') } if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!mute') !== -1) { - this.say(say, where, '!mute ') + Help.say(say, where, '!mute ') } } - this.say(say, where, '__ END __') + Help.say(say, where, '__ END __') } /** @@ -97,9 +97,9 @@ class Help { * @param {Array} args */ static showStream (say, where, args) { - this.say(say, where, 'IRC Radio Bot :: Stream', false, true) - this.say(say, where, 'Affiche l\'url du flux radio pour ton player favoris', false, true) - this.say(say, where, '!stream') + Help.say(say, where, 'IRC Radio Bot :: Stream', false, true) + Help.say(say, where, 'Affiche l\'url du flux radio pour ton player favoris', false, true) + Help.say(say, where, '!stream') } /** @@ -109,11 +109,11 @@ class Help { * @param {Array} args */ static showMute (say, where, args) { - this.say(say, where, 'IRC Radio Bot :: Mute') - this.say(say, where, 'Permet d\'activer ou de désactiver les messages publics du bot') - this.say(say, where, '!mute on') - this.say(say, where, '!mute off') - this.say(say, where, '!mute state') + Help.say(say, where, 'IRC Radio Bot :: Mute') + Help.say(say, where, 'Permet d\'activer ou de désactiver les messages publics du bot') + Help.say(say, where, '!mute on') + Help.say(say, where, '!mute off') + Help.say(say, where, '!mute state') } /** @@ -123,9 +123,9 @@ class Help { * @param {Array} args */ static showArtistSong (say, where, args) { - this.say(say, where, 'IRC Radio Bot :: Artist/Song') - this.say(say, where, `${args[2]} add {[facultatif] nom} < Permet de rajouter une notification sur ${args[2] === '!artist' ? 'cet artiste' : 'ce morceau'}`) - this.say(say, where, `${args[2]} del {[facultatif] nom} < Permet de supprimer une notification sur ${args[2] === '!artist' ? 'cet artiste' : 'ce morceau'}`) + Help.say(say, where, 'IRC Radio Bot :: Artist/Song') + Help.say(say, where, `${args[2]} add {[facultatif] nom} < Permet de rajouter une notification sur ${args[2] === '!artist' ? 'cet artiste' : 'ce morceau'}`) + Help.say(say, where, `${args[2]} del {[facultatif] nom} < Permet de supprimer une notification sur ${args[2] === '!artist' ? 'cet artiste' : 'ce morceau'}`) } /** @@ -134,17 +134,17 @@ class Help { * @param {String} where */ static showStats (say, where) { - this.say(say, where, 'IRC Radio Bot :: Stats') - this.say(say, where, 'Permet d\'afficher le nombre de passage à l\'antenne d\'un artiste/morceau sur une période donnée') - this.say(say, where, '!stats day ') - this.say(say, where, '!stats week <title>') - this.say(say, where, '!stats month <title>') - this.say(say, where, '!stats year <title>') - this.say(say, where, '!stats lastday <title>') - this.say(say, where, '!stats lastweek <title>') - this.say(say, where, '!stats lastmonth <title>') - this.say(say, where, '!stats lastyear <title>') - this.say(say, where, 'Si <title> n\'est pas spécifie la stats concerne le nombre d\'artistes différents pour cette période') + Help.say(say, where, 'IRC Radio Bot :: Stats') + Help.say(say, where, 'Permet d\'afficher le nombre de passage à l\'antenne d\'un artiste/morceau sur une période donnée') + Help.say(say, where, '!stats day <title>') + Help.say(say, where, '!stats week <title>') + Help.say(say, where, '!stats month <title>') + Help.say(say, where, '!stats year <title>') + Help.say(say, where, '!stats lastday <title>') + Help.say(say, where, '!stats lastweek <title>') + Help.say(say, where, '!stats lastmonth <title>') + Help.say(say, where, '!stats lastyear <title>') + Help.say(say, where, 'Si <title> n\'est pas spécifie la stats concerne le nombre d\'artistes différents pour cette période') } /** @@ -153,16 +153,16 @@ class Help { * @param {String} where */ static showWhen (say, where) { - this.say(say, where, 'IRC Radio Bot :: When') - this.say(say, where, 'Permet d\'afficher la dernier passage d\'un artiste/morceau sur une période donnée') - this.say(say, where, '!when day <title>') - this.say(say, where, '!when week <title>') - this.say(say, where, '!when month <title>') - this.say(say, where, '!when year <title>') - this.say(say, where, '!when lastday <title>') - this.say(say, where, '!when lastweek <title>') - this.say(say, where, '!when lastmonth <title>') - this.say(say, where, '!when lastyear <title>') + Help.say(say, where, 'IRC Radio Bot :: When') + Help.say(say, where, 'Permet d\'afficher la dernier passage d\'un artiste/morceau sur une période donnée') + Help.say(say, where, '!when day <title>') + Help.say(say, where, '!when week <title>') + Help.say(say, where, '!when month <title>') + Help.say(say, where, '!when year <title>') + Help.say(say, where, '!when lastday <title>') + Help.say(say, where, '!when lastweek <title>') + Help.say(say, where, '!when lastmonth <title>') + Help.say(say, where, '!when lastyear <title>') } /** @@ -171,16 +171,16 @@ class Help { * @param {String} where */ static showList (say, where) { - this.say(say, where, 'IRC Radio Bot :: List') - this.say(say, where, 'Permet d\'afficher la liste des titres joués pour un artiste sr une période donnée') - this.say(say, where, '!list day <artist>') - this.say(say, where, '!list week <artist>') - this.say(say, where, '!list month <artist>') - this.say(say, where, '!list year <artist>') - this.say(say, where, '!list lastday <artist>') - this.say(say, where, '!list lastweek <artist>') - this.say(say, where, '!list lastmonth <artist>') - this.say(say, where, '!list lastyear <artist>') + Help.say(say, where, 'IRC Radio Bot :: List') + Help.say(say, where, 'Permet d\'afficher la liste des titres joués pour un artiste sr une période donnée') + Help.say(say, where, '!list day <artist>') + Help.say(say, where, '!list week <artist>') + Help.say(say, where, '!list month <artist>') + Help.say(say, where, '!list year <artist>') + Help.say(say, where, '!list lastday <artist>') + Help.say(say, where, '!list lastweek <artist>') + Help.say(say, where, '!list lastmonth <artist>') + Help.say(say, where, '!list lastyear <artist>') } /** @@ -189,11 +189,11 @@ class Help { * @param {String} where */ static showNotifications (say, where) { - this.say(say, where, 'IRC Radio Bot :: Notifications') - this.say(say, where, 'Permet d\'activer et de désactiver les notifications pour soit') - this.say(say, where, '!notifications off') - this.say(say, where, '!notifications off') - this.say(say, where, '!notifications state < Permet de voir si l\'on a désactivé ou non les notifications') + Help.say(say, where, 'IRC Radio Bot :: Notifications') + Help.say(say, where, 'Permet d\'activer et de désactiver les notifications pour soit') + Help.say(say, where, '!notifications off') + Help.say(say, where, '!notifications off') + Help.say(say, where, '!notifications state < Permet de voir si l\'on a désactivé ou non les notifications') } } -- 2.39.5 From 1887164501dbf10fb678c55b8cb5919d05f24ed5 Mon Sep 17 00:00:00 2001 From: dbroqua <dbroqua@mousur.org> Date: Mon, 13 Jan 2020 11:58:05 +0100 Subject: [PATCH 02/11] Fixed bug in lastDay --- libs/Queries.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/Queries.js b/libs/Queries.js index c35f122..e2f10f2 100644 --- a/libs/Queries.js +++ b/libs/Queries.js @@ -72,8 +72,8 @@ class Queries { break case 'lastday': this.period = { - $gte: moment().subtract('day').startOf('day'), - $lte: moment().subtract('day').endOf('day') + $gte: moment().subtract(1, 'day').startOf('day'), + $lte: moment().subtract(1, 'day').endOf('day') } break case 'lastweek': -- 2.39.5 From 688c5a88580a0082fe472ca720785103038f9851 Mon Sep 17 00:00:00 2001 From: dbroqua <dbroqua@mousur.org> Date: Mon, 13 Jan 2020 14:26:18 +0100 Subject: [PATCH 03/11] @6-ajouter-un-alias-a-lastday --- libs/Help.js | 3 +++ libs/List.js | 1 + libs/Queries.js | 1 + libs/Statistics.js | 1 + libs/When.js | 1 + 5 files changed, 7 insertions(+) diff --git a/libs/Help.js b/libs/Help.js index 3cbe700..7ef6017 100644 --- a/libs/Help.js +++ b/libs/Help.js @@ -141,6 +141,7 @@ class Help { Help.say(say, where, '!stats month <title>') Help.say(say, where, '!stats year <title>') Help.say(say, where, '!stats lastday <title>') + Help.say(say, where, '!stats yesterday <title>') Help.say(say, where, '!stats lastweek <title>') Help.say(say, where, '!stats lastmonth <title>') Help.say(say, where, '!stats lastyear <title>') @@ -160,6 +161,7 @@ class Help { Help.say(say, where, '!when month <title>') Help.say(say, where, '!when year <title>') Help.say(say, where, '!when lastday <title>') + Help.say(say, where, '!when yesterday <title>') Help.say(say, where, '!when lastweek <title>') Help.say(say, where, '!when lastmonth <title>') Help.say(say, where, '!when lastyear <title>') @@ -178,6 +180,7 @@ class Help { Help.say(say, where, '!list month <artist>') Help.say(say, where, '!list year <artist>') Help.say(say, where, '!list lastday <artist>') + Help.say(say, where, '!list yesterday <artist>') Help.say(say, where, '!list lastweek <artist>') Help.say(say, where, '!list lastmonth <artist>') Help.say(say, where, '!list lastyear <artist>') diff --git a/libs/List.js b/libs/List.js index 9edf055..829d09e 100644 --- a/libs/List.js +++ b/libs/List.js @@ -30,6 +30,7 @@ class When extends Queries { case 'month': case 'year': case 'lastday': + case 'yesterday': case 'lastweek': case 'lastmonth': case 'lastyear': diff --git a/libs/Queries.js b/libs/Queries.js index e2f10f2..accc9ee 100644 --- a/libs/Queries.js +++ b/libs/Queries.js @@ -71,6 +71,7 @@ class Queries { } break case 'lastday': + case 'yesterday': this.period = { $gte: moment().subtract(1, 'day').startOf('day'), $lte: moment().subtract(1, 'day').endOf('day') diff --git a/libs/Statistics.js b/libs/Statistics.js index 83c2058..510bbd2 100644 --- a/libs/Statistics.js +++ b/libs/Statistics.js @@ -30,6 +30,7 @@ class When extends Queries { case 'month': case 'year': case 'lastday': + case 'yesterday': case 'lastweek': case 'lastmonth': case 'lastyear': diff --git a/libs/When.js b/libs/When.js index a8e63c8..e8b40a1 100644 --- a/libs/When.js +++ b/libs/When.js @@ -31,6 +31,7 @@ class When extends Queries { case 'month': case 'year': case 'lastday': + case 'yesterday': case 'lastweek': case 'lastmonth': case 'lastyear': -- 2.39.5 From 7cd51aaf94cac1ad42bffd01d3159f915f614bab Mon Sep 17 00:00:00 2001 From: dbroqua <dbroqua@mousur.org> Date: Mon, 13 Jan 2020 14:45:06 +0100 Subject: [PATCH 04/11] @4-gestion-des-admins --- index.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index d4bc206..4e45fbc 100644 --- a/index.js +++ b/index.js @@ -174,6 +174,7 @@ const botSay = (where, what, dontDisplayRadio, forceSend) => { */ const actions = (where, message, from) => { const exploded = message.split(' ') + const admins = (process.env.ADMINISTRATORS || 'ALL').toLowerCase().split(',') let allowedCmds = ['ALL'] if (process.env.AVAILABLE_CMD) { @@ -251,25 +252,29 @@ const actions = (where, message, from) => { } break case '!mute': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!mute') !== -1) { - if (exploded[2] && ['on', 'off', 'state'].indexOf(exploded[2]) !== -1) { - switch (exploded[2]) { - case 'on': - isMute = true - botSay(where, "Pff ! c'est toujours pareil ici :'(", false, true) - break - case 'off': - isMute = false - botSay(where, "Merci ! j'avais tellement de chose à te dire...", false, true) - break - default: - botSay(where, isMute ? 'Je ne dirais rien !' : "T'inquiète ! tu vas l'avoir ta notif !", false, true) + if (admins[0] === 'ALL' || allowedCmds.indexOf(from.toLowerCase()) !== -1) { + if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!mute') !== -1) { + if (exploded[2] && ['on', 'off', 'state'].indexOf(exploded[2]) !== -1) { + switch (exploded[2]) { + case 'on': + isMute = true + botSay(where, "Pff ! c'est toujours pareil ici :'(", false, true) + break + case 'off': + isMute = false + botSay(where, "Merci ! j'avais tellement de chose à te dire...", false, true) + break + default: + botSay(where, isMute ? 'Je ne dirais rien !' : "T'inquiète ! tu vas l'avoir ta notif !", false, true) + } + } else { + botSay(where, '!help mute', false, true) } } else { - botSay(where, '!help mute', false, true) + unknownCmd() } } else { - unknownCmd() + botSay(where, 'Bien tenté mais...') } break -- 2.39.5 From d2ebf282aa629a7fef432310fca67fea5291e554 Mon Sep 17 00:00:00 2001 From: dbroqua <dbroqua@mousur.org> Date: Thu, 16 Jan 2020 10:11:02 +0100 Subject: [PATCH 05/11] Fixed bug on json parse --- index.js | 77 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index 4e45fbc..9761095 100644 --- a/index.js +++ b/index.js @@ -99,46 +99,51 @@ if (!process.env.STREAM_TYPE || process.env.STREAM_TYPE === 'radio') { }, (error, response, body) => { if (!error && response.statusCode === 200) { - const res = JSON.parse(process.env.STREAM_PARSE ? body.replace('updateFromMediaItem(', '').replace(');', '') : body) + try { + const res = JSON.parse(process.env.STREAM_PARSE ? body.replace('updateFromMediaItem(', '').replace(');', '') : body) - let title = null - let artist = null + let title = null + let artist = null - switch (process.env.STREAM_TYPE) { - case 'json': - title = res[titlePath[0]] - for (let i = 1; i < titlePath.length; i += 1) { - title = title[titlePath[i]] - } - artist = res[artistPath[0]] - for (let i = 1; i < artistPath.length; i += 1) { - artist = artist[artistPath[i]] - } - - currentSong = { - title: title, - artist: artist - } - break - case 'alien': - try { - for (let i = 0; i < res.length; i += 1) { - if (res[i].name === 'Cult') { - currentSong = { - title: res[i].nowPlaying.track.title, - artist: res[i].nowPlaying.track.artist - } - break - } + switch (process.env.STREAM_TYPE) { + case 'json': + title = res[titlePath[0]] + for (let i = 1; i < titlePath.length; i += 1) { + title = title[titlePath[i]] + } + artist = res[artistPath[0]] + for (let i = 1; i < artistPath.length; i += 1) { + artist = artist[artistPath[i]] } - } catch (e) { - console.error('ERR:', e) - } - break - } - if (previousSong.title !== currentSong.title && previousSong.artist !== currentSong.artist) { - checkCurrentSong() + currentSong = { + title: title, + artist: artist + } + break + case 'alien': + try { + for (let i = 0; i < res.length; i += 1) { + if (res[i].name === 'Cult') { + currentSong = { + title: res[i].nowPlaying.track.title, + artist: res[i].nowPlaying.track.artist + } + break + } + } + } catch (e) { + console.error('ERR:', e) + } + break + } + + if (previousSong.title !== currentSong.title && previousSong.artist !== currentSong.artist) { + checkCurrentSong() + } + } catch (e) { + error = e + console.error('getStream error:', error) } } else { console.error('ERR:', error) -- 2.39.5 From 4020faec1397cb381f8787198436e6912178dab6 Mon Sep 17 00:00:00 2001 From: dbroqua <dbroqua@mousur.org> Date: Fri, 24 Jan 2020 09:09:32 +0100 Subject: [PATCH 06/11] Fixed bug on undefined title/artist --- index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 9761095..e446765 100644 --- a/index.js +++ b/index.js @@ -96,8 +96,7 @@ if (!process.env.STREAM_TYPE || process.env.STREAM_TYPE === 'radio') { request({ method: 'GET', url: process.env.STREAM_URL - }, - (error, response, body) => { + }, (error, response, body) => { if (!error && response.statusCode === 200) { try { const res = JSON.parse(process.env.STREAM_PARSE ? body.replace('updateFromMediaItem(', '').replace(');', '') : body) @@ -138,7 +137,7 @@ if (!process.env.STREAM_TYPE || process.env.STREAM_TYPE === 'radio') { break } - if (previousSong.title !== currentSong.title && previousSong.artist !== currentSong.artist) { + if (currentSong.title && currentSong.artist && previousSong.title !== currentSong.title && previousSong.artist !== currentSong.artist) { checkCurrentSong() } } catch (e) { @@ -301,7 +300,7 @@ bot.addListener('pm', function (from, to, message) { // Say hello! if (process.env.SAY_HELLO && process.env.SAY_HELLO === 'true') { bot.addListener('join', (channel, who) => { - // Welcome them in! + // Welcome them in! if (who !== process.env.IRC_NICKNAME) { botSay(process.env.IRC_CHANNEL, 'Hello ' + who + '! Have a metal day! \\m/(-.-)\\m/', true, true) } -- 2.39.5 From c7655bf6c92c32d8e0c918b87d48bbcba9c452aa Mon Sep 17 00:00:00 2001 From: dbroqua <dbroqua@mousur.org> Date: Fri, 24 Jan 2020 09:56:38 +0100 Subject: [PATCH 07/11] Fixed bug --- libs/Help.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libs/Help.js b/libs/Help.js index 7ef6017..f3fed22 100644 --- a/libs/Help.js +++ b/libs/Help.js @@ -1,6 +1,6 @@ class Help { - static helpSay (say, where, message) { + static say(say, where, message) { say(where, message, true, true) } @@ -11,7 +11,7 @@ class Help { * @param {Array} args * @param {Array} allowedCmds */ - static show (say, where, args, allowedCmds) { + static show(say, where, args, allowedCmds) { if (args.length > 2) { switch (args[2]) { case '!artist': @@ -96,7 +96,7 @@ class Help { * @param {String} where * @param {Array} args */ - static showStream (say, where, args) { + static showStream(say, where, args) { Help.say(say, where, 'IRC Radio Bot :: Stream', false, true) Help.say(say, where, 'Affiche l\'url du flux radio pour ton player favoris', false, true) Help.say(say, where, '!stream') @@ -108,7 +108,7 @@ class Help { * @param {String} where * @param {Array} args */ - static showMute (say, where, args) { + static showMute(say, where, args) { Help.say(say, where, 'IRC Radio Bot :: Mute') Help.say(say, where, 'Permet d\'activer ou de désactiver les messages publics du bot') Help.say(say, where, '!mute on') @@ -122,7 +122,7 @@ class Help { * @param {String} where * @param {Array} args */ - static showArtistSong (say, where, args) { + static showArtistSong(say, where, args) { Help.say(say, where, 'IRC Radio Bot :: Artist/Song') Help.say(say, where, `${args[2]} add {[facultatif] nom} < Permet de rajouter une notification sur ${args[2] === '!artist' ? 'cet artiste' : 'ce morceau'}`) Help.say(say, where, `${args[2]} del {[facultatif] nom} < Permet de supprimer une notification sur ${args[2] === '!artist' ? 'cet artiste' : 'ce morceau'}`) @@ -133,7 +133,7 @@ class Help { * @param {Function} say * @param {String} where */ - static showStats (say, where) { + static showStats(say, where) { Help.say(say, where, 'IRC Radio Bot :: Stats') Help.say(say, where, 'Permet d\'afficher le nombre de passage à l\'antenne d\'un artiste/morceau sur une période donnée') Help.say(say, where, '!stats day <title>') @@ -153,7 +153,7 @@ class Help { * @param {Function} say * @param {String} where */ - static showWhen (say, where) { + static showWhen(say, where) { Help.say(say, where, 'IRC Radio Bot :: When') Help.say(say, where, 'Permet d\'afficher la dernier passage d\'un artiste/morceau sur une période donnée') Help.say(say, where, '!when day <title>') @@ -172,7 +172,7 @@ class Help { * @param {Function} say * @param {String} where */ - static showList (say, where) { + static showList(say, where) { Help.say(say, where, 'IRC Radio Bot :: List') Help.say(say, where, 'Permet d\'afficher la liste des titres joués pour un artiste sr une période donnée') Help.say(say, where, '!list day <artist>') @@ -191,7 +191,7 @@ class Help { * @param {Function} say * @param {String} where */ - static showNotifications (say, where) { + static showNotifications(say, where) { Help.say(say, where, 'IRC Radio Bot :: Notifications') Help.say(say, where, 'Permet d\'activer et de désactiver les notifications pour soit') Help.say(say, where, '!notifications off') -- 2.39.5 From 9e5437f360f1a0ac245496cbe3adf387dbeaf2a2 Mon Sep 17 00:00:00 2001 From: dbroqua <dbroqua@mousur.org> Date: Tue, 28 Jan 2020 10:10:50 +0100 Subject: [PATCH 08/11] Fixed bug for duplicate song (heavy1) --- index.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index e446765..af69bbc 100644 --- a/index.js +++ b/index.js @@ -32,10 +32,16 @@ let currentSong = { artist: null, title: null } -let previousSong = { - artist: null, - title: null -} +const previousSongs = [ + { + artist: null, + title: null + }, + { + artist: null, + title: null + } +] // Initialisation du bot IRC const bot = new irc.Client(process.env.IRC_SERVER, process.env.IRC_NICKNAME, { @@ -45,8 +51,7 @@ const bot = new irc.Client(process.env.IRC_SERVER, process.env.IRC_NICKNAME, { channels: [process.env.IRC_CHANNEL] }) -const checkCurrentSong = () => { - previousSong = currentSong +const displayCurrentSong = () => { // Just for debug console.info(`Now playing: ${currentSong.artist} - ${currentSong.title}`) @@ -89,7 +94,7 @@ if (!process.env.STREAM_TYPE || process.env.STREAM_TYPE === 'radio') { artist: artist } - checkCurrentSong() + displayCurrentSong() }) } else { setInterval(() => { @@ -137,8 +142,13 @@ if (!process.env.STREAM_TYPE || process.env.STREAM_TYPE === 'radio') { break } - if (currentSong.title && currentSong.artist && previousSong.title !== currentSong.title && previousSong.artist !== currentSong.artist) { - checkCurrentSong() + if (currentSong.title && currentSong.artist && + (previousSongs[0].title !== currentSong.title && previousSongs[0].artist !== currentSong.artist) && + (previousSongs[1].title !== currentSong.title && previousSongs[1].artist !== currentSong.artist) + ) { + previousSongs[1] = previousSongs[0] + previousSongs[0] = currentSong + displayCurrentSong() } } catch (e) { error = e -- 2.39.5 From 0e1ebea121605a35af032b9858eece2a53180727 Mon Sep 17 00:00:00 2001 From: dbroqua <dbroqua@mousur.org> Date: Thu, 30 Jan 2020 16:11:48 +0100 Subject: [PATCH 09/11] Fixed typo --- libs/ArtistSong.js | 12 ++++++------ libs/Help.js | 18 +++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/libs/ArtistSong.js b/libs/ArtistSong.js index 100394c..ad370bd 100644 --- a/libs/ArtistSong.js +++ b/libs/ArtistSong.js @@ -21,11 +21,11 @@ class ArtistSong { .limit(1) .exec((err, last) => { if (err || - last.length === 0 || - (last[0] !== undefined && - last[0].artist !== value.artist && - last[0].title !== value.title - ) + last.length === 0 || + (last[0] !== undefined && + last[0].artist !== value.artist && + last[0].title !== value.title + ) ) { console.log('Save song!', value) // Previous song was different => save song! @@ -178,7 +178,7 @@ class ArtistSong { } }) } else { - botSay(from, `${item.value} n'est dpas dans tes favoris, c'est moche de vieillir...`) + botSay(from, `${item.value} n'est pas dans tes favoris, c'est moche de vieillir...`) } }) .catch(err => { diff --git a/libs/Help.js b/libs/Help.js index f3fed22..1a2ec4d 100644 --- a/libs/Help.js +++ b/libs/Help.js @@ -1,6 +1,6 @@ class Help { - static say(say, where, message) { + static say (say, where, message) { say(where, message, true, true) } @@ -11,7 +11,7 @@ class Help { * @param {Array} args * @param {Array} allowedCmds */ - static show(say, where, args, allowedCmds) { + static show (say, where, args, allowedCmds) { if (args.length > 2) { switch (args[2]) { case '!artist': @@ -96,7 +96,7 @@ class Help { * @param {String} where * @param {Array} args */ - static showStream(say, where, args) { + static showStream (say, where, args) { Help.say(say, where, 'IRC Radio Bot :: Stream', false, true) Help.say(say, where, 'Affiche l\'url du flux radio pour ton player favoris', false, true) Help.say(say, where, '!stream') @@ -108,7 +108,7 @@ class Help { * @param {String} where * @param {Array} args */ - static showMute(say, where, args) { + static showMute (say, where, args) { Help.say(say, where, 'IRC Radio Bot :: Mute') Help.say(say, where, 'Permet d\'activer ou de désactiver les messages publics du bot') Help.say(say, where, '!mute on') @@ -122,7 +122,7 @@ class Help { * @param {String} where * @param {Array} args */ - static showArtistSong(say, where, args) { + static showArtistSong (say, where, args) { Help.say(say, where, 'IRC Radio Bot :: Artist/Song') Help.say(say, where, `${args[2]} add {[facultatif] nom} < Permet de rajouter une notification sur ${args[2] === '!artist' ? 'cet artiste' : 'ce morceau'}`) Help.say(say, where, `${args[2]} del {[facultatif] nom} < Permet de supprimer une notification sur ${args[2] === '!artist' ? 'cet artiste' : 'ce morceau'}`) @@ -133,7 +133,7 @@ class Help { * @param {Function} say * @param {String} where */ - static showStats(say, where) { + static showStats (say, where) { Help.say(say, where, 'IRC Radio Bot :: Stats') Help.say(say, where, 'Permet d\'afficher le nombre de passage à l\'antenne d\'un artiste/morceau sur une période donnée') Help.say(say, where, '!stats day <title>') @@ -153,7 +153,7 @@ class Help { * @param {Function} say * @param {String} where */ - static showWhen(say, where) { + static showWhen (say, where) { Help.say(say, where, 'IRC Radio Bot :: When') Help.say(say, where, 'Permet d\'afficher la dernier passage d\'un artiste/morceau sur une période donnée') Help.say(say, where, '!when day <title>') @@ -172,7 +172,7 @@ class Help { * @param {Function} say * @param {String} where */ - static showList(say, where) { + static showList (say, where) { Help.say(say, where, 'IRC Radio Bot :: List') Help.say(say, where, 'Permet d\'afficher la liste des titres joués pour un artiste sr une période donnée') Help.say(say, where, '!list day <artist>') @@ -191,7 +191,7 @@ class Help { * @param {Function} say * @param {String} where */ - static showNotifications(say, where) { + static showNotifications (say, where) { Help.say(say, where, 'IRC Radio Bot :: Notifications') Help.say(say, where, 'Permet d\'activer et de désactiver les notifications pour soit') Help.say(say, where, '!notifications off') -- 2.39.5 From af307b5888781ee3c488bcb6187218c5638d273f Mon Sep 17 00:00:00 2001 From: dbroqua <dbroqua@mousur.org> Date: Fri, 31 Jan 2020 16:27:27 +0100 Subject: [PATCH 10/11] Fixed bug for parse JSON --- index.js | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index af69bbc..9dc367e 100644 --- a/index.js +++ b/index.js @@ -27,11 +27,12 @@ if (process.env.STREAM_PATH_ARTIST) { artistPath = process.env.STREAM_PATH_ARTIST.split('.') } -// Stockage du morceau en cours de lecture +// Sauvegarde temporaire du morceau en cours de lecture let currentSong = { artist: null, title: null } +// Sauvegarde des précédents morceaux (des 2 derniers à cause d'un bug sur Heavy1) const previousSongs = [ { artist: null, @@ -51,6 +52,31 @@ const bot = new irc.Client(process.env.IRC_SERVER, process.env.IRC_NICKNAME, { channels: [process.env.IRC_CHANNEL] }) +/** + * Fonction permettant d'extraire des infos depuis un objet JSON + * @param {Object} json + * @param {String} array + */ +const extractInfoFromJSON = (json, path) => { + let value = json + for (let i = 0; i < path.length; i += 1) { + if (path[i].indexOf('[') !== -1) { + const extractKeyIdex = path[i].split('[') + const key = extractKeyIdex[0] + const index = extractKeyIdex[1].replace(']', '') + + value = value[key][index] + } else { + value = value[path[i]] + } + } + + return value +} + +/** + * Fonction d'aficher le morceau en cours de lecture (et de le sauvegarder en base) + */ const displayCurrentSong = () => { // Just for debug console.info(`Now playing: ${currentSong.artist} - ${currentSong.title}`) @@ -111,14 +137,8 @@ if (!process.env.STREAM_TYPE || process.env.STREAM_TYPE === 'radio') { switch (process.env.STREAM_TYPE) { case 'json': - title = res[titlePath[0]] - for (let i = 1; i < titlePath.length; i += 1) { - title = title[titlePath[i]] - } - artist = res[artistPath[0]] - for (let i = 1; i < artistPath.length; i += 1) { - artist = artist[artistPath[i]] - } + title = extractInfoFromJSON(res, titlePath) + artist = extractInfoFromJSON(res, artistPath) currentSong = { title: title, -- 2.39.5 From c0f6dea24a5d05140aaadd9233edd441f3eda87d Mon Sep 17 00:00:00 2001 From: dbroqua <contact@darkou.fr> Date: Tue, 26 Oct 2021 15:35:25 +0200 Subject: [PATCH 11/11] Fixed bug for CosmicFuzzFM radio --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 9dc367e..72e5e2f 100644 --- a/index.js +++ b/index.js @@ -106,8 +106,6 @@ if (!process.env.STREAM_TYPE || process.env.STREAM_TYPE === 'radio') { // Listener on new song stream.on('metadata', function (data) { - console.info('RAW DATA:', data) - // let song = data.replace(/[^A-Za-z 0-9 .,?""!@#$%^&*()-_=+;:<>/\\|}{[\]`~]*/g, '') const song = data.substring(13, data.length - 1).replace(/\0/g, '').replace('\';', '') // Extract artist = title from song @@ -130,7 +128,7 @@ if (!process.env.STREAM_TYPE || process.env.STREAM_TYPE === 'radio') { }, (error, response, body) => { if (!error && response.statusCode === 200) { try { - const res = JSON.parse(process.env.STREAM_PARSE ? body.replace('updateFromMediaItem(', '').replace(');', '') : body) + const res = JSON.parse(process.env.STREAM_PARSE ? body.substr(body.indexOf('{')).replace(');', '') : body) let title = null let artist = null -- 2.39.5