Merge pull request #2 from Maathias/fixes

Fixes
This commit was merged in pull request #2.
This commit is contained in:
Mateusz Pstrucha authored and GitHub committed 2021-11-03 23:17:32 +01:00
commit 7a8a2d6f5d
5 files changed
+95 -48

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
import dotenv from 'dotenv'
const config = dotenv.config()
export default config
+40 -26
View File
@@ -1,4 +1,4 @@
import dotenv from 'dotenv'
import env from './env.js'
import chalk from 'chalk'
import { player, queueCommand } from './modules/playback.js'
@@ -7,16 +7,14 @@ import { now, durations, seconds } from './modules/timings.js'
import Track from './Track.js'
dotenv.config()
const defaultVolume = parseInt(process.env.VOLUME_DEFAULT),
offset = Number(process.env.OFFSET),
verbose = Number(process.env.VERBOSE)
const defaultVolume = parseInt(process.env.VOLUME_DEFAULT ?? 100),
offset = Number(process.env.OFFSET ?? 0),
verbose = Number(process.env.VERBOSE ?? 0)
function play(track) {
queueCommand('volume', [defaultVolume])
queueCommand('change', [track.url])
player.play()
queueCommand('volume', [defaultVolume])
updateStatus(track.tid, 0, track.duration, false)
}
@@ -27,8 +25,14 @@ function sToHM(seconds) {
return [h, m]
}
function pad(n) {
return n < 10 ? '0' + n : n
}
console.info(
`Volume: ${chalk.yellow(defaultVolume)}, Verbose: ${chalk.yellow(verbose)}`
`Volume: ${chalk.yellow(defaultVolume)}, Verbose: ${chalk.yellow(
verbose
)}, Limit: ${chalk.yellow(process.env.LIMIT)}`
)
console.info(`Fetching playlist (${chalk.yellow(process.env.DOMAIN)})`)
@@ -82,26 +86,30 @@ getTop()
let [h, m] = sToHM(currentSeconds + sched)
console.info(
`scheduling ${track.tid} @ ${chalk.red(`${h}:${m}`)} in ${sched}s`
`scheduling ${track.tid} @ ${chalk.red(
`${h}:${pad(m)}`
)} in ${sched}s`
)
console.info(
` (${chalk.cyan((track.duration / 60e3).toFixed(1))}) [${chalk.blue(
`╚═ (${chalk.cyan((track.duration / 60e3).toFixed(1))}) [${chalk.blue(
track.title.slice(0, 50)
)}]`
)
if (sched < 0) return
if (sched < -10) return
setTimeout(() => {
console.info(
`${chalk.magenta(`${h}:${m}`)} playing ${track.tid} (${track.ytid})`
`${chalk.magenta(`${h}:${pad(m)}`)} playing ${track.tid} (${
track.ytid
})`
)
console.info(
`(${chalk.cyan((track.duration / 60e3).toFixed(1))}) ${chalk.blue(
track.title.slice(0, 50)
)} `
` ╚ (${chalk.cyan(
(track.duration / 60e3).toFixed(1)
)}) ${chalk.blue(track.title.slice(0, 50))} `
)
play(track)
@@ -117,22 +125,28 @@ getTop()
let endin = end - currentSeconds,
[h, m] = sToHM(end)
console.info(`End of playback #${nthPrzerwa} @ ${h}:${m}`)
console.info(`» End of playback #${nthPrzerwa} @ ${h}:${pad(m)}`)
setTimeout(() => {
console.info(`Ending playback for #${nthPrzerwa}`)
player.fadeOut(6)
updateStatus(null, null, null, true)
if (nthPrzerwa == seconds.length - 1) {
console.info(`--- Day Ended ---`)
if (nthPrzerwa == seconds.length - 1) {
setTimeout(() => {
console.info(`≡≡≡ Day Ended ≡≡≡`)
setTimeout(() => {
process.exit(0)
}, 20e3)
}
}, 10e3)
}, endin * 1e3)
}
if (endin < 0) continue
setTimeout(() => {
console.info(`« Ending playback for #${nthPrzerwa} »\n`)
player.fadeOut(6)
updateStatus(null, null, null, true)
}, endin * 1e3)
}
player.pause()
console.info(`\nTrack scheduling done`)
console.info(`_______________________________`)
console.info(`─────────────────────────`)
})
+11 -7
View File
@@ -1,13 +1,11 @@
import chalk from 'chalk'
import dotenv from 'dotenv'
import got from 'got'
import spotifyToYt from 'spotify-to-yt'
dotenv.config()
const { DOMAIN, SECRET } = process.env,
headers = { authorization: 'Bearer ' + SECRET },
verbose = Number(process.env.VERBOSE)
verbose = Number(process.env.VERBOSE),
limit = Number(process.env.LIMIT ?? 40)
function updateStatus(tid, progress, duration, paused) {
return new Promise((resolve, reject) => {
@@ -53,11 +51,17 @@ function getTop() {
})
.json()
.then((top) => {
console.info(`Resolving ${chalk.cyan(top.length)} tracks`)
console.info(
`Resolving ${chalk.cyan(
top.length > limit ? limit + '/' + top.length : top.length
)} tracks`
)
Promise.all(
top.map((tid) => spotifyToYt.trackGet(`spotify:track:${tid}`))
top
.slice(0, limit)
.map((tid) => spotifyToYt.trackGet(`spotify:track:${tid}`))
).then((yts) => {
top.map((tid, i) => {
top.slice(0, limit).map((tid, i) => {
yts[i].tid = tid
})
resolve(yts)
+2 -2
View File
@@ -14,7 +14,7 @@ radio.stdout.on('data', (data) => {
for (let line of lines) {
let [status, ...data] = JSON.parse(line)
verbose > 0 && console.info(' -> ', status, data)
verbose > 0 && console.info(' < ', status, data)
if (status == 'waiting')
if (data[0]) {
@@ -35,7 +35,7 @@ radio.on('close', (code) => {
})
function sendCommand(comm, data = [null]) {
verbose > 1 && console.info(' <- ', comm, data)
verbose > 1 && console.info(' > ', comm, data)
busy = true
radio.stdin.write(JSON.stringify(['command', comm, ...data]) + '\n')
}
+37 -13
View File
@@ -2,6 +2,7 @@ from dotenv import load_dotenv
import vlc
import pafy
import requests
import json
from time import sleep
@@ -17,19 +18,19 @@ def info(type, data):
def play(args):
player.play()
info('ok', ['playing', True])
info('play', ['done'])
def pause(args):
player.pause()
info('ok', ['pause', True])
info('pause', ['done'])
def volume(args):
vol, *args = args
player.audio_set_volume(vol)
info('ok', ['volume', vol])
result = player.audio_set_volume(vol)
info('volume', ['set', vol, result])
def fadeOut(args):
@@ -41,8 +42,8 @@ def fadeOut(args):
player.audio_set_volume(v)
sleep(duration / vol)
stop()
info('ok', ['faded out', True])
pause([])
info('fadeOut', ['done'])
def fadeIn(args):
@@ -54,7 +55,7 @@ def fadeIn(args):
player.audio_set_volume(v)
sleep(duration / target)
info('ok', ['faded in', True])
info('fadeIn', ['done'])
def close(args):
@@ -63,20 +64,43 @@ def close(args):
def change(args):
url, *args = args
target, *args = args
def get(url):
info('change', ['get', url])
audio = pafy.new(url)
best = audio.getbestaudio(preftype="webm")
return best.url
def check(url):
info('change', ['check', url])
try:
r = requests.head(url)
info('change', ['check', r.status_code])
return r.status_code
except requests.ConnectionError:
info('change', ['check', 'failed'])
return 600
playurl = get(target)
tries = 0
while check(playurl) >= 400 and tries < 10:
playurl = get(target)
tries += 1
if tries == 10:
return info('change', ['failed'])
audio = pafy.new(url)
best = audio.getbestaudio(preftype="webm")
playurl = best.url
Media = Instance.media_new(playurl)
# Media.get_mrl()
player.set_media(Media)
info('ok', ['changed', url])
info('change', ['done', target])
def stop(args=[]):
player.stop()
info('ok', ['stop', True])
info('stop', ['done'])
def what(comm):