Initial commit

This commit is contained in:
Alix von Schirp
2023-01-18 01:53:25 +01:00
commit e326867ebd
6 changed files with 293 additions and 0 deletions

View File

@@ -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<String, String> 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<args.length; a++){
Arguments arg;
if(args[a].startsWith("--")){
arg = Arguments.getByName(args[a].substring(2));
} else if(args[a].startsWith("-")){
arg = Arguments.getByAlias(args[a].substring(1));
} else {
break;
}
if(arg==null) {
ArgumentHandler.handle(Arguments.HELP);
return;
}
if(args.length-a==1){
ArgumentHandler.handle(arg);
} else if(args[a+1].startsWith("--") || args[a+1].startsWith("-")){
ArgumentHandler.handle(arg);
} else {
ArgumentHandler.handle(arg, args[a+1]);
}
}
// Start Progress Bar
try (ProgressBar pb = new ProgressBar("LastFM -> 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
}
}

View File

@@ -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 <argument>.");
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.");
}
}

View File

@@ -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 <level>", "log", "l"),
SECRET("spotifysecret", "[]", "--spotifysecret <secret>", "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;
}
}

View File

@@ -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")))
}
}