irc-radio-bot/index.js

220 lines
6.1 KiB
JavaScript
Raw Normal View History

2019-12-24 11:32:16 +01:00
const radio = require('radio-stream')
2019-12-29 19:08:35 +01:00
const request = require('request')
2019-12-24 11:32:16 +01:00
const irc = require('irc-upd')
const models = require('./models').models
const Help = require('./libs/Help')
2019-12-24 16:30:45 +01:00
const ArtistSong = require('./libs/ArtistSong')
2019-12-25 12:47:09 +01:00
const When = require('./libs/When')
2019-12-25 13:10:13 +01:00
const Statistics = require('./libs/Statistics')
2019-12-25 13:47:42 +01:00
const List = require('./libs/List')
2020-01-02 13:58:41 +01:00
const Preferences = require('./libs/Preferences')
2019-12-24 11:32:16 +01:00
2019-12-24 16:30:45 +01:00
// Init des librairies
const artistSong = new ArtistSong(models)
2019-12-25 12:47:09 +01:00
const when = new When(models)
2019-12-25 13:10:13 +01:00
const stats = new Statistics(models)
2019-12-25 13:47:42 +01:00
const list = new List(models)
2020-01-02 13:58:41 +01:00
const preferences = new Preferences(models)
2019-12-24 11:32:16 +01:00
2020-01-06 20:14:12 +01:00
let titlePath = null
let artistPath = null
if (process.env.STREAM_PATH_TITLE) {
titlePath = process.env.STREAM_PATH_TITLE.split('.')
}
if (process.env.STREAM_PATH_ARTIST) {
artistPath = process.env.STREAM_PATH_ARTIST.split('.')
}
2019-12-24 16:30:45 +01:00
// Stockage du morceau en cours de lecture
let currentSong = {
artist: null,
title: null
}
2019-12-29 19:08:35 +01:00
let previousSong = {
artist: null,
title: null
}
2019-12-24 16:30:45 +01:00
// Initialisation du bot IRC
2019-12-24 11:32:16 +01:00
const bot = new irc.Client(process.env.IRC_SERVER, process.env.IRC_NICKNAME, {
userName: process.env.IRC_USER_NAME,
realName: process.env.IRC_REAL_NAME,
password: process.env.IRC_PASSWORD,
channels: [process.env.IRC_CHANNEL]
})
2019-12-29 19:08:35 +01:00
const checkCurrentSong = () => {
previousSong = currentSong
// Just for debug
console.info(`Now playing: ${currentSong.artist} - ${currentSong.title}`)
// Publish song on IRC channel
bot.say(process.env.IRC_CHANNEL, `${process.env.RADIO_ALIAS} : ${currentSong.artist} - ${currentSong.title}`)
// Save song for history
2019-12-30 13:36:15 +01:00
artistSong.saveCurrent({
2019-12-29 19:08:35 +01:00
radio: process.env.RADIO_ALIAS,
artist: currentSong.artist,
title: currentSong.title
})
// Notify some people ?
artistSong.notify(botSay, process.env.IRC_CHANNEL, currentSong)
}
// Initialisation du stream radio
if (!process.env.STREAM_TYPE || process.env.STREAM_TYPE === 'radio') {
const stream = radio.createReadStream(process.env.STREAM_URL)
stream.on('connect', function () {
console.info('Radio Stream connected!')
console.info(stream.headers)
})
// Listener on new song
stream.on('metadata', function (data) {
2019-12-29 21:31:23 +01:00
console.log('RAW DATA:', data)
// let song = data.replace(/[^A-Za-z 0-9 .,?""!@#$%^&*()-_=+;:<>/\\|}{[\]`~]*/g, '')
2019-12-30 10:07:04 +01:00
const song = data.substring(13, data.length - 1).replace(/\0/g, '').replace('\';', '')
2019-12-29 19:08:35 +01:00
// Extract artist = title from song
const splitted = song.split(' - ')
const artist = splitted[0]
const title = splitted[1]
currentSong = {
title: title,
artist: artist
}
checkCurrentSong()
})
} else {
setInterval(() => {
request({
method: 'GET',
url: process.env.STREAM_URL
},
(error, response, body) => {
if (!error && response.statusCode === 200) {
2020-01-06 20:14:12 +01:00
const res = JSON.parse(process.env.STREAM_PARSE ? body.replace('updateFromMediaItem(', '').replace(');', '') : body)
let title = null
let artist = null
2020-01-02 09:51:01 +01:00
switch (process.env.STREAM_TYPE) {
case 'json':
2020-01-06 20:14:12 +01:00
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]]
}
2020-01-02 09:51:01 +01:00
currentSong = {
2020-01-06 20:14:12 +01:00
title: title,
artist: artist
2020-01-02 09:51:01 +01:00
}
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
2019-12-29 19:08:35 +01:00
}
if (previousSong.title !== currentSong.title && previousSong.artist !== currentSong.artist) {
checkCurrentSong()
}
} else {
console.error('ERR:', error)
}
})
}, 4000)
}
2019-12-24 11:32:16 +01:00
2019-12-24 16:30:45 +01:00
// Gestion des erreurs du bot IRC
2019-12-24 11:32:16 +01:00
bot.addListener('error', function (message) {
console.error('ERR:', message)
})
2019-12-24 16:30:45 +01:00
/**
* Fonction permettant de publier un message sur IRC
* @param {String} where
* @param {String} what
*/
2019-12-24 11:32:16 +01:00
const botSay = (where, what) => {
bot.say(where, what)
}
2019-12-24 16:30:45 +01:00
/**
* Gestion des actions
* @param {String} where
* @param {String} message
* @param {String} from
*/
const actions = (where, message, from) => {
2019-12-24 11:32:16 +01:00
const exploded = message.split(' ')
// Le message publié est pour le bot
if (exploded[0].indexOf(process.env.IRC_NICKNAME) === 0) {
2020-01-07 09:57:58 +01:00
switch (exploded[1]) {
case '!artist':
case '!song':
artistSong.action(exploded[1].replace('!', ''), botSay, from, currentSong, exploded)
break
case '!help':
Help.show(botSay, where, exploded)
break
case '!when':
when.action(botSay, where, exploded)
break
case '!stats':
stats.action(botSay, where, exploded)
break
case '!list':
list.action(botSay, where, exploded)
break
case '!notifications':
preferences.notifications(botSay, where, from, exploded)
break
case '!stream':
botSay(where, `${process.env.RADIO_ALIAS} : ${process.env.PUBLIC_RADIO_STREAM}`)
break
default:
botSay(where, 'Commande inconnue, tape !help si tu es perdu')
break
2019-12-24 11:32:16 +01:00
}
}
}
// Listener for public message
bot.addListener(`message${process.env.IRC_CHANNEL}`, function (from, to, message) {
2019-12-24 16:30:45 +01:00
actions(process.env.IRC_CHANNEL, message.args.splice(1).join(' '), from)
2019-12-24 11:32:16 +01:00
})
// Listener for private message
bot.addListener('pm', function (from, to, message) {
2019-12-24 16:30:45 +01:00
actions(from, message.args.join(' '), from)
2019-12-24 11:32:16 +01:00
})
2019-12-25 09:40:18 +01:00
// Say hello!
if (process.env.SAY_HELLO && process.env.SAY_HELLO === 'true') {
bot.addListener('join', (channel, who) => {
// Welcome them in!
if (who !== process.env.IRC_NICKNAME) {
botSay(process.env.IRC_CHANNEL, 'Hello ' + who + '! Have a metal day! \\m/(-.-)\\m/')
}
})
}