Search in sources :

Example 1 with ArtistSimplified

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);
        }
    }
}
Also used : StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Playable(net.robinfriedli.aiode.audio.Playable) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) Callable(java.util.concurrent.Callable) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) Lists(com.google.common.collect.Lists) Playlist(net.robinfriedli.aiode.entities.Playlist) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) Track(se.michaelthelin.spotify.model_objects.specification.Track) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) SpotifyUri(net.robinfriedli.aiode.audio.spotify.SpotifyUri) UnavailableResourceException(net.robinfriedli.aiode.exceptions.UnavailableResourceException) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) CommandManager(net.robinfriedli.aiode.command.CommandManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) IOException(java.io.IOException) AudioManager(net.robinfriedli.aiode.audio.AudioManager) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) UrlValidator(org.apache.commons.validator.routines.UrlValidator) Aiode(net.robinfriedli.aiode.Aiode) ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) List(java.util.List) AudioTrackLoader(net.robinfriedli.aiode.audio.AudioTrackLoader) SpotifyTrackResultHandler(net.robinfriedli.aiode.audio.spotify.SpotifyTrackResultHandler) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) CommandContext(net.robinfriedli.aiode.command.CommandContext) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) TrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.TrackLoadingExecutor) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) SpotifyTrackResultHandler(net.robinfriedli.aiode.audio.spotify.SpotifyTrackResultHandler) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) StringList(net.robinfriedli.stringlist.StringList) List(java.util.List) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Track(se.michaelthelin.spotify.model_objects.specification.Track) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException)

Example 2 with ArtistSimplified

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;
    }
}
Also used : ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified)

Example 3 with ArtistSimplified

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();
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) StringList(net.robinfriedli.stringlist.StringList) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) LevenshteinDistance(org.apache.commons.text.similarity.LevenshteinDistance)

Example 4 with ArtistSimplified

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(", "));
    }
}
Also used : StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Playable(net.robinfriedli.aiode.audio.Playable) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) Callable(java.util.concurrent.Callable) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) Lists(com.google.common.collect.Lists) Playlist(net.robinfriedli.aiode.entities.Playlist) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) Track(se.michaelthelin.spotify.model_objects.specification.Track) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) SpotifyUri(net.robinfriedli.aiode.audio.spotify.SpotifyUri) UnavailableResourceException(net.robinfriedli.aiode.exceptions.UnavailableResourceException) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) CommandManager(net.robinfriedli.aiode.command.CommandManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) IOException(java.io.IOException) AudioManager(net.robinfriedli.aiode.audio.AudioManager) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) UrlValidator(org.apache.commons.validator.routines.UrlValidator) Aiode(net.robinfriedli.aiode.Aiode) ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) List(java.util.List) AudioTrackLoader(net.robinfriedli.aiode.audio.AudioTrackLoader) SpotifyTrackResultHandler(net.robinfriedli.aiode.audio.spotify.SpotifyTrackResultHandler) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) CommandContext(net.robinfriedli.aiode.command.CommandContext) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) TrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.TrackLoadingExecutor) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) StringList(net.robinfriedli.stringlist.StringList) List(java.util.List) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Track(se.michaelthelin.spotify.model_objects.specification.Track) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException)

Example 5 with ArtistSimplified

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);
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Session(org.hibernate.Session) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) Callable(java.util.concurrent.Callable) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) User(net.dv8tion.jda.api.entities.User) Util(net.robinfriedli.aiode.util.Util) Playlist(net.robinfriedli.aiode.entities.Playlist) AbstractSourceDecidingCommand(net.robinfriedli.aiode.command.commands.AbstractSourceDecidingCommand) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) Track(se.michaelthelin.spotify.model_objects.specification.Track) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) UnavailableResourceException(net.robinfriedli.aiode.exceptions.UnavailableResourceException) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) CommandManager(net.robinfriedli.aiode.command.CommandManager) Collection(java.util.Collection) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Collectors(java.util.stream.Collectors) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Aiode(net.robinfriedli.aiode.Aiode) ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) URLEncoder(java.net.URLEncoder) List(java.util.List) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) CommandContext(net.robinfriedli.aiode.command.CommandContext) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified)

Aggregations

ArtistSimplified (se.michaelthelin.spotify.model_objects.specification.ArtistSimplified)6 ShowSimplified (se.michaelthelin.spotify.model_objects.specification.ShowSimplified)6 StringList (net.robinfriedli.stringlist.StringList)5 IOException (java.io.IOException)4 List (java.util.List)4 Callable (java.util.concurrent.Callable)4 Aiode (net.robinfriedli.aiode.Aiode)4 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)4 YouTubePlaylist (net.robinfriedli.aiode.audio.youtube.YouTubePlaylist)4 YouTubeService (net.robinfriedli.aiode.audio.youtube.YouTubeService)4 YouTubeVideo (net.robinfriedli.aiode.audio.youtube.YouTubeVideo)4 CommandContext (net.robinfriedli.aiode.command.CommandContext)4 CommandManager (net.robinfriedli.aiode.command.CommandManager)4 Playlist (net.robinfriedli.aiode.entities.Playlist)4 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)4 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)4 NoSpotifyResultsFoundException (net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException)4 UnavailableResourceException (net.robinfriedli.aiode.exceptions.UnavailableResourceException)4 SearchEngine (net.robinfriedli.aiode.util.SearchEngine)4 AlbumSimplified (se.michaelthelin.spotify.model_objects.specification.AlbumSimplified)4