Implemented Fetching tracks and selecting periods.
Also deleted progress bar dependency after removal from code in 3445f51 and cleaned up formatting.
This commit is contained in:
@@ -3,15 +3,12 @@ package de.b00tload.tools.lastfmtospotifyplaylist;
|
||||
|
||||
import de.b00tload.tools.lastfmtospotifyplaylist.arguments.ArgumentHandler;
|
||||
import de.b00tload.tools.lastfmtospotifyplaylist.arguments.Arguments;
|
||||
import de.b00tload.tools.lastfmtospotifyplaylist.util.PeriodHelper;
|
||||
import de.umass.lastfm.Caller;
|
||||
import de.umass.lastfm.Period;
|
||||
import de.umass.lastfm.Track;
|
||||
import de.umass.lastfm.User;
|
||||
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.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static de.b00tload.tools.lastfmtospotifyplaylist.util.Logger.logLn;
|
||||
@@ -24,27 +21,27 @@ public class LastFMToSpotify {
|
||||
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;
|
||||
if(args[a].startsWith("--")){
|
||||
if (args[a].startsWith("--")) {
|
||||
arg = Arguments.getByName(args[a].substring(2));
|
||||
} else if(args[a].startsWith("-")){
|
||||
} else if (args[a].startsWith("-")) {
|
||||
arg = Arguments.getByAlias(args[a].substring(1));
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if(arg==null) {
|
||||
if (arg == null) {
|
||||
ArgumentHandler.handle(Arguments.HELP);
|
||||
return;
|
||||
}
|
||||
if(args.length-a==1){
|
||||
if (args.length - a == 1) {
|
||||
ArgumentHandler.handle(arg);
|
||||
} else if(args[a+1].startsWith("--") || args[a+1].startsWith("-")){
|
||||
} else if (args[a + 1].startsWith("--") || args[a + 1].startsWith("-")) {
|
||||
ArgumentHandler.handle(arg);
|
||||
} else {
|
||||
ArgumentHandler.handle(arg, args[a+1]);
|
||||
ArgumentHandler.handle(arg, args[a + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,17 +53,21 @@ public class LastFMToSpotify {
|
||||
Caller.getInstance().setUserAgent(configuration.get("requests.useragent"));
|
||||
logLn(User.getInfo(configuration.get("lastfm.user"), configuration.get("lastfm.apikey")).getName(), 1);
|
||||
logLn("Reading from LastFM...", 1);
|
||||
User.getTopTracks(configuration.get("lastfm.user"), Period.ONE_MONTH, configuration.get("lastfm.apikey"));
|
||||
Collection<Track> tracks = User.getTopTracks(configuration.get("lastfm.user"), PeriodHelper.getPeriodByString(configuration.get("lastfm.period")), configuration.get("lastfm.apikey"));
|
||||
for (Track track : tracks) {
|
||||
logLn(track.getName() + " by " + track.getArtist(), 3);
|
||||
}
|
||||
logLn("Creating Playlist...", 1);
|
||||
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"));
|
||||
//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 (IOException | ParseException | SpotifyWebApiException e) {
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
//TODO: Implement
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.b00tload.tools.lastfmtospotifyplaylist.arguments;
|
||||
|
||||
import de.umass.lastfm.Period;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.LINE_SEPERATOR;
|
||||
@@ -7,14 +8,19 @@ import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.configur
|
||||
|
||||
public class ArgumentHandler {
|
||||
|
||||
public static void handle(Arguments argument, @Nullable String value){
|
||||
switch (argument){
|
||||
public static void handle(Arguments argument, @Nullable String value) {
|
||||
switch (argument) {
|
||||
case HELP -> help(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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,10 +28,10 @@ public class ArgumentHandler {
|
||||
handle(argument, null);
|
||||
}
|
||||
|
||||
private static void help(String value){
|
||||
if(value == 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()) {
|
||||
for (Arguments arg : Arguments.values()) {
|
||||
String name = arg.getName();
|
||||
String description = arg.getDescription();
|
||||
System.out.println("____________________");
|
||||
@@ -35,7 +41,7 @@ public class ArgumentHandler {
|
||||
System.exit(200);
|
||||
}
|
||||
Arguments arg = Arguments.resolveByNameOrAlias(value);
|
||||
if(arg == null) {
|
||||
if (arg == null) {
|
||||
System.out.println("This argument is unknown. Use --help to get a list of all arguments");
|
||||
System.exit(200);
|
||||
}
|
||||
@@ -47,7 +53,7 @@ public class ArgumentHandler {
|
||||
System.out.println(" DESCRIPTION" + LINE_SEPERATOR + description);
|
||||
System.out.println(" USAGE: " + usage);
|
||||
StringBuilder aliasString = new StringBuilder();
|
||||
for(String alias : aliases){
|
||||
for (String alias : aliases) {
|
||||
aliasString.append(", -").append(alias);
|
||||
}
|
||||
System.out.println(" ALIASES:" + aliasString.substring(1));
|
||||
@@ -55,8 +61,8 @@ public class ArgumentHandler {
|
||||
System.exit(200);
|
||||
}
|
||||
|
||||
private static void verbose(String value){
|
||||
if(value == null){
|
||||
private static void verbose(String value) {
|
||||
if (value == null) {
|
||||
System.out.println("--loglevel must be provided with a numeric log level. Check usage: " + Arguments.VERBOSE.getUsage());
|
||||
System.exit(500);
|
||||
}
|
||||
@@ -71,7 +77,7 @@ public class ArgumentHandler {
|
||||
}
|
||||
|
||||
private static void token(String value) {
|
||||
if(value == null || value.equalsIgnoreCase("")){
|
||||
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);
|
||||
}
|
||||
@@ -79,7 +85,7 @@ public class ArgumentHandler {
|
||||
}
|
||||
|
||||
private static void user(String value) {
|
||||
if(value == null || value.equalsIgnoreCase("")){
|
||||
if (value == null || value.equalsIgnoreCase("")) {
|
||||
System.out.println("--lastfmuser must be provided with a LastFM username. Check usage: " + Arguments.USER.getUsage());
|
||||
System.exit(500);
|
||||
}
|
||||
@@ -87,7 +93,7 @@ public class ArgumentHandler {
|
||||
}
|
||||
|
||||
private static void client(String value) {
|
||||
if(value == null || value.equalsIgnoreCase("")){
|
||||
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);
|
||||
}
|
||||
@@ -95,10 +101,14 @@ public class ArgumentHandler {
|
||||
}
|
||||
|
||||
private static void secret(String value) {
|
||||
if(value == null || value.equalsIgnoreCase("")){
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,17 @@ public enum Arguments {
|
||||
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");
|
||||
+ "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 description;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user