diff --git a/index.js b/index.js index 72e5e2f..9b6b38c 100644 --- a/index.js +++ b/index.js @@ -27,22 +27,15 @@ if (process.env.STREAM_PATH_ARTIST) { artistPath = process.env.STREAM_PATH_ARTIST.split('.') } -// Sauvegarde temporaire du morceau en cours de lecture +// Stockage 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, - title: null - }, - { - artist: null, - title: null - } -] +let previousSong = { + artist: null, + title: null +} // Initialisation du bot IRC const bot = new irc.Client(process.env.IRC_SERVER, process.env.IRC_NICKNAME, { @@ -52,32 +45,8 @@ 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 = () => { +const checkCurrentSong = () => { + previousSong = currentSong // Just for debug console.info(`Now playing: ${currentSong.artist} - ${currentSong.title}`) @@ -106,6 +75,8 @@ 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 @@ -118,59 +89,56 @@ if (!process.env.STREAM_TYPE || process.env.STREAM_TYPE === 'radio') { artist: artist } - displayCurrentSong() + checkCurrentSong() }) } else { setInterval(() => { 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.substr(body.indexOf('{')).replace(');', '') : body) + 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 = extractInfoFromJSON(res, titlePath) - artist = extractInfoFromJSON(res, artistPath) + 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 + 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 - } + } catch (e) { + console.error('ERR:', e) + } + break + } - 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 - console.error('getStream error:', error) + if (previousSong.title !== currentSong.title && previousSong.artist !== currentSong.artist) { + checkCurrentSong() } } else { console.error('ERR:', error) @@ -206,112 +174,51 @@ 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) { - allowedCmds = process.env.AVAILABLE_CMD.split(',') - } - if (where.indexOf('#') === 0 && process.env[`AVAILABLE_CMD_${where.replace('#', '')}`]) { - allowedCmds = process.env[`AVAILABLE_CMD_${where.replace('#', '')}`].split(',') - } - - const unknownCmd = () => { - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!help') !== -1) { - botSay(where, 'Commande inconnue, tape !help si tu es perdu', false, true) - } else { - botSay(where, 'Commande inconnue...', false, true) - } - } // Le message publié est pour le bot if (exploded[0].indexOf(process.env.IRC_NICKNAME) === 0) { switch (exploded[1]) { case '!artist': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!artist') !== -1) { - artistSong.action(exploded[1].replace('!', ''), botSay, from, currentSong, exploded) - } else { - unknownCmd() - } - break case '!song': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!song') !== -1) { - artistSong.action(exploded[1].replace('!', ''), botSay, from, currentSong, exploded) - } else { - unknownCmd() - } + artistSong.action(exploded[1].replace('!', ''), botSay, from, currentSong, exploded) break case '!help': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!help') !== -1) { - Help.show(botSay, where, exploded, allowedCmds) - } else { - unknownCmd() - } + Help.show(botSay, where, exploded) break case '!when': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!when') !== -1) { - when.action(botSay, where, exploded) - } else { - unknownCmd() - } + when.action(botSay, where, exploded) break case '!stats': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!stats') !== -1) { - stats.action(botSay, where, exploded) - } else { - unknownCmd() - } + stats.action(botSay, where, exploded) break case '!list': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!list') !== -1) { - list.action(botSay, where, exploded) - } else { - unknownCmd() - } + list.action(botSay, where, exploded) break case '!notifications': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!notifications') !== -1) { - preferences.notifications(botSay, where, from, exploded) - } else { - unknownCmd() - } + preferences.notifications(botSay, where, from, exploded) break case '!stream': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!stream') !== -1) { - botSay(where, process.env.PUBLIC_RADIO_STREAM) - } else { - unknownCmd() - } + botSay(where, process.env.PUBLIC_RADIO_STREAM) break case '!mute': - 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 { - unknownCmd() + if (exploded[2] && ['on', 'off', 'state'].exploded[2] !== -1) { + switch (exploded[2]) { + case 'on': + isMute = true + break + case 'off': + isMute = false + break + default: + botSay(where, isMute ? 'Je ne dirais rien !' : "T'inquiète ! tu vas l'avoir ta notif !", false, true) } } else { - botSay(where, 'Bien tenté mais...') + botSay(where, '!help mute', false, true) } break default: - unknownCmd() + botSay(where, 'Commande inconnue, tape !help si tu es perdu', false, true) break } } @@ -328,7 +235,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) } diff --git a/libs/ArtistSong.js b/libs/ArtistSong.js index ad370bd..100394c 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 pas dans tes favoris, c'est moche de vieillir...`) + botSay(from, `${item.value} n'est dpas dans tes favoris, c'est moche de vieillir...`) } }) .catch(err => { diff --git a/libs/Help.js b/libs/Help.js index 1a2ec4d..b7b32e8 100644 --- a/libs/Help.js +++ b/libs/Help.js @@ -1,6 +1,6 @@ class Help { - static say (say, where, message) { + static helpSay (say, where, message) { say(where, message, true, true) } @@ -9,85 +9,48 @@ class Help { * @param {Function} say * @param {String} where * @param {Array} args - * @param {Array} allowedCmds */ - static show (say, where, args, allowedCmds) { + static show (say, where, args) { if (args.length > 2) { switch (args[2]) { case '!artist': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!artist') !== -1) { - Help.showArtistSong(say, where, args) - } - break case '!song': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!song') !== -1) { - Help.showArtistSong(say, where, args) - } + Help.showArtistSong(say, where, args) break case '!stats': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!stats') !== -1) { - Help.showStats(say, where) - } + Help.showStats(say, where) break case '!when': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!when') !== -1) { - Help.showWhen(say, where) - } + Help.showWhen(say, where) break case '!list': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!list') !== -1) { - Help.showList(say, where) - } + Help.showList(say, where) break case '!notifications': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!notifications') !== -1) { - Help.showNotifications(say, where) - } + Help.showNotifications(say, where) break case '!stream': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!stream') !== -1) { - Help.showStream(say, where) - } + Help.showStream(say, where) break case '!mute': - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!mute') !== -1) { - Help.showMute(say, where) - } + Help.showMute(say, where) break default: break } } else { - Help.say(say, where, 'IRC Radio Bot :: Help') - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!artist') !== -1) { - Help.say(say, where, '!artist {nom à sauvegarder si différent du morceau en cours}') - } - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!song') !== -1) { - Help.say(say, where, '!song {nom à sauvegarder si différent du morceau en cours}') - } - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!stats') !== -1) { - Help.say(say, where, '!stats {artist/song}') - } - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!when') !== -1) { - Help.say(say, where, '!when ') - } - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!list') !== -1) { - Help.say(say, where, '!list ') - } - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!help') !== -1) { - Help.say(say, where, '!help {command}') - } - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!notifications') !== -1) { - Help.say(say, where, '!notifications ') - } - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!stream') !== -1) { - Help.say(say, where, '!stream') - } - if (allowedCmds[0] === 'ALL' || allowedCmds.indexOf('!mute') !== -1) { - Help.say(say, where, '!mute ') - } + this.say(say, where, 'IRC Radio Bot :: Help') + this.say(say, where, '!artist {nom à sauvegarder si différent du morceau en cours}') + this.say(say, where, '!song {nom à sauvegarder si différent du morceau en cours}') + this.say(say, where, '!stats {artist/song}') + this.say(say, where, '!when ') + this.say(say, where, '!list ') + this.say(say, where, '!help {command}') + this.say(say, where, '!notifications ') + this.say(say, where, '!stream') + this.say(say, where, '!mute ') } - Help.say(say, where, '__ END __') + this.say(say, where, '__ END __') } /** @@ -97,9 +60,9 @@ class Help { * @param {Array} 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') + 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') } /** @@ -109,11 +72,11 @@ class Help { * @param {Array} 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') - Help.say(say, where, '!mute off') - Help.say(say, where, '!mute state') + 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') } /** @@ -123,9 +86,9 @@ class Help { * @param {Array} 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'}`) + 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'}`) } /** @@ -134,18 +97,17 @@ class Help { * @param {String} 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 ') - 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 yesterday <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') + 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 <title>') + 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') } /** @@ -154,17 +116,16 @@ class Help { * @param {String} 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>') - 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 yesterday <title>') - Help.say(say, where, '!when lastweek <title>') - Help.say(say, where, '!when lastmonth <title>') - Help.say(say, where, '!when lastyear <title>') + 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>') } /** @@ -173,17 +134,16 @@ class Help { * @param {String} 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>') - 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 yesterday <artist>') - Help.say(say, where, '!list lastweek <artist>') - Help.say(say, where, '!list lastmonth <artist>') - Help.say(say, where, '!list lastyear <artist>') + 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>') } /** @@ -192,11 +152,11 @@ class Help { * @param {String} 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') - 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') + 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') } } diff --git a/libs/List.js b/libs/List.js index 829d09e..9edf055 100644 --- a/libs/List.js +++ b/libs/List.js @@ -30,7 +30,6 @@ 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 accc9ee..c35f122 100644 --- a/libs/Queries.js +++ b/libs/Queries.js @@ -71,10 +71,9 @@ class Queries { } break case 'lastday': - case 'yesterday': this.period = { - $gte: moment().subtract(1, 'day').startOf('day'), - $lte: moment().subtract(1, 'day').endOf('day') + $gte: moment().subtract('day').startOf('day'), + $lte: moment().subtract('day').endOf('day') } break case 'lastweek': diff --git a/libs/Statistics.js b/libs/Statistics.js index 510bbd2..83c2058 100644 --- a/libs/Statistics.js +++ b/libs/Statistics.js @@ -30,7 +30,6 @@ 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 e8b40a1..a8e63c8 100644 --- a/libs/When.js +++ b/libs/When.js @@ -31,7 +31,6 @@ class When extends Queries { case 'month': case 'year': case 'lastday': - case 'yesterday': case 'lastweek': case 'lastmonth': case 'lastyear':