Fixed bug on json parse
This commit is contained in:
parent
b6beab9a71
commit
d2ebf282aa
1 changed files with 41 additions and 36 deletions
77
index.js
77
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)
|
||||
|
|
Loading…
Reference in a new issue