const fetch = require("node-fetch");
// kdwb = stream 1201
const URL = 'https://us.api.iheart.com/api/v3/live-meta/stream/1201/trackHistory?limit=1'
const fetchNowPlaying = async () => {
const response = await fetch(URL);
let data = await response.json();
// Their response normally looks something like this:
/*
{"meta":{"offset":0,"pageSize":1,"totalSize":18888},"data":[{"artistId":59842,"albumId":51917650,"trackId":51917651,"title":"Hotel California","artist":"Eagles","album":"Hotel California","trackDuration":391,"imagePath":"http://image.iheart.com/ihr-ingestion-pipeline-production-wmg/new_release/20171109032050569/603497862559/resources/603497862559.jpg","explicitLyrics":false,"lyricsId":0,"startTime":1613681614,"endTime":1613681999,"playbackRights":{"onDemand":true},"dataSource":"Pnp"}],"links":{"next":"AAAAAGAu1RIAAAAAAAAAAQAAScg"}}
*/
if (data.data) {
data = data.data;
}
if (Array.isArray(data)) {
data = data[0];
}
const songInfo = {songTitle:data.title, artist: data.artist};
songInfo.when = new Date().getTime();
return songInfo;
};
exports.endpoint = async function(request, response) {
const songInfo = await fetchNowPlaying();
response.end(JSON.stringify(songInfo));
}