Search in sources :

Example 1 with Track

use of se.michaelthelin.spotify.model_objects.specification.Track in project spotifybot by NotEchoDE.

the class LastSongCommand method exec.

@Override
@SneakyThrows
public void exec(String userName, String id, UserLevel userLevel, String[] args) {
    PagingCursorbased<PlayHistory> historyPagingCursorbased = getRoot().getSpotifyApi().getCurrentUsersRecentlyPlayedTracks().limit(1).build().execute();
    String trackId = historyPagingCursorbased.getItems()[0].getTrack().getId();
    Track track = getRoot().getSpotifyApi().getTrack(trackId).build().execute();
    sendMessage(getModule().getEntry("lastSong"), "$USER", userName, "$LASTSONG", track.getName(), "$ARTISTS", SpotifyUtils.getArtists(track));
}
Also used : PlayHistory(se.michaelthelin.spotify.model_objects.specification.PlayHistory) Track(se.michaelthelin.spotify.model_objects.specification.Track) SneakyThrows(lombok.SneakyThrows)

Example 2 with Track

use of se.michaelthelin.spotify.model_objects.specification.Track in project spotifybot by NotEchoDE.

the class PlayCommand method exec.

@SneakyThrows
@Override
public void exec(String userName, String id, UserLevel userLevel, String[] args) {
    ModuleEntry sPlay = getModule().getEntry("sPlay");
    if (!userLevel.isHigherOrEquals(sPlay.getUserLevel())) {
        sendMessage(getModule(ModuleType.SYSTEM).getEntry("noPerms"), "$USER", userName, "$ROLE", sPlay.getUserLevel().getPrettyName());
        return;
    }
    if (args.length >= 1) {
        StringBuilder searchQuery = new StringBuilder();
        for (int i = 0; i < args.length; i++) if (i != 0)
            searchQuery.append(" ");
        else
            searchQuery.append(args[i]);
        Paging<Track> search = getRoot().getSpotifyApi().searchTracks(searchQuery.toString()).build().execute();
        if (search.getTotal() == 0) {
            sendMessage(getModule(ModuleType.SONGREQUEST).getEntry("notFound"), "$USER", userName);
            return;
        }
        String uri = search.getItems()[0].getUri();
        CountryCode country = getRoot().getSpotifyApi().getCurrentUsersProfile().build().execute().getCountry();
        Track track = getRoot().getSpotifyApi().getTrack(uri.replace("spotify:track:", "")).build().execute();
        if (Arrays.stream(track.getAvailableMarkets()).noneMatch(countryCode -> countryCode.equals(CountryCode.DE))) {
            sendMessage(getModule(ModuleType.SONGREQUEST).getEntry("notAvailable"), "$USER", userName, "$SONG", track.getName());
            return;
        }
        getRoot().getSpotifyApi().addItemToUsersPlaybackQueue(uri).build().execute();
        getRoot().getSpotifyApi().skipUsersPlaybackToNextTrack().build().execute();
        sendMessage(sPlay, "$USER", userName, "$SONG", track.getName());
        return;
    }
    getRoot().getSpotifyApi().startResumeUsersPlayback().build().execute();
    String uri = SpotifyUtils.getUriFromJson(getRoot().getSpotifyApi().getUsersCurrentlyPlayingTrack().build().getJson());
    Track track = getRoot().getSpotifyApi().getTrack(SpotifyUtils.getIdFromUri(uri)).build().execute();
    sendMessage(sPlay, "$USER", userName, "$SONG", track.getName(), "$ARTISTS", SpotifyUtils.getArtists(track));
}
Also used : ModuleEntry(de.notecho.spotify.database.user.entities.module.ModuleEntry) CountryCode(com.neovisionaries.i18n.CountryCode) Track(se.michaelthelin.spotify.model_objects.specification.Track) SneakyThrows(lombok.SneakyThrows)

Example 3 with Track

use of se.michaelthelin.spotify.model_objects.specification.Track in project spotifybot by NotEchoDE.

the class SongrequestReward method exec.

@SneakyThrows
@Override
public void exec(RewardRedeemedEvent event, String userName, String id, String message) {
    Paging<Track> search = getRoot().getSpotifyApi().searchTracks(message).build().execute();
    if (search.getTotal() == 0) {
        sendMessage(getModule().getEntry("notFound"), "$USER", userName);
        setRedemptionStatus(event, RedemptionStatus.CANCELED);
        return;
    }
    String uri = search.getItems()[0].getUri();
    Track track = getRoot().getSpotifyApi().getTrack(uri.replace("spotify:track:", "")).build().execute();
    if (Arrays.stream(track.getAvailableMarkets()).noneMatch(countryCode -> countryCode.equals(CountryCode.DE))) {
        sendMessage(getModule(ModuleType.SONGREQUEST).getEntry("notAvailable"), "$USER", userName, "$SONG", track.getName());
        setRedemptionStatus(event, RedemptionStatus.CANCELED);
        return;
    }
    getRoot().getSpotifyApi().addItemToUsersPlaybackQueue(uri).build().execute();
    sendMessage(getModule().getEntry("requested"), "$USER", userName, "$SONG", track.getName());
}
Also used : Track(se.michaelthelin.spotify.model_objects.specification.Track) SneakyThrows(lombok.SneakyThrows)

Example 4 with Track

use of se.michaelthelin.spotify.model_objects.specification.Track in project aiode by robinfriedli.

the class SearchCommand method searchSpotifyTrack.

private void searchSpotifyTrack() throws Exception {
    if (getCommandInput().isBlank()) {
        throw new InvalidCommandException("No search term entered");
    }
    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.isEmpty()) {
        EmbedBuilder embedBuilder = new EmbedBuilder();
        Util.appendEmbedList(embedBuilder, found, track -> track.getName() + " - " + track.getAlbum().getName() + " - " + StringList.create(track.getArtists(), ArtistSimplified::getName).toSeparatedString(", "), "Track - Album - Artist");
        sendMessage(embedBuilder);
    } else {
        throw new NoSpotifyResultsFoundException(String.format("No Spotify track found for '%s'", getCommandInput()));
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StringList(net.robinfriedli.stringlist.StringList) List(java.util.List) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Track(se.michaelthelin.spotify.model_objects.specification.Track) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException)

Example 5 with Track

use of se.michaelthelin.spotify.model_objects.specification.Track 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)

Aggregations

Track (se.michaelthelin.spotify.model_objects.specification.Track)17 List (java.util.List)8 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)8 IOException (java.io.IOException)7 StringList (net.robinfriedli.stringlist.StringList)7 Episode (se.michaelthelin.spotify.model_objects.specification.Episode)7 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)6 YouTubePlaylist (net.robinfriedli.aiode.audio.youtube.YouTubePlaylist)6 AlbumSimplified (se.michaelthelin.spotify.model_objects.specification.AlbumSimplified)6 PlaylistSimplified (se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified)6 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)5 SneakyThrows (lombok.SneakyThrows)5 YouTubeVideo (net.robinfriedli.aiode.audio.youtube.YouTubeVideo)5 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)5 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)5 AudioItem (com.sedmelluq.discord.lavaplayer.track.AudioItem)4 YouTubeService (net.robinfriedli.aiode.audio.youtube.YouTubeService)4 NoSpotifyResultsFoundException (net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException)4 ParseException (org.apache.hc.core5.http.ParseException)4 SpotifyWebApiException (se.michaelthelin.spotify.exceptions.SpotifyWebApiException)4