Package org.localify.data
Class SpotifyService
java.lang.Object
org.localify.data.SpotifyService
Service for interacting with the Spotify API.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA holder for Spotify credentials. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ThreadPoolExecutorThe thread pool executor for running Spotify API calls.static final SemaphoreA semaphore for limiting the number of concurrent Spotify API calls. -
Constructor Summary
ConstructorsConstructorDescriptionSpotifyService(GenreRepository genreRepository, ArtistGenreRepository artistGenreRepository, SongRepository songRepository, ArtistSongRepository artistSongRepository, UserRepository userRepository, UserSeedRepository userSeedRepository, ArtistChangeRepository artistChangeRepository, UserTopArtistsSnapshotRepository userTopArtistsSnapshotRepository, UserTopArtistsSnapshotEntryRepository userTopArtistsSnapshotEntryRepository, PlaylistSongPairRepository playlistSongPairRepository, PlaylistRepository playlistRepository, UserCityRepository userCityRepository) Constructs a new SpotifyService. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddArtistGenres(Artist artist, String[] genres) Adds genres to an artist.voidbulkUpdateArtists(List<Artist> artists) Update a list of artists already in spotify in bulk.createArtistById(String spotifyId) Creates an artist from a Spotify ID.createArtistsByAlbumIds(List<String> spotifyAlbumIds) Creates a list of artists from a list of Spotify album IDs.createArtistsByIds(List<String> spotifyIds) Creates a list of artists from a list of Spotify IDs.createArtistsByTrackIds(List<String> spotifyTrackIds) Creates a list of artists from a list of Spotify track IDs.createPlaylist(String name, String description) Creates a playlist.Finds and creates a list of artists from a name.findOrCreateSong(se.michaelthelin.spotify.model_objects.specification.Track track) Finds or creates a song from a Spotify track.String[]flattenPlaylistToSpotifyIds(Playlist playlist) Flattens a playlist to a list of Spotify track IDs.se.michaelthelin.spotify.model_objects.miscellaneous.AudioAnalysisgetAnalysisForTrack(String spotifyId) Gets the audio analysis for a track.se.michaelthelin.spotify.model_objects.specification.ArtistgetBySpotifyId(String id) Gets an artist by their Spotify ID.se.michaelthelin.spotify.model_objects.specification.AudioFeaturesgetFeaturesForTrack(String spotifyId) Gets the audio features for a track.getIsrc(se.michaelthelin.spotify.model_objects.specification.Track track) Gets the ISRC for a track.static StringgetLargestImage(se.michaelthelin.spotify.model_objects.specification.Image[] images) Gets the URL of the largest image from an array of images.getOrCreateGenreByName(String name) Gets or creates a genre by name.getOrCreatePlaylist(Playlist playlist) Gets or creates a playlist.getPlaylist(String spotifyId) Gets a playlist by its Spotify ID.List<se.michaelthelin.spotify.model_objects.specification.Track>getTopTracksBySpotifyId(String id, com.neovisionaries.i18n.CountryCode countryCode) Gets an artist's top tracks by their Spotify ID.voidgetUserSeedsForFrame(String timeFrame, User ur, int limit) Gets a user's top artists for a given time frame.voidmakePlaylistPrivate(Playlist playlist, User user) Makes a playlist private.booleanplaylistHasSongs(Playlist playlist) Checks if a playlist has songs.voidrefreshUserCreds(User ur) Refreshes a user's Spotify credentials.voidsavePlaylistToSpotify(Playlist playlist, User user) Saves a playlist to Spotify.voidSaves a list of songs for an artist.voidsaveUserPlaylistToSpotify(Playlist playlist, UUID userId) Saves a user's playlist to Spotify.voidsharePlaylist(Playlist playlist, User user) Shares a playlist with a user.voidupdateArtist(UUID artistId) Update an artist already in spotify.voidUpdates an artist's top tracks.voidupdateSong(Song song, se.michaelthelin.spotify.model_objects.specification.Track track) Updates a song with data from a Spotify track.voidUpdates a user's top artists snapshot.voidupdateUserSeed(User ur, Artist ar) Updates a user's seed artists.voidupdateUserTopArtists(UUID userId) Updates a user's top artists.
-
Field Details
-
executor
The thread pool executor for running Spotify API calls. -
semaphore
A semaphore for limiting the number of concurrent Spotify API calls.
-
-
Constructor Details
-
SpotifyService
public SpotifyService(GenreRepository genreRepository, ArtistGenreRepository artistGenreRepository, SongRepository songRepository, ArtistSongRepository artistSongRepository, UserRepository userRepository, UserSeedRepository userSeedRepository, ArtistChangeRepository artistChangeRepository, UserTopArtistsSnapshotRepository userTopArtistsSnapshotRepository, UserTopArtistsSnapshotEntryRepository userTopArtistsSnapshotEntryRepository, PlaylistSongPairRepository playlistSongPairRepository, PlaylistRepository playlistRepository, UserCityRepository userCityRepository) Constructs a new SpotifyService.- Parameters:
genreRepository- The genre repository.artistGenreRepository- The artist-genre repository.songRepository- The song repository.artistSongRepository- The artist-song repository.userRepository- The user repository.userSeedRepository- The user seed repository.artistChangeRepository- The artist change repository.userTopArtistsSnapshotRepository- The user top artists snapshot repository.userTopArtistsSnapshotEntryRepository- The user top artists snapshot entry repository.playlistSongPairRepository- The playlist-song pair repository.playlistRepository- The playlist repository.userCityRepository- The user-city repository.
-
-
Method Details
-
refreshUserCreds
Refreshes a user's Spotify credentials.- Parameters:
ur- The user to refresh credentials for.
-
getBySpotifyId
Gets an artist by their Spotify ID.- Parameters:
id- The Spotify ID.- Returns:
- The artist.
-
getTopTracksBySpotifyId
public List<se.michaelthelin.spotify.model_objects.specification.Track> getTopTracksBySpotifyId(String id, com.neovisionaries.i18n.CountryCode countryCode) Gets an artist's top tracks by their Spotify ID.- Parameters:
id- The Spotify ID.countryCode- The country code.- Returns:
- A list of tracks.
-
addArtistGenres
Adds genres to an artist.- Parameters:
artist- The artist.genres- The genres to add.
-
updateArtist
Update an artist already in spotify.- Parameters:
artistId- the artist's id
-
bulkUpdateArtists
Update a list of artists already in spotify in bulk.- Parameters:
artists- the list of artists (limit 50)
-
updateArtistTracks
Updates an artist's top tracks.- Parameters:
ar- The artist to update.
-
getIsrc
Gets the ISRC for a track.- Parameters:
track- The track.- Returns:
- The ISRC.
-
updateSong
Updates a song with data from a Spotify track.- Parameters:
song- The song to update.track- The Spotify track.
-
findOrCreateSong
@Transactional public Song findOrCreateSong(se.michaelthelin.spotify.model_objects.specification.Track track) Finds or creates a song from a Spotify track.- Parameters:
track- The Spotify track.- Returns:
- The found or created song.
-
saveSongs
Saves a list of songs for an artist.- Parameters:
artist- The artist.songs- The list of songs.
-
getOrCreateGenreByName
Gets or creates a genre by name.- Parameters:
name- The name of the genre.- Returns:
- The genre.
-
getLargestImage
public static String getLargestImage(se.michaelthelin.spotify.model_objects.specification.Image[] images) Gets the URL of the largest image from an array of images.- Parameters:
images- The array of images.- Returns:
- The URL of the largest image.
-
getUserSeedsForFrame
Gets a user's top artists for a given time frame.- Parameters:
timeFrame- The time frame (e.g., "long_term", "medium_term", "short_term").ur- The user.limit- The maximum number of artists to return.
-
updateUserTopArtists
Updates a user's top artists.- Parameters:
userId- The ID of the user.
-
updateUserSeed
Updates a user's seed artists.- Parameters:
ur- The user.ar- The artist.
-
updateTopArtistsSnapshotAndEntry
Updates a user's top artists snapshot.- Parameters:
ur- The user.ar- The artist.
-
createArtistById
Creates an artist from a Spotify ID.- Parameters:
spotifyId- The Spotify ID.- Returns:
- The created artist.
-
createArtistsByIds
Creates a list of artists from a list of Spotify IDs.- Parameters:
spotifyIds- The list of Spotify IDs.- Returns:
- The list of created artists.
-
createArtistsByTrackIds
Creates a list of artists from a list of Spotify track IDs.- Parameters:
spotifyTrackIds- The list of Spotify track IDs.- Returns:
- The list of created artists.
-
createArtistsByAlbumIds
Creates a list of artists from a list of Spotify album IDs.- Parameters:
spotifyAlbumIds- The list of Spotify album IDs.- Returns:
- The list of created artists.
-
findAndCreateSpotifyIdsByName
Finds and creates a list of artists from a name.- Parameters:
name- The name of the artist.- Returns:
- The list of found or created artists.
-
getAnalysisForTrack
public se.michaelthelin.spotify.model_objects.miscellaneous.AudioAnalysis getAnalysisForTrack(String spotifyId) Gets the audio analysis for a track.- Parameters:
spotifyId- The Spotify ID of the track.- Returns:
- The audio analysis.
-
getFeaturesForTrack
public se.michaelthelin.spotify.model_objects.specification.AudioFeatures getFeaturesForTrack(String spotifyId) Gets the audio features for a track.- Parameters:
spotifyId- The Spotify ID of the track.- Returns:
- The audio features.
-
getPlaylist
Gets a playlist by its Spotify ID.- Parameters:
spotifyId- The Spotify ID.- Returns:
- The playlist ID, or null if not found.
-
createPlaylist
Creates a playlist.- Parameters:
name- The name of the playlist.description- The description of the playlist.- Returns:
- The ID of the created playlist.
-
getOrCreatePlaylist
Gets or creates a playlist.- Parameters:
playlist- The playlist.- Returns:
- The Spotify ID of the playlist.
-
savePlaylistToSpotify
@Transactional(propagation=REQUIRES_NEW) public void savePlaylistToSpotify(Playlist playlist, User user) Saves a playlist to Spotify.- Parameters:
playlist- The playlist to save.user- The user who owns the playlist.
-
saveUserPlaylistToSpotify
Saves a user's playlist to Spotify.- Parameters:
playlist- The playlist to save.userId- The ID of the user.
-
makePlaylistPrivate
Makes a playlist private.- Parameters:
playlist- The playlist.user- The user who owns the playlist.
-
flattenPlaylistToSpotifyIds
Flattens a playlist to a list of Spotify track IDs.- Parameters:
playlist- The playlist.- Returns:
- An array of Spotify track IDs.
-
playlistHasSongs
Checks if a playlist has songs.- Parameters:
playlist- The playlist.- Returns:
- True if the playlist has songs, false otherwise.
-