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:
Alix von Schirp
2023-01-18 04:45:07 +01:00
parent 0681844dab
commit 362ac0e4c3
5 changed files with 72 additions and 42 deletions

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,15 +3,12 @@ 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 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; import static de.b00tload.tools.lastfmtospotifyplaylist.util.Logger.logLn;
@@ -56,17 +53,21 @@ public class LastFMToSpotify {
Caller.getInstance().setUserAgent(configuration.get("requests.useragent")); Caller.getInstance().setUserAgent(configuration.get("requests.useragent"));
logLn(User.getInfo(configuration.get("lastfm.user"), configuration.get("lastfm.apikey")).getName(), 1); logLn(User.getInfo(configuration.get("lastfm.user"), configuration.get("lastfm.apikey")).getName(), 1);
logLn("Reading from LastFM...", 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); logLn("Creating Playlist...", 1);
SpotifyApi.Builder build = SpotifyApi.builder(); //SpotifyApi.Builder build = SpotifyApi.builder();
build.setClientId(configuration.get("spotify.clientid")); //build.setClientId(configuration.get("spotify.clientid"));
build.setClientSecret(configuration.get("spotify.secret")); //build.setClientSecret(configuration.get("spotify.secret"));
build.setRedirectUri(URI.create("http://localhost:9876/callback/spotify/")); //build.setRedirectUri(URI.create("http://localhost:9876/callback/spotify/"));
SpotifyApi api = build.build(); //SpotifyApi api = build.build();
api.setAccessToken(configuration.get("spotify.access")); //api.setAccessToken(configuration.get("spotify.access"));
api.createPlaylist(api.getCurrentUsersProfile().build().execute().getId(), configuration.get("playlist.name")).setHeader("User-Agent", configuration.get("requests.useragent")); //api.createPlaylist(api.getCurrentUsersProfile().build().execute().getId(), configuration.get("playlist.name")).setHeader("User-Agent", configuration.get("requests.useragent"));
logLn("Done.", 1); logLn("Done.", 1);
} catch (IOException | ParseException | SpotifyWebApiException e) { // } catch (IOException | ParseException | SpotifyWebApiException e) {
} catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
//TODO: Implement //TODO: Implement

View File

@@ -1,5 +1,6 @@
package de.b00tload.tools.lastfmtospotifyplaylist.arguments; package de.b00tload.tools.lastfmtospotifyplaylist.arguments;
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;
@@ -15,6 +16,11 @@ public class ArgumentHandler {
case CLIENT -> client(value); case CLIENT -> client(value);
case TOKEN -> token(value); case TOKEN -> token(value);
case USER -> user(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);
} }
} }
@@ -101,4 +107,8 @@ public class ArgumentHandler {
} }
configuration.put("spotify.secret", value); configuration.put("spotify.secret", value);
} }
private static void period(Period value) {
configuration.put("lastfm.period", value.getString());
}
} }

View File

@@ -23,7 +23,17 @@ public enum Arguments {
TOKEN("lastfmtoken", "[Required]" + LINE_SEPERATOR TOKEN("lastfmtoken", "[Required]" + LINE_SEPERATOR
+ "Sets the LastFM API token.", "--lastfmtoken <apitoken>", "lT", "lToken"), + "Sets the LastFM API token.", "--lastfmtoken <apitoken>", "lT", "lToken"),
USER("lastfmuser", "[Required]" + LINE_SEPERATOR 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 name;
private final String description; private final String description;

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;
}
}