From b7830190c6ce7d18516b324d2637641178eb3611 Mon Sep 17 00:00:00 2001 From: Alix von Schirp Date: Mon, 23 Jan 2023 21:41:46 +0100 Subject: [PATCH] Cover upload implemented closes #3 --- .../LastFMToSpotify.java | 4 +++- .../arguments/ArgumentHandler.java | 16 +++++++++++++ .../arguments/Arguments.java | 4 +++- .../util/FileHelper.java | 23 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/FileHelper.java diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java index 0a28505..ff1d2da 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java @@ -110,7 +110,9 @@ public class LastFMToSpotify { } } api.addItemsToPlaylist(list.getId(), adders.toArray(String[]::new)).build().execute(); - if(configuration.containsKey(configuration.get("playlist.cover"))) api.uploadCustomPlaylistCoverImage(list.getId()).image_data(configuration.get("playlist.cover")).build().execute(); + if(configuration.containsKey("playlist.cover")){ + logLn("Check for \"null\" if setting cover was successful: " + api.uploadCustomPlaylistCoverImage(list.getId()).image_data(configuration.get("playlist.cover")).setHeader("User-Agent", configuration.get("requests.useragent")).build().execute(),3); + } logLn("Done.", 1); // } catch (IOException | ParseException | SpotifyWebApiException e) { } catch (Exception e) { diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/ArgumentHandler.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/ArgumentHandler.java index aa8ef72..170ea2e 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/ArgumentHandler.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/ArgumentHandler.java @@ -1,10 +1,16 @@ package de.b00tload.tools.lastfmtospotifyplaylist.arguments; +import de.b00tload.tools.lastfmtospotifyplaylist.util.FileHelper; import de.umass.lastfm.Period; import org.jetbrains.annotations.Nullable; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; + import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.LINE_SEPERATOR; import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.configuration; +import static de.b00tload.tools.lastfmtospotifyplaylist.util.Logger.logLn; public class ArgumentHandler { @@ -21,6 +27,7 @@ public class ArgumentHandler { case QUARTERLY -> period(Period.THREE_MONTHS); case BIANNUALLY -> period(Period.SIX_MONTHS); case YEARLY -> period(Period.TWELVE_MONTHS); + case COVER -> cover(value); } } @@ -111,4 +118,13 @@ public class ArgumentHandler { private static void period(Period value) { configuration.put("lastfm.period", value.getString()); } + + private static void cover(String value) { + if (value == null || value.equalsIgnoreCase("") || !Files.exists(Path.of(value.replace("\\", "//")))) { + System.out.println("--coverart must be provided with a path to a png file. Check usage: " + Arguments.COVER.getUsage()); + System.exit(500); + } + String base64 = FileHelper.encodeFileToBase64(new File(value.replace("\\", "//"))); + configuration.put("playlist.cover", base64); + } } diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/Arguments.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/Arguments.java index c8fcd00..2dfa0d9 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/Arguments.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/Arguments.java @@ -33,7 +33,9 @@ public enum Arguments { BIANNUALLY("biannually", "[Optional]" + LINE_SEPERATOR + "Creates a playlist from your top tracks from last half-year.", "--biannualy", "B"), YEARLY("yearly", "[Optional]" + LINE_SEPERATOR - + "Creates a playlist from your top tracks from last year.", "--anually", "A");; + + "Creates a playlist from your top tracks from last year.", "--anually", "A"), + COVER("coverart", "[Optional]" + LINE_SEPERATOR + + "Will set a cover art for the playlist. Must be jpeg/jpg.", "--coverart ", "ca", "cover"); private final String name; private final String description; diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/FileHelper.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/FileHelper.java new file mode 100644 index 0000000..e1ad05b --- /dev/null +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/FileHelper.java @@ -0,0 +1,23 @@ +package de.b00tload.tools.lastfmtospotifyplaylist.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Base64; + +public class FileHelper { + + public static String encodeFileToBase64(File file){ + String encodedfile = null; + try (FileInputStream fileInputStreamReader = new FileInputStream(file)){ + byte[] bytes = new byte[(int)file.length()]; + fileInputStreamReader.read(bytes); + encodedfile = Base64.getEncoder().encodeToString(bytes); + } catch (IOException e) { + e.printStackTrace(); + } + + return encodedfile; + } + +}