Last active
May 22, 2021 03:42
-
-
Save FoxCouncil/459f4e2093fde298f0498230d3579be4 to your computer and use it in GitHub Desktop.
A NodeJS based tool to pull NowPlaying data from AzuraCast and push it to Azure Blob Storage and Pusher notification.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config() | |
let oldCl = console.log; | |
console.log = function(msg) { | |
oldCl('[' + new Date().toISOString() + '] ' + msg); | |
} | |
const http = require('http'); | |
const i2b64 = require('image-to-base64'); | |
const storage = require('azure-storage'); | |
const blobService = storage.createBlobService(process.env.AZURE_BLOB); | |
const storageOptions = { contentSettings: { contentType: 'application/json' } }; | |
console.log('Starting Radio Tools...'); | |
console.log(process.env.TEST_VALUE); | |
let currentSongId = []; | |
function processNowPlaying() { | |
http.get(process.env.AZURA_SERVER_NP_URL, (resp) => { | |
let dataBuffer = ''; | |
resp.on('data', (chunk) => { | |
dataBuffer += chunk; | |
}); | |
resp.on('end', () => { | |
const stationData = JSON.parse(dataBuffer); | |
stationData.forEach((data) => { | |
const perfStartTime = Date.now(); | |
const sData = data.station; | |
const npData = data.now_playing.song; | |
const stationName = data.station.name.padStart(17); | |
if (currentSongId[sData.id] === npData.id) { | |
return; | |
} | |
currentSongId[sData.id] = npData.id | |
i2b64(npData.art).then((resp) => { | |
npData.art = 'data:image/jpeg;base64,' + resp; | |
delete npData.custom_fields; | |
delete npData.lyrics; | |
delete npData.id; | |
const stationCode = data.station.shortcode; | |
blobService.createBlockBlobFromText('radio', stationCode + '.json', JSON.stringify(npData), storageOptions, function(error, result, response) { | |
if (error) { | |
console.log(stationName + 'Couldn\'t upload nowplaying!'); | |
console.error(error); | |
} else { | |
console.log(stationName + ': ' + npData.text + ' ['+(Date.now() - perfStartTime).toString()+'ms]'); | |
// sendPusher(pusherData); | |
} | |
}); | |
}); | |
}); | |
}); | |
}).on('error', (err) => { | |
console.error('Error: ' + err.message); | |
}); | |
} | |
setInterval(function() { | |
processNowPlaying(); | |
}, 1000 * 10); // Ten Seconds | |
processNowPlaying(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment