Implemented a few features #1

Merged
B00tLoad merged 6 commits from alix into master 2023-01-18 04:51:07 +01:00
7 changed files with 142 additions and 67 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
# Project exclude paths # Project exclude paths
/target/ /target/
.idea/
compile.bat

View File

@@ -56,11 +56,6 @@
<artifactId>javalin</artifactId> <artifactId>javalin</artifactId>
<version>5.3.1</version> <version>5.3.1</version>
</dependency> </dependency>
<dependency>
<groupId>me.tongfei</groupId>
<artifactId>progressbar</artifactId>
<version>0.9.5</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -3,25 +3,26 @@ package de.b00tload.tools.lastfmtospotifyplaylist;
import de.b00tload.tools.lastfmtospotifyplaylist.arguments.ArgumentHandler; import de.b00tload.tools.lastfmtospotifyplaylist.arguments.ArgumentHandler;
import de.b00tload.tools.lastfmtospotifyplaylist.arguments.Arguments; import de.b00tload.tools.lastfmtospotifyplaylist.arguments.Arguments;
import de.b00tload.tools.lastfmtospotifyplaylist.util.PeriodHelper;
import de.umass.lastfm.Caller; import de.umass.lastfm.Caller;
import de.umass.lastfm.Period; import de.umass.lastfm.Track;
import de.umass.lastfm.User; 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.util.Collection;
import java.net.URI;
import java.util.HashMap; import java.util.HashMap;
import static de.b00tload.tools.lastfmtospotifyplaylist.util.Logger.logLn;
public class LastFMToSpotify { public class LastFMToSpotify {
public static final String LINE_SEPERATOR = System.getProperty("line.separator"); public static final String LINE_SEPERATOR = System.getProperty("line.separator");
public static HashMap<String, String> configuration; public static HashMap<String, String> configuration;
public static void main(String[] args) { public static void main(String[] args) {
// create hash map with user agent
configuration = new HashMap<>();
configuration.put("requests.useragent", "LastFMToSpotify/1.0-Snapshot (" + System.getProperty("os.name") + "; " + System.getProperty("os.arch") + ") Java/" + System.getProperty("java.version")); configuration.put("requests.useragent", "LastFMToSpotify/1.0-Snapshot (" + System.getProperty("os.name") + "; " + System.getProperty("os.arch") + ") Java/" + System.getProperty("java.version"));
// parse arguments
for (int a = 0; a < args.length; a++) { for (int a = 0; a < args.length; a++) {
Arguments arg; Arguments arg;
if (args[a].startsWith("--")) { if (args[a].startsWith("--")) {
@@ -29,7 +30,7 @@ public class LastFMToSpotify {
} else if (args[a].startsWith("-")) { } else if (args[a].startsWith("-")) {
arg = Arguments.getByAlias(args[a].substring(1)); arg = Arguments.getByAlias(args[a].substring(1));
} else { } else {
break; continue;
} }
if (arg == null) { if (arg == null) {
ArgumentHandler.handle(Arguments.HELP); ArgumentHandler.handle(Arguments.HELP);
@@ -44,40 +45,29 @@ public class LastFMToSpotify {
} }
} }
// 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; // Start Progress Bar
case 2: try {
pb.setExtraMessage("Authenticating with LastFM..."); logLn("Authenticating with Spotify...", 1);
logLn("Authenticating with LastFM...", 1);
Caller.getInstance().setUserAgent(configuration.get("requests.useragent")); Caller.getInstance().setUserAgent(configuration.get("requests.useragent"));
User.getInfo(configuration.get("lastfm.user"), configuration.get("lastfm.apikey")).getName(); logLn(User.getInfo(configuration.get("lastfm.user"), configuration.get("lastfm.apikey")).getName(), 1);
break; logLn("Reading from LastFM...", 1);
case 3: Collection<Track> tracks = User.getTopTracks(configuration.get("lastfm.user"), PeriodHelper.getPeriodByString(configuration.get("lastfm.period")), configuration.get("lastfm.apikey"));
pb.setExtraMessage("Reading from LastFM..."); for (Track track : tracks) {
User.getTopTracks(configuration.get("lastfm.user"), Period.ONE_MONTH, configuration.get("lastfm.apikey")); logLn(track.getName() + " by " + track.getArtist(), 3);
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;
} }
} logLn("Creating Playlist...", 1);
} catch (IOException | ParseException | SpotifyWebApiException e) { //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"));
logLn("Done.", 1);
// } catch (IOException | ParseException | SpotifyWebApiException e) {
} catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
//TODO: Implement //TODO: Implement

View File

@@ -1,6 +1,6 @@
package de.b00tload.tools.lastfmtospotifyplaylist.arguments; package de.b00tload.tools.lastfmtospotifyplaylist.arguments;
import de.b00tload.tools.lastfmtospotifyplaylist.util.Logger; import de.umass.lastfm.Period;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.LINE_SEPERATOR; import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.LINE_SEPERATOR;
@@ -12,6 +12,15 @@ public class ArgumentHandler {
switch (argument) { switch (argument) {
case HELP -> help(value); case HELP -> help(value);
case VERBOSE -> verbose(value); case VERBOSE -> verbose(value);
case SECRET -> secret(value);
case CLIENT -> client(value);
case TOKEN -> token(value);
case USER -> user(value);
case WEEKLY -> period(Period.WEEK);
case MONTHLY -> period(Period.ONE_MONTH);
case QUARTERLY -> period(Period.THREE_MONTHS);
case BIANNUALLY -> period(Period.SIX_MONTHS);
case YEARLY -> period(Period.TWELVE_MONTHS);
} }
} }
@@ -53,7 +62,53 @@ public class ArgumentHandler {
} }
private static void verbose(String value) { private static void verbose(String value) {
//wenn value != int if (value == null) {
System.out.println("--loglevel must be provided with a numeric log level. Check usage: " + Arguments.VERBOSE.getUsage());
System.exit(500);
}
try {
int loglevel = Integer.parseInt(value);
configuration.put("logging.level", String.valueOf(loglevel));
} catch (NumberFormatException e) {
System.out.println("LogLevel must be a numeric value."); System.out.println("LogLevel must be a numeric value.");
System.exit(500);
}
}
private static void token(String value) {
if (value == null || value.equalsIgnoreCase("")) {
System.out.println("--lastfmtoken must be provided with an api token from LastFM. Check usage: " + Arguments.TOKEN.getUsage());
System.exit(500);
}
configuration.put("lastfm.apikey", value);
}
private static void user(String value) {
if (value == null || value.equalsIgnoreCase("")) {
System.out.println("--lastfmuser must be provided with a LastFM username. Check usage: " + Arguments.USER.getUsage());
System.exit(500);
}
configuration.put("lastfm.user", value);
}
private static void client(String value) {
if (value == null || value.equalsIgnoreCase("")) {
System.out.println("--spotifyclient must be provided with a client id from Spotify. Check usage: " + Arguments.CLIENT.getUsage());
System.exit(500);
}
configuration.put("spotify.clientid", value);
}
private static void secret(String value) {
if (value == null || value.equalsIgnoreCase("")) {
System.out.println("--spotifysecret must be provided with a client secret from Spotify. Check usage: " + Arguments.SECRET.getUsage());
System.exit(500);
}
configuration.put("spotify.secret", value);
}
private static void period(Period value) {
configuration.put("lastfm.period", value.getString());
} }
} }

View File

@@ -16,7 +16,24 @@ public enum Arguments {
+ " - 2: Verbose Will echo current step being worked on" + 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", + " - 3: Debug Will give specific information on what excactly the tool is doing",
"--loglevel <level>", "log", "l"), "--loglevel <level>", "log", "l"),
SECRET("spotifysecret", "[]", "--spotifysecret <secret>", "sS", "sSecret"); SECRET("spotifysecret", "[Required]" + LINE_SEPERATOR
+ "Sets the spotify client secret.", "--spotifysecret <secret>", "sS", "sSecret"),
CLIENT("spotifyclient", "[Required]" + LINE_SEPERATOR
+ "Sets the spotify cliend id.", "--spotifyclient <clientid>", "sC", "sClient"),
TOKEN("lastfmtoken", "[Required]" + LINE_SEPERATOR
+ "Sets the LastFM API token.", "--lastfmtoken <apitoken>", "lT", "lToken"),
USER("lastfmuser", "[Required]" + LINE_SEPERATOR
+ "Sets the LastFM API token.", "--lastfmuser <username>", "lU", "lUser"),
WEEKLY("weekly", "[Optional]" + LINE_SEPERATOR
+ "Creates a playlist from your top tracks from last week.", "--weekly", "W"),
MONTHLY("monthly", "[Optional, Default]" + LINE_SEPERATOR
+ "Creates a playlist from your top tracks from last month.", "--monthly", "M"),
QUARTERLY("quarterly", "[Optional]" + LINE_SEPERATOR
+ "Creates a playlist from your top tracks from last quarter.", "--quarterly", "Q"),
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");;
private final String name; private final String name;
private final String description; private final String description;

View File

@@ -5,7 +5,9 @@ import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.configur
public class Logger { public class Logger {
public static void logLn(String string, int priority){ public static void logLn(String string, int priority){
if(Integer.parseInt(configuration.get("verbose.level"))) if(Integer.parseInt(configuration.get("logging.level"))>=priority){
System.out.println(string);
}
} }
} }

View File

@@ -0,0 +1,14 @@
package de.b00tload.tools.lastfmtospotifyplaylist.util;
import de.umass.lastfm.Period;
public class PeriodHelper {
public static Period getPeriodByString(String string){
for(Period p : Period.values()){
if(p.getString().equalsIgnoreCase(string)) return p;
}
return Period.ONE_MONTH;
}
}