commit e326867ebd32699c9988ff203edaf2044607f62c Author: Alix von Schirp Date: Wed Jan 18 01:53:25 2023 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..744289d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Project exclude paths +/target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f3f5e33 --- /dev/null +++ b/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + de.b00tload.tools + LastfmToSpotifyPlaylist + 1.0-SNAPSHOT + + + 18 + 18 + UTF-8 + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.3.0 + + + package + + single + + + + + + jar-with-dependencies + + + + de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify + + + + + + + + + se.michaelthelin.spotify + spotify-web-api-java + 7.3.0 + + + de.u-mass + lastfm-java + 0.1.2 + + + io.javalin + javalin + 5.3.1 + + + me.tongfei + progressbar + 0.9.5 + + + + \ No newline at end of file diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java new file mode 100644 index 0000000..113c598 --- /dev/null +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java @@ -0,0 +1,86 @@ +package de.b00tload.tools.lastfmtospotifyplaylist; + + +import de.b00tload.tools.lastfmtospotifyplaylist.arguments.ArgumentHandler; +import de.b00tload.tools.lastfmtospotifyplaylist.arguments.Arguments; +import de.umass.lastfm.Caller; +import de.umass.lastfm.Period; +import de.umass.lastfm.User; +import me.tongfei.progressbar.ProgressBar; +import org.apache.hc.core5.http.ParseException; +import se.michaelthelin.spotify.SpotifyApi; +import se.michaelthelin.spotify.exceptions.SpotifyWebApiException; + +import java.io.IOException; +import java.net.URI; +import java.util.HashMap; + +public class LastFMToSpotify { + + public static final String LINE_SEPERATOR = System.getProperty("line.separator"); + public static HashMap configuration; + + public static void main(String[] args) { + configuration.put("requests.useragent", "LastFMToSpotify/1.0-Snapshot (" + System.getProperty("os.name") + "; " + System.getProperty("os.arch") + ") Java/"+System.getProperty("java.version")); + for(int a = 0; a Spotify Playlist", 4)) { + for (int progress = 1; progress<=5; progress++) { + pb.step(); // step by 1 + switch (progress) { + case 1: + pb.setExtraMessage("Authenticating with Spotify..."); + + break; + case 2: + pb.setExtraMessage("Authenticating with LastFM..."); + Caller.getInstance().setUserAgent(configuration.get("requests.useragent")); + User.getInfo(configuration.get("lastfm.user"), configuration.get("lastfm.apikey")).getName(); + break; + case 3: + pb.setExtraMessage("Reading from LastFM..."); + User.getTopTracks(configuration.get("lastfm.user"), Period.ONE_MONTH, configuration.get("lastfm.apikey")); + break; + case 4: + pb.setExtraMessage("Creating Playlist..."); + SpotifyApi.Builder build = SpotifyApi.builder(); + build.setClientId(configuration.get("spotify.clientid")); + build.setClientSecret(configuration.get("spotify.secret")); + build.setRedirectUri(URI.create("http://localhost:9876/callback/spotify/")); + SpotifyApi api = build.build(); + api.setAccessToken(configuration.get("spotify.access")); + api.createPlaylist(api.getCurrentUsersProfile().build().execute().getId(), configuration.get("playlist.name")).setHeader("User-Agent", configuration.get("requests.useragent")); + break; + case 5: + pb.setExtraMessage("Done."); + break; + } + } + } catch (IOException | ParseException | SpotifyWebApiException e) { + throw new RuntimeException(e); + } + //TODO: Implement + } + +} diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/ArgumentHandler.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/ArgumentHandler.java new file mode 100644 index 0000000..21cc314 --- /dev/null +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/ArgumentHandler.java @@ -0,0 +1,59 @@ +package de.b00tload.tools.lastfmtospotifyplaylist.arguments; + +import de.b00tload.tools.lastfmtospotifyplaylist.util.Logger; +import org.jetbrains.annotations.Nullable; + +import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.LINE_SEPERATOR; +import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.configuration; + +public class ArgumentHandler { + + public static void handle(Arguments argument, @Nullable String value){ + switch (argument){ + case HELP -> help(value); + case VERBOSE -> verbose(value); + } + } + + public static void handle(Arguments argument) { + handle(argument, null); + } + + private static void help(String value){ + if(value == null){ + System.out.println("This is a list of all available commands. For more specific help on the argument run --help ."); + for(Arguments arg : Arguments.values()) { + String name = arg.getName(); + String description = arg.getDescription(); + System.out.println("____________________"); + System.out.println(name); + System.out.println(" DESCRIPTION" + LINE_SEPERATOR + description); + } + System.exit(200); + } + Arguments arg = Arguments.resolveByNameOrAlias(value); + if(arg == null) { + System.out.println("This argument is unknown. Use --help to get a list of all arguments"); + System.exit(200); + } + String name = arg.getName(); + String description = arg.getDescription(); + String[] aliases = arg.getAliases(); + String usage = arg.getUsage(); + System.out.println(name); + System.out.println(" DESCRIPTION" + LINE_SEPERATOR + description); + System.out.println(" USAGE: " + usage); + StringBuilder aliasString = new StringBuilder(); + for(String alias : aliases){ + aliasString.append(", -").append(alias); + } + System.out.println(" ALIASES:" + aliasString.substring(1)); + System.out.println("____________________"); + System.exit(200); + } + + private static void verbose(String value){ + //wenn value != int + System.out.println("LogLevel must be a numeric value."); + } +} diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/Arguments.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/Arguments.java new file mode 100644 index 0000000..f535f66 --- /dev/null +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/arguments/Arguments.java @@ -0,0 +1,69 @@ +package de.b00tload.tools.lastfmtospotifyplaylist.arguments; + +import java.util.List; + +import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.LINE_SEPERATOR; + +public enum Arguments { + + HELP("help", "[Optional, will not execute tool] " + LINE_SEPERATOR + + "Shows a list of all commands or, if provided, help for a given command.", "--help [argument]", "h", "?"), + VERBOSE("loglevel", "[Optional] " + LINE_SEPERATOR + + "Sets the loglevel. May flood the console. Use carefully." + LINE_SEPERATOR + + "Possible loglevels:" + LINE_SEPERATOR + + " - 0: Quiet Will run completely quietly" + LINE_SEPERATOR + + " - 1: Default Will only show progress" + LINE_SEPERATOR + + " - 2: Verbose Will echo current step being worked on" + LINE_SEPERATOR + + " - 3: Debug Will give specific information on what excactly the tool is doing", + "--loglevel ", "log", "l"), + SECRET("spotifysecret", "[]", "--spotifysecret ", "sS", "sSecret"); + + private final String name; + private final String description; + private final String usage; + private final String[] aliases; + + Arguments(String name, String description, String usage, String... aliases){ + this.name = name; + this.description = description; + this.usage = usage; + this.aliases = aliases; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public String getUsage() { + return usage; + } + + public String[] getAliases() { + return aliases; + } + + public static Arguments getByAlias(String alias){ + for(Arguments arg : values()){ + if(List.of(arg.getAliases()).contains(alias)) return arg; + } + return null; + } + + public static Arguments getByName(String name){ + for(Arguments arg : values()){ + if(arg.getName().equalsIgnoreCase(name)) return arg; + } + return null; + } + + public static Arguments resolveByNameOrAlias(String v){ + Arguments ret = getByName(v); + if(ret != null) return ret; + ret = getByAlias(v); + return ret; + } +} diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/Logger.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/Logger.java new file mode 100644 index 0000000..eb38e8d --- /dev/null +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/Logger.java @@ -0,0 +1,11 @@ +package de.b00tload.tools.lastfmtospotifyplaylist.util; + +import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.configuration; + +public class Logger { + + public static void logLn(String string, int priority){ + if(Integer.parseInt(configuration.get("verbose.level"))) + } + +}