Last active
July 28, 2025 11:04
-
-
Save elvl/ce5493dcf12d57450989a56d7f969dca to your computer and use it in GitHub Desktop.
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
| import mpdApi from 'mpd-api' | |
| const withConnection = async (callback, host = 'raspberrypi.local', port = 6600) => { | |
| if (!callback) return | |
| let client | |
| try { | |
| client = await mpdApi.connect({ host, port }) | |
| return await callback(client) | |
| } finally { | |
| client?.disconnect() | |
| } | |
| } | |
| const getContext = async client => { | |
| const status = await client.api.status.get() | |
| const queue = await client.api.queue.info() | |
| return { status, queue } | |
| } | |
| const renderStatus = ctx => { | |
| const { status, queue } = ctx | |
| const keys = [ | |
| 'volume', | |
| 'repeat', | |
| 'random', | |
| 'consume', | |
| 'state', | |
| 'duration', | |
| 'elapsed', | |
| 'playlistlength', | |
| ] | |
| console.log('--- Status ---') | |
| console.log(keys.map(key => `${key}: ${status[key]}`).join(' | ')) | |
| const currentSong = queue.find(song => status.songid === song.id) | |
| if (status.state === 'play' && currentSong) { | |
| console.log(`\nNow Playing: ${currentSong.file}`) | |
| } | |
| if (queue.length > 0) { | |
| console.log('\n--- Queue ---') | |
| for (const song of queue) { | |
| const isPlaying = currentSong?.id === song.id ? '*' : ' ' | |
| console.log(`[${isPlaying}] ${song.file}`) | |
| } | |
| } else { | |
| console.log('Queue is empty.') | |
| } | |
| } | |
| const MPDError = mpdApi.mpd.MPDError | |
| const CODES = MPDError.CODES | |
| const ERROR_MESSAGE = new Map([ | |
| [CODES.NOT_LIST, 'Not List Error (unused)'], | |
| [CODES.ARG, 'Invalid arguments for the command'], | |
| [CODES.PASSWORD, 'Incorrect password'], | |
| [CODES.PERMISSION, 'Permission denied'], | |
| [CODES.UNKNOWN, 'Unknown error'], | |
| [CODES.NO_EXIST, 'The requested resource does not exist'], | |
| [CODES.PLAYLIST_MAX, 'Playlist is too large'], | |
| [CODES.SYSTEM, 'A system error occurred'], | |
| [CODES.PLAYLIST_LOAD, 'Failed to load playlist'], | |
| [CODES.UPDATE_ALREADY, 'Failed to update the database'], | |
| [CODES.PLAYER_SYNC, 'Player is not playing'], | |
| [CODES.EXIST, 'Already exists'], | |
| ]) | |
| const main = async () => { | |
| try { | |
| await withConnection(async client => { | |
| if (!client) return | |
| const ctx = await getContext(client) | |
| renderStatus(ctx) | |
| }) | |
| } catch (err) { | |
| if (err instanceof MPDError) { | |
| const message = ERROR_MESSAGE.get(err.errno) || 'Undefined MPD error' | |
| console.error(`MPD Error: ${message} (${err.message})`) | |
| } else if (err instanceof AggregateError) { | |
| const message = err.code?.includes('ECONNREFUSED') | |
| ? 'Failed to connect to MPD server:' | |
| : 'Multiple errors occurred:' | |
| console.error(message, err.errors.map(e => e.message).join(', ')) | |
| } else { | |
| console.error('An unexpected error occurred:', err) | |
| } | |
| } | |
| } | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment