From 7a2e66e61468651a11a5224a53e7b518877fe635 Mon Sep 17 00:00:00 2001 From: Maxim Date: Thu, 21 Nov 2024 03:20:37 +0400 Subject: [PATCH] save track by button * L by default --- .../one/clownless/blockify/BlockifyMain.java | 22 +++++++ .../clownless/blockify/util/SpotifyUtil.java | 64 ++++++++++++++++++- .../resources/assets/blockify/lang/en_us.json | 1 + 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/main/java/one/clownless/blockify/BlockifyMain.java b/src/main/java/one/clownless/blockify/BlockifyMain.java index 0d017ef..94fd55e 100644 --- a/src/main/java/one/clownless/blockify/BlockifyMain.java +++ b/src/main/java/one/clownless/blockify/BlockifyMain.java @@ -26,6 +26,7 @@ public class BlockifyMain implements ModInitializer private static KeyBinding nextKey; private static KeyBinding prevKey; private static KeyBinding forceKey; + private static KeyBinding addTrackKey; private static KeyBinding hideKey; private static KeyBinding increaseVolumeKey; private static KeyBinding decreaseVolumeKey; @@ -34,6 +35,7 @@ public class BlockifyMain implements ModInitializer private boolean nextKeyPrevState = false; private boolean prevKeyPrevState = false; private boolean forceKeyPrevState = false; + private boolean addTrackKeyPrevState = false; private boolean hideKeyPrevState = false; private boolean increaseVolumeKeyPrevState = false; private boolean decreaseVolumeKeyPrevState = false; @@ -110,6 +112,15 @@ public void run() ) ); + addTrackKey = KeyBindingHelper.registerKeyBinding( + new KeyBinding( + "blockify.key.addTrack", + InputUtil.Type.KEYSYM, + GLFW.GLFW_KEY_L, + "Blockify" + ) + ); + forceKey = KeyBindingHelper.registerKeyBinding( new KeyBinding( "blockify.key.force", @@ -164,6 +175,7 @@ public void run() nextKeyHandler(nextKey.isPressed()); prevKeyHandler(prevKey.isPressed()); forceKeyHandler(forceKey.isPressed()); + addTrackKeyHandler(addTrackKey.isPressed()); hideKeyHandler(hideKey.isPressed()); increaseVolumeKeyHandler(increaseVolumeKey.isPressed()); decreaseVolumeKeyHandler(decreaseVolumeKey.isPressed()); @@ -230,6 +242,16 @@ public void prevKeyHandler(boolean currPressState) prevKeyPrevState = currPressState; } + public void addTrackKeyHandler(boolean currPressState) + { + if (currPressState && !addTrackKeyPrevState) + { + LOGGER.info("Add Track Key Pressed"); + SpotifyUtil.addTrack(); + } + addTrackKeyPrevState = currPressState; + } + public void forceKeyHandler(boolean currPressState) { if (currPressState && !forceKeyPrevState) diff --git a/src/main/java/one/clownless/blockify/util/SpotifyUtil.java b/src/main/java/one/clownless/blockify/util/SpotifyUtil.java index 96073f6..7a6e351 100644 --- a/src/main/java/one/clownless/blockify/util/SpotifyUtil.java +++ b/src/main/java/one/clownless/blockify/util/SpotifyUtil.java @@ -105,7 +105,7 @@ public static String authorize() authURI.append("&response_type=code"); authURI.append("&redirect_uri=http%3A%2F%2Flocalhost%3A8001%2Fcallback"); authURI.append("&scope=user-read-playback-state%20user-read-currently-playing"); - authURI.append("%20user-modify-playback-state"); + authURI.append("%20user-modify-playback-state%20user-library-modify"); authURI.append("&code_challenge_method=S256"); verifier = PKCEUtil.generateCodeVerifier(); challenge = PKCEUtil.generateCodeChallenge(verifier); @@ -243,6 +243,57 @@ public static void refreshActiveSession() LOGGER.info("Successfully refreshed active session"); } + public static void addTrack(String track_id) + { + try + { + HttpRequest putReq = HttpRequest.newBuilder(new URI("https://api.spotify.com/v1/me/tracks?ids=" + track_id)) + .PUT(HttpRequest.BodyPublishers.ofString("")) + .header("Authorization", "Bearer " + accessToken).build(); + HttpResponse putRes = client.send(putReq, HttpResponse.BodyHandlers.ofString()); + LOGGER.info("Add Track Request (" + track_id + "): " + putRes.statusCode()); + if (putRes.statusCode() == 200) + { + MinecraftClient.getInstance().player.sendMessage(Text.of("Track Saved!"), true); + } + else if (putRes.statusCode() == 404) + { + refreshActiveSession(); + LOGGER.info("Retrying put request..."); + putRes = client.send(putReq, HttpResponse.BodyHandlers.ofString()); + LOGGER.info("Add Track Request (" + track_id + "): " + putRes.statusCode()); + } + else if (putRes.statusCode() == 403) + { + MinecraftClient.getInstance().player.sendMessage(Text.of("Spotify Premium is required for this feature."), false); + } + else if (putRes.statusCode() == 401) + { + if (refreshAccessToken()) + { + addTrack(track_id); + } + else + { + isAuthorized = false; + } + } + } catch (IOException | InterruptedException | URISyntaxException e) + { + if (e instanceof IOException && e.getMessage().equals("Connection reset")) + { + LOGGER.info("Attempting to retry put request..."); + addTrack(track_id); + LOGGER.info("Successfully sent put request"); + } + else + { + LOGGER.error(e.getMessage()); + } + } + + } + public static void putRequest(String type) { try @@ -344,6 +395,13 @@ public static void nextSong() }); } + public static void addTrack() + { + EXECUTOR_SERVICE.execute(() -> { + addTrack(getPlaybackInfo()[7]); + }); + } + public static void prevSong() { EXECUTOR_SERVICE.execute(() -> { @@ -382,7 +440,7 @@ public static void playPause() public static String[] getPlaybackInfo() { - String[] results = new String[7]; + String[] results = new String[8]; try { playbackResponse = client.send(playbackRequest, HttpResponse.BodyHandlers.ofString()); @@ -401,6 +459,7 @@ public static String[] getPlaybackInfo() results[2] = json.get("progress_ms").getAsString(); results[3] = json.get("item").getAsJsonObject().get("duration_ms").getAsString(); results[4] = json.get("item").getAsJsonObject().get("images").getAsJsonArray().get(1).getAsJsonObject().get("url").getAsString(); + results[5] = json.get("item").getAsJsonObject().get("id").getAsString(); return results; } results[0] = json.get("item").getAsJsonObject().get("name").getAsString(); @@ -433,6 +492,7 @@ public static String[] getPlaybackInfo() } results[5] = json.get("item").getAsJsonObject().get("external_urls").getAsJsonObject().get("spotify").getAsString(); results[6] = json.get("device").getAsJsonObject().get("volume_percent").getAsString(); + results[7] = json.get("item").getAsJsonObject().get("id").getAsString(); isPlaying = json.get("is_playing").getAsBoolean(); } else if (playbackResponse.statusCode() == 401) diff --git a/src/main/resources/assets/blockify/lang/en_us.json b/src/main/resources/assets/blockify/lang/en_us.json index b957873..03f4bac 100644 --- a/src/main/resources/assets/blockify/lang/en_us.json +++ b/src/main/resources/assets/blockify/lang/en_us.json @@ -4,6 +4,7 @@ "blockify.key.next": "Next Song", "blockify.key.prev": "Previous Song", "blockify.key.force": "Force Update", + "blockify.key.addTrack": "Save track", "blockify.key.hide": "Hide Spotify", "blockify.key.increaseVolume": "Increase Volume", "blockify.key.decreaseVolume": "Decrease Volume",