« get me outta code hell

loop-play.js « src - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/loop-play.js
blob: e59fbc29108340ad3ae2b832a341613962887f76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict'

const fs = require('fs')

const { spawn } = require('child_process')
const { promisify } = require('util')
const fetch = require('node-fetch')
const sanitize = require('sanitize-filename')
const promisifyProcess = require('./promisify-process')

const writeFile = promisify(fs.writeFile)
const unlink = promisify(fs.unlink)

module.exports = async function loopPlay(fn) {
  // Looping play function. Takes one argument, the "pick" function,
  // which returns a track to play. Preemptively downloads the next
  // track while the current one is playing for seamless continuation
  // from one song to the next. Stops when the result of the pick
  // function is null (or similar).

  async function downloadNext() {
    const picked = fn()

    if (picked == null) {
      return false
    }

    const [ title, href ] = picked
    console.log(`Downloading ${title}..\n${href}`)

    const wavFile = `.${sanitize(title)}.wav`

    const res = await fetch(href)
    const buffer = await res.buffer()
    await writeFile('./.temp-track', buffer)

    try {
      await convert('./.temp-track', wavFile)
    } catch(err) {
      console.warn('Failed to convert ' + title)
      console.warn('Selecting a new track\n')

      return await downloadNext()
    }

    await unlink('./.temp-track')

    return wavFile
  }

  let wavFile = await downloadNext()

  while (wavFile) {
    const nextPromise = downloadNext()
    await playFile(wavFile)
    await unlink(wavFile)
    wavFile = await nextPromise
  }
}

function convert(fromFile, toFile) {
  const avconv = spawn('avconv', ['-y', '-i', fromFile, toFile])
  return promisifyProcess(avconv, false)
}

function playFile(file) {
  const play = spawn('play', [file])
  return promisifyProcess(play)
}