From d2ebf282aa629a7fef432310fca67fea5291e554 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Thu, 16 Jan 2020 10:11:02 +0100 Subject: [PATCH 1/7] 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 Date: Fri, 24 Jan 2020 09:09:32 +0100 Subject: [PATCH 2/7] 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 Date: Fri, 24 Jan 2020 09:56:38 +0100 Subject: [PATCH 3/7] 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 ') @@ -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 4/7] 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 5/7] 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 6/7] 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 7/7] 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