From 0641921fec7882196a73bf743fcbb538ece4c8f2 Mon Sep 17 00:00:00 2001 From: Ian Wijma Date: Sat, 18 Nov 2023 01:18:52 +1100 Subject: [PATCH] Volume up and down --- src/commands/authenticate.js | 2 +- src/commands/volumeDown.js | 17 +++++++++ src/commands/volumeUp.js | 17 +++++++++ tmp/state-data | 72 ++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/commands/volumeDown.js create mode 100644 src/commands/volumeUp.js create mode 100644 tmp/state-data diff --git a/src/commands/authenticate.js b/src/commands/authenticate.js index 97a7127..36642d8 100644 --- a/src/commands/authenticate.js +++ b/src/commands/authenticate.js @@ -5,7 +5,7 @@ const {nanoid} = require("nanoid"); const express = require('express') const {spotify, spotifyCredentialsFile, hasSpotifyCredentials, saveSpotifyCredentials} = require("../utilities/spotify"); -exports.command = 'authenticate' +exports.command = ['authenticate', 'auth'] exports.description = 'starts the Spotity authentication process' exports.builder = (argv) => argv .option('client-id', { diff --git a/src/commands/volumeDown.js b/src/commands/volumeDown.js new file mode 100644 index 0000000..b16b6fc --- /dev/null +++ b/src/commands/volumeDown.js @@ -0,0 +1,17 @@ +const { spotify} = require('../utilities/spotify') + +exports.command = 'volume-down' +exports.handler = async () => { + try { + await spotify.ensure(); + const { body } = await spotify.getMyCurrentPlaybackState(); + const { device = {} } = body + const { supports_volume = false, volume_percent = 100 } = device; + + if (supports_volume) { + await spotify.setVolume(Math.min(Math.max(volume_percent - 10, 0), 100)); + } + } catch (e) { + console.error(e.message); + } +}; \ No newline at end of file diff --git a/src/commands/volumeUp.js b/src/commands/volumeUp.js new file mode 100644 index 0000000..638c67f --- /dev/null +++ b/src/commands/volumeUp.js @@ -0,0 +1,17 @@ +const { spotify} = require('../utilities/spotify') + +exports.command = 'volume-up' +exports.handler = async () => { + try { + await spotify.ensure(); + const { body } = await spotify.getMyCurrentPlaybackState(); + const { device = {} } = body + const { supports_volume = false, volume_percent = 100 } = device; + + if (supports_volume) { + await spotify.setVolume(Math.min(Math.max(volume_percent + 10, 0), 100)); + } + } catch (e) { + console.error(e.message); + } +}; \ No newline at end of file diff --git a/tmp/state-data b/tmp/state-data new file mode 100644 index 0000000..3f56691 --- /dev/null +++ b/tmp/state-data @@ -0,0 +1,72 @@ +{ + device: { + id: 'acc9f0df86034061b5d026e606ea1a6976f09298', + is_active: true, + is_private_session: false, + is_restricted: false, + name: 'pop-os', + supports_volume: true, + type: 'Computer', + volume_percent: 46 + }, + shuffle_state: true, + repeat_state: 'off', + timestamp: 1700230542949, + context: { + external_urls: { spotify: 'https://open.spotify.com/collection/tracks' }, + href: 'https://api.spotify.com/v1/me/tracks', + type: 'collection', + uri: 'spotify:user:1150564340:collection' + }, + progress_ms: 96296, + item: { + album: { + album_type: 'single', + artists: [Array], + available_markets: [Array], + external_urls: [Object], + href: 'https://api.spotify.com/v1/albums/57d2Ti8vswyMFDpz4xauZw', + id: '57d2Ti8vswyMFDpz4xauZw', + images: [Array], + name: 'Wall Of Bass', + release_date: '2018-04-13', + release_date_precision: 'day', + total_tracks: 1, + type: 'album', + uri: 'spotify:album:57d2Ti8vswyMFDpz4xauZw' + }, + artists: [ [Object], [Object] ], + available_markets: [ + 'AR', 'AU', 'AT', 'BE', 'BO', 'BR', 'BG', 'CA', 'CL', 'CO', + 'CR', 'CY', 'CZ', 'DK', 'DO', 'DE', 'EC', 'EE', 'SV', 'FI', + 'FR', 'GR', 'GT', 'HN', 'HK', 'HU', 'IS', 'IE', 'IT', 'LV', + 'LT', 'LU', 'MY', 'MT', 'MX', 'NL', 'NZ', 'NI', 'NO', 'PA', + 'PY', 'PE', 'PH', 'PL', 'PT', 'SG', 'SK', 'ES', 'SE', 'CH', + 'TW', 'TR', 'UY', 'US', 'GB', 'AD', 'LI', 'MC', 'ID', 'JP', + 'TH', 'VN', 'RO', 'IL', 'ZA', 'SA', 'AE', 'BH', 'QA', 'OM', + 'KW', 'EG', 'MA', 'DZ', 'TN', 'LB', 'JO', 'PS', 'IN', 'BY', + 'KZ', 'MD', 'UA', 'AL', 'BA', 'HR', 'ME', 'MK', 'RS', 'SI', + 'KR', 'BD', 'PK', 'LK', 'GH', 'KE', 'NG', 'TZ', 'UG', 'AG', + ... 84 more items + ], + disc_number: 1, + duration_ms: 264736, + explicit: true, + external_ids: { isrc: 'NLQS91800057' }, + external_urls: { + spotify: 'https://open.spotify.com/track/6MM8Rkumhy4RguXgtHCVk3' + }, + href: 'https://api.spotify.com/v1/tracks/6MM8Rkumhy4RguXgtHCVk3', + id: '6MM8Rkumhy4RguXgtHCVk3', + is_local: false, + name: 'Wall Of Bass', + popularity: 37, + preview_url: 'https://p.scdn.co/mp3-preview/7c27853ef9f45167de007e32a3ad39750c90ba4e?cid=0974061c80f44d3b9a3bc9b6b42b5820', + track_number: 1, + type: 'track', + uri: 'spotify:track:6MM8Rkumhy4RguXgtHCVk3' + }, + currently_playing_type: 'track', + actions: { disallows: { resuming: true } }, + is_playing: true +} \ No newline at end of file