use of se.michaelthelin.spotify.model_objects.specification.ArtistSimplified in project aiode by robinfriedli.
the class AbstractPlayableLoadingCommand method loadTrack.
private void loadTrack(AudioManager audioManager) throws Exception {
int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 20);
Callable<List<Track>> loadTrackCallable = () -> getSpotifyService().searchTrack(getCommandInput(), argumentSet("own"), limit);
List<Track> found;
if (argumentSet("own")) {
found = runWithLogin(loadTrackCallable);
} else {
found = runWithCredentials(loadTrackCallable);
}
if (found.size() == 1) {
createPlayableForTrack(found.get(0), audioManager);
} else if (found.isEmpty()) {
throw new NoSpotifyResultsFoundException(String.format("No Spotify track found for '%s'", getCommandInput()));
} else {
if (argumentSet("select")) {
askQuestion(found, track -> {
String artistString = StringList.create(track.getArtists(), ArtistSimplified::getName).toSeparatedString(", ");
return String.format("%s by %s", track.getName(), artistString);
}, track -> track.getAlbum().getName());
} else {
SpotifyTrackResultHandler resultHandler = new SpotifyTrackResultHandler(getContext().getGuild(), getContext().getSession());
createPlayableForTrack(resultHandler.getBestResult(getCommandInput(), found), audioManager);
}
}
}
use of se.michaelthelin.spotify.model_objects.specification.ArtistSimplified in project aiode by robinfriedli.
the class SpotifyTrack method getDisplay.
public String getDisplay() {
String name = getName();
String artistString = exhaustiveMatch(track -> StringList.create(track.getArtists(), ArtistSimplified::getName).toSeparatedString(", "), episode -> {
ShowSimplified show = episode.getShow();
if (show != null) {
return show.getName();
}
return null;
});
if (!Strings.isNullOrEmpty(artistString)) {
return String.format("%s by %s", name, artistString);
} else {
return name;
}
}
use of se.michaelthelin.spotify.model_objects.specification.ArtistSimplified in project aiode by robinfriedli.
the class YouTubeService method getBestEditDistance.
private int getBestEditDistance(SpotifyTrack spotifyTrack, Video video) {
LevenshteinDistance levenshteinDistance = LevenshteinDistance.getDefaultInstance();
String trackName = spotifyTrack.getName().toLowerCase().trim().replaceAll("\\s+", " ");
String videoTitle = video.getSnippet().getTitle().toLowerCase().trim().replaceAll("\\s+", " ");
return spotifyTrack.exhaustiveMatch(track -> {
ArtistSimplified[] artists = track.getArtists();
String firstArtist = artists.length > 0 ? artists[0].getName().toLowerCase() : "";
StringList artistNames = StringList.create(artists, ArtistSimplified::getName);
String artistString = artistNames.toSeparatedString(", ").toLowerCase();
String featuringArtistString = artistNames.size() > 1 ? artistNames.subList(1).toSeparatedString(", ").toLowerCase() : "";
int parenthesesNotEscaped = bestEditDistanceForParams(levenshteinDistance, trackName, videoTitle, artists, artistString, firstArtist, featuringArtistString);
String videoTitleWithParenthesesRemoved = videoTitle.replaceAll("\\(.*\\)|\\[.*]", "").trim().replaceAll("\\s+", " ");
int parenthesesEscaped = bestEditDistanceForParams(levenshteinDistance, trackName, videoTitleWithParenthesesRemoved, artists, artistString, firstArtist, featuringArtistString);
return Math.min(parenthesesEscaped, parenthesesNotEscaped);
}, episode -> {
String episodeName = episode.getName().toLowerCase();
Integer distanceName = levenshteinDistance.apply(episodeName, videoTitle);
ShowSimplified show = episode.getShow();
if (show == null) {
return distanceName;
}
String showName = show.getName().toLowerCase();
Integer distanceShowName = levenshteinDistance.apply(showName + " " + episodeName, videoTitle);
Integer distanceNameShow = levenshteinDistance.apply(episodeName + " " + showName, videoTitle);
return IntStream.of(distanceName, distanceShowName, distanceNameShow).min().getAsInt();
});
}
use of se.michaelthelin.spotify.model_objects.specification.ArtistSimplified in project aiode by robinfriedli.
the class AbstractPlayableLoadingCommand method loadSpotifyAlbum.
private void loadSpotifyAlbum(AudioManager audioManager) throws Exception {
int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 20);
Callable<List<AlbumSimplified>> albumLoadCallable = () -> getSpotifyService().searchAlbum(getCommandInput(), argumentSet("own"), limit);
List<AlbumSimplified> albums;
if (argumentSet("own")) {
albums = runWithLogin(albumLoadCallable);
} else {
albums = runWithCredentials(albumLoadCallable);
}
if (albums.size() == 1) {
AlbumSimplified album = albums.get(0);
List<Track> tracks = runWithCredentials(() -> getSpotifyService().getAlbumTracks(album.getId()));
PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
List<Playable> playables = playableFactory.createPlayables(shouldRedirectSpotify(), tracks);
handleResults(playables);
loadedAlbum = album;
} else if (albums.isEmpty()) {
throw new NoSpotifyResultsFoundException(String.format("No albums found for '%s'", getCommandInput()));
} else {
askQuestion(albums, AlbumSimplified::getName, album -> StringList.create(album.getArtists(), ArtistSimplified::getName).toSeparatedString(", "));
}
}
use of se.michaelthelin.spotify.model_objects.specification.ArtistSimplified in project aiode by robinfriedli.
the class SearchCommand method listTracks.
private void listTracks(List<SpotifyTrack> spotifyTracks, String name, String owner, String artist, String path) {
EmbedBuilder embedBuilder = new EmbedBuilder();
long totalDuration = spotifyTracks.stream().mapToInt(track -> {
Integer durationMs = track.getDurationMs();
return Objects.requireNonNullElse(durationMs, 0);
}).sum();
embedBuilder.addField("Name", name, true);
embedBuilder.addField("Song count", String.valueOf(spotifyTracks.size()), true);
embedBuilder.addField("Duration", Util.normalizeMillis(totalDuration), true);
if (owner != null) {
embedBuilder.addField("Owner", owner, true);
}
if (artist != null) {
embedBuilder.addField("Artist", artist, true);
}
if (!spotifyTracks.isEmpty()) {
String url = "https://open.spotify.com/" + path;
embedBuilder.addField("First tracks:", "[Full list](" + url + ")", false);
Util.appendEmbedList(embedBuilder, spotifyTracks.size() > 5 ? spotifyTracks.subList(0, 5) : spotifyTracks, spotifyTrack -> spotifyTrack.exhaustiveMatch(track -> String.format("%s - %s - %s", track.getName(), StringList.create(track.getArtists(), ArtistSimplified::getName).toSeparatedString(", "), Util.normalizeMillis(track.getDurationMs() != null ? track.getDurationMs() : 0)), episode -> String.format("%s - %s - %s", episode.getName(), episode.getShow() != null ? episode.getShow().getName() : "", Util.normalizeMillis(episode.getDurationMs() != null ? episode.getDurationMs() : 0))), "Track - Artist - Duration");
}
sendMessage(embedBuilder);
}
Aggregations