Created
February 5, 2018 14:00
-
-
Save mhingston/6bb2fbb42559a25f2d8f57cce32315b5 to your computer and use it in GitHub Desktop.
Use ACRCloud (https://www.acrcloud.com) to identify songs from a digital radio station and email you when the same artist plays 3 times in a row.
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
/* | |
Use ACRCloud (https://www.acrcloud.com) to identify songs from a digital radio station and email you when the same artist plays 3 times in a row which could be useful for certain radio competitions (e.g. http://www.heart.co.uk/radio/win-with-hearts-30k-triple-play/). This is just a proof on concept. | |
npm init -y | |
npm install --save lodash mailgun-js express body-parser | |
1) Edit the config as below, you'll need a Mailgun API key for sending emails. | |
2) Run this app server behind a reverse proxy like NGINX. | |
3) Setup broadcast monitoring on ACRCloud. | |
4) After you have added a project to listen to a stream, setup a result callback with a post type of JSON to the URL where your app server is being served. | |
*/ | |
// Begin - Edit the config | |
const apiKey = 'key-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; | |
const domain = 'yourdomain.com'; | |
const from = `acrcloud@${domain}`; | |
const to = '[email protected]'; | |
const subject = 'Your Subject Goes Here'; | |
const artist = 'Justin Timberlake'; | |
const smsText = 'PLAY'; | |
const shortCode = '82122'; | |
// End - Edit the config | |
const _ = require('lodash'); | |
const mailgun = require('mailgun-js')({apiKey, domain}); | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const items = []; | |
const app = express(); | |
app.use(bodyParser.json()); | |
const sendEmail = () => | |
{ | |
const data = | |
{ | |
from, | |
to, | |
subject, | |
text: `${artist} has played three times in a row! Text ${smsText} to ${shortCode} now for a chance to win. Ignore this email if you don\'t see it straight away.` | |
}; | |
mailgun.messages().send(data, (error, body) => | |
{ | |
console.log(body); | |
}); | |
} | |
app.post('/', (req, res) => | |
{ | |
if(_.get(req.body, 'data.status.msg') === 'Success') | |
{ | |
const re = new RegExp(artist, 'i'); | |
const timestamp = new Date(_.get(req.body, 'data.metadata.timestamp_utc')); | |
if(timestamp.getHours() >= 10 && timestamp.getHours() <= 15) // between 10am and 4pm | |
{ | |
_.forEach(_.get(req.body, 'data.metadata.music', []), (music) => | |
{ | |
const artists = []; | |
_.forEach(_.get(music, 'artists', []), (artist) => | |
{ | |
artists.push(artist.name); | |
}); | |
items.push( | |
{ | |
title: music.title, | |
artist: artists.join(', '), | |
album: _.get(music, 'album.name'), | |
timestamp: timestamp | |
}); | |
if(items.length > 3) | |
{ | |
items.shift(); | |
} | |
}); | |
if(items.length > 2 && re.test(items[items.length-3].artist) && re.test(items[items.length-2].artist) && re.test(items[items.length-1].artist)) // the artist has played 3 times | |
{ | |
sendEmail(); | |
} | |
} | |
} | |
res.json({status: 'OK'}); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment