const fetch = require("node-fetch");
const URL = 'https://api.tunegenie.com/v2/brand/nowplaying/?apiid=m2g_bar&b=kqrs&count=1';
const fetchNowPlaying = async () => {
const response = await fetch(URL);
if (!response.status === 200) {
// todo - wire up some kind of error reporting/monitoring
const err = await response.text();
throw Error('Unable to fetch current song for KQRS: ' + err);
}
let responseData = response.json();
if (Array.isArray(responseData)) {
responseData = responseData[0];
}
const songInfo = {
artist: responseData.artist,
songTitle: responseData.song,
when: new Date().getTime()
};
return songInfo;
};
exports.endpoint = async function(request, response) {
const songInfo = await fetchNowPlaying();
response.end(JSON.stringify(songInfo));
}