Implemented checking flags for exclusivity

- Also fixed a problem where required arguments were not found by their name, but only by their aliases.
- Minor cleanup.
- Fixed a problem where default logging level was not set successfully due to a naming error in the configuration field.
This commit is contained in:
2023-01-24 16:21:06 +01:00
parent bc38824fa9
commit 745acc765f
5 changed files with 45 additions and 17 deletions

2
.gitignore vendored
View File

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

View File

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

View File

@@ -37,10 +37,13 @@ public class LastFMToSpotify {
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("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)) {
return;
}
if(!ArgumentHandler.checkExclusivity(args)){
return;
}
// parse arguments
for (int a = 0; a < args.length; a++) {
@@ -112,15 +115,11 @@ public class LastFMToSpotify {
searchQuery.append(" album:").append(track.getAlbum());
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();
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){
if(t.getName().equalsIgnoreCase(track.getName())){
adders.add(t.getUri());
logLn("Added " + add[0].getName() + " to " + configuration.get("playlist.name"), 3);
break;
}
for(se.michaelthelin.spotify.model_objects.specification.Track t : add){
if(t.getName().equalsIgnoreCase(track.getName())){
adders.add(t.getUri());
logLn("Added " + add[0].getName() + " to " + configuration.get("playlist.name"), 3);
break;
}
}
}

View File

@@ -17,6 +17,7 @@ import java.time.temporal.ChronoField;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
import java.util.Arrays;
import java.util.Locale;
import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.LINE_SEPERATOR;
@@ -58,20 +59,40 @@ public class ArgumentHandler {
// check if all required arguments are given
Arguments[] required = {Arguments.SECRET, Arguments.CLIENT, Arguments.TOKEN, Arguments.USER};
for (Arguments argument : required) {
boolean found = false;
boolean found = List.of(args).contains("--" + argument.getName());
// check all aliases
for (String alias : argument.getAliases()) {
// if one alias is found check next argument
if (List.of(args).contains(alias)) {
if (List.of(args).contains("-"+alias)) {
found = true;
break;
}
}
// else return false
if (!found) {
logLn("Missing required argument " + argument.getName(), 1);
return false;
}
}
}
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;
}

View File

@@ -91,4 +91,15 @@ public enum Arguments {
ret = getByAlias(v);
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();
}
}