Merge pull request #12 from B00tLoad/flagexclusivity

Implemented checking flags for exclusivity
This commit was merged in pull request #12.
This commit is contained in:
Môrrîl
2023-01-24 16:30:24 +01:00
committed by GitHub
5 changed files with 45 additions and 17 deletions

2
.gitignore vendored
View File

@@ -2,4 +2,4 @@
/target/ /target/
.idea/ .idea/
compile.bat compile.bat
.vscode/ /.vscode/

View File

@@ -1,3 +0,0 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}

View File

@@ -36,10 +36,13 @@ public class LastFMToSpotify {
configuration = new HashMap<>(); 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"));
configuration.put("playlist.name", "LastFMToSpotify@" + LocalDateTime.now(Clock.systemDefaultZone()).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); configuration.put("playlist.name", "LastFMToSpotify@" + LocalDateTime.now(Clock.systemDefaultZone()).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
configuration.put("log.level", "1"); configuration.put("logging.level", "1");
if (!ArgumentHandler.checkArguments(args)) { if (!ArgumentHandler.checkArguments(args)) {
return; return;
} }
if(!ArgumentHandler.checkExclusivity(args)){
return;
}
// parse arguments // parse arguments
for (int a = 0; a < args.length; a++) { for (int a = 0; a < args.length; a++) {
@@ -111,9 +114,6 @@ public class LastFMToSpotify {
searchQuery.append(" album:").append(track.getAlbum()); searchQuery.append(" album:").append(track.getAlbum());
logLn("Search query: " + searchQuery, 3); logLn("Search query: " + searchQuery, 3);
se.michaelthelin.spotify.model_objects.specification.Track[] add = api.searchTracks(searchQuery.toString()).market(CountryCode.DE).setHeader("User-Agent", configuration.get("requests.useragent")).build().execute().getItems(); se.michaelthelin.spotify.model_objects.specification.Track[] add = api.searchTracks(searchQuery.toString()).market(CountryCode.DE).setHeader("User-Agent", configuration.get("requests.useragent")).build().execute().getItems();
if(add.length!=0) {
// adders.add(add[0].getUri());
// logLn("Added " + add[0].getName() + " to " + configuration.get("playlist.name"), 3);
for(se.michaelthelin.spotify.model_objects.specification.Track t : add){ for(se.michaelthelin.spotify.model_objects.specification.Track t : add){
if(t.getName().equalsIgnoreCase(track.getName())){ if(t.getName().equalsIgnoreCase(track.getName())){
adders.add(t.getUri()); adders.add(t.getUri());
@@ -122,7 +122,6 @@ public class LastFMToSpotify {
} }
} }
} }
}
api.addItemsToPlaylist(list.getId(), adders.toArray(String[]::new)).build().execute(); api.addItemsToPlaylist(list.getId(), adders.toArray(String[]::new)).build().execute();
if(configuration.containsKey("playlist.cover")){ if(configuration.containsKey("playlist.cover")){
logLn("Check for \"null\" if setting cover was successful: " + api.uploadCustomPlaylistCoverImage(list.getId()).image_data(configuration.get("playlist.cover")).setHeader("User-Agent", configuration.get("requests.useragent")).build().execute(),3); logLn("Check for \"null\" if setting cover was successful: " + api.uploadCustomPlaylistCoverImage(list.getId()).image_data(configuration.get("playlist.cover")).setHeader("User-Agent", configuration.get("requests.useragent")).build().execute(),3);

View File

@@ -14,6 +14,7 @@ import java.time.LocalDateTime;
import java.time.format.TextStyle; import java.time.format.TextStyle;
import java.time.temporal.ChronoField; import java.time.temporal.ChronoField;
import java.time.temporal.WeekFields; import java.time.temporal.WeekFields;
import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.LINE_SEPERATOR; import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.LINE_SEPERATOR;
@@ -52,23 +53,43 @@ public class ArgumentHandler {
// check if all required arguments are given // check if all required arguments are given
Arguments[] required = {Arguments.SECRET, Arguments.CLIENT, Arguments.TOKEN, Arguments.USER}; Arguments[] required = {Arguments.SECRET, Arguments.CLIENT, Arguments.TOKEN, Arguments.USER};
for (Arguments argument : required) { for (Arguments argument : required) {
boolean found = false; boolean found = List.of(args).contains("--" + argument.getName());
// check all aliases // check all aliases
for (String alias : argument.getAliases()) { for (String alias : argument.getAliases()) {
// if one alias is found check next argument // if one alias is found check next argument
if (List.of(args).contains(alias)) { if (List.of(args).contains("-"+alias)) {
found = true; found = true;
break; break;
} }
} }
// else return false // else return false
if (!found) { if (!found) {
logLn("Missing required argument " + argument.getName(), 1);
return false; return false;
} }
} }
return true; return true;
} }
public static boolean checkExclusivity(String[] args){
Arguments[][] exclusive = {{Arguments.PUBLIC, Arguments.COLLABORATIVE}, {Arguments.WEEKLY, Arguments.MONTHLY, Arguments.QUARTERLY, Arguments.BIANNUALLY, Arguments.YEARLY}};
for(Arguments[] arguments : exclusive){
int count = 0;
for(Arguments argument : arguments){
if(List.of(args).contains("--"+argument.getName())) count++;
for(String alias : argument.getAliases()) {
if(List.of(args).contains("-"+alias)) count++;
}
if(count>1){
logLn("You may only use one flag out of every exclusive group." + LINE_SEPERATOR +
"This exclusive group contains of: " + Arrays.toString(arguments), 1);
return false;
}
}
}
return true;
}
private static void help(String value) { private static void help(String value) {
if (value == null || value.isEmpty()) { if (value == null || value.isEmpty()) {
logLn("This is a list of all available commands. For more specific help on the argument run --help <argument>.", 1); logLn("This is a list of all available commands. For more specific help on the argument run --help <argument>.", 1);

View File

@@ -91,4 +91,15 @@ public enum Arguments {
ret = getByAlias(v); ret = getByAlias(v);
return ret; return ret;
} }
@Override
public String toString() {
StringBuilder args = new StringBuilder("--").append(this.name).append(" (");
for(String alias : this.getAliases()){
args.append("-").append(alias).append(", ");
}
args.delete(args.length()-2, args.length());
args.append(")");
return args.toString();
}
} }