Fixed bug for parse JSON
This commit is contained in:
parent
0e1ebea121
commit
af307b5888
1 changed files with 29 additions and 9 deletions
38
index.js
38
index.js
|
@ -27,11 +27,12 @@ if (process.env.STREAM_PATH_ARTIST) {
|
||||||
artistPath = process.env.STREAM_PATH_ARTIST.split('.')
|
artistPath = process.env.STREAM_PATH_ARTIST.split('.')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stockage du morceau en cours de lecture
|
// Sauvegarde temporaire du morceau en cours de lecture
|
||||||
let currentSong = {
|
let currentSong = {
|
||||||
artist: null,
|
artist: null,
|
||||||
title: null
|
title: null
|
||||||
}
|
}
|
||||||
|
// Sauvegarde des précédents morceaux (des 2 derniers à cause d'un bug sur Heavy1)
|
||||||
const previousSongs = [
|
const previousSongs = [
|
||||||
{
|
{
|
||||||
artist: null,
|
artist: null,
|
||||||
|
@ -51,6 +52,31 @@ const bot = new irc.Client(process.env.IRC_SERVER, process.env.IRC_NICKNAME, {
|
||||||
channels: [process.env.IRC_CHANNEL]
|
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 displayCurrentSong = () => {
|
||||||
// Just for debug
|
// Just for debug
|
||||||
console.info(`Now playing: ${currentSong.artist} - ${currentSong.title}`)
|
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) {
|
switch (process.env.STREAM_TYPE) {
|
||||||
case 'json':
|
case 'json':
|
||||||
title = res[titlePath[0]]
|
title = extractInfoFromJSON(res, titlePath)
|
||||||
for (let i = 1; i < titlePath.length; i += 1) {
|
artist = extractInfoFromJSON(res, artistPath)
|
||||||
title = title[titlePath[i]]
|
|
||||||
}
|
|
||||||
artist = res[artistPath[0]]
|
|
||||||
for (let i = 1; i < artistPath.length; i += 1) {
|
|
||||||
artist = artist[artistPath[i]]
|
|
||||||
}
|
|
||||||
|
|
||||||
currentSong = {
|
currentSong = {
|
||||||
title: title,
|
title: title,
|
||||||
|
|
Loading…
Reference in a new issue