diff --git a/index.js b/index.js index 72e5e2f..4e45fbc 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) @@ -328,7 +296,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..7ef6017 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) }