Package com.tagtraum.ubermusic.lastfm
Class LastFM
- java.lang.Object
-
- com.tagtraum.core.WebService
-
- com.tagtraum.ubermusic.lastfm.LastFM
-
- All Implemented Interfaces:
TagProvider,UberMusic
public class LastFM extends WebService implements UberMusic, TagProvider
LastFM.- Author:
- Hendrik Schreiber
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLastFM.Session
-
Field Summary
-
Fields inherited from class com.tagtraum.core.WebService
HTTP_METHOD_GET, HTTP_METHOD_POST, HTTP_METHOD_PUT
-
-
Constructor Summary
Constructors Constructor Description LastFM()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AudioArtistartistInfo(String artist, String mbid)Fetch artists for the given artist from Last.fm.List<Similarity<AudioArtist>>artistSimilar(String artist, String mbid)Fetch similar artists for the given artist from Last.fm.LastFM.SessionauthGetSession(String token)Gets a session key for the token.StringauthGetToken()Obtains token necessary for authorization.protected booleancacheResource(URL url, byte[] content)Allows turning caching for a specific resource and its content on and off.SessioncreateSession(LastFM.Session session)SessioncreateSession(String sessionKey, String username)static MoodderiveMood(List<Tag> tags)Attempt to derive a mood from the given tags.List<AudioArtist>getArtists(AudioSong song)Lookup artists for a song.URLgetAuthorizationURL(String token)Needed to authorize this application for a given user.protected org.json.simple.JSONObjectgetJSONObject(String request, Charset responseCharset, long maximumAge, TimeUnit timeUnit)Utility method to get a JSON dictionary from a webservice.protected org.json.simple.JSONObjectgetJSONObject(String request, Charset responseCharset, Map<String,String> parameters)List<Similarity<AudioArtist>>getSimilarArtists(AudioSong song)Try to find similar artists for the given song.List<Tag>getTags(AudioSong song)Fetch the top tags for the given song from Last.fm.URLgetURL(AudioSong song)Obtain a Last.fm-URL for the given song.protected voidhandleStatusCode(HttpURLConnection httpURLConnection, int statusCode)Handles standard responses to status codes.voidlibraryAddTrack(AudioSong song, String sessionKey)Adds a song to the user's library.voidlibraryAddTrack(String name, String artist, String sessionKey)Adds a song to the user's library.List<AudioSong>lookup(AudioSong song)Looks up metadata for a song.List<AudioSong>lookup(String name, String artist, String mbid)List<AudioSong>tagGetTopTracks(String tag, int page)Get the top tracks for a given tag.booleantrackScrobble(AudioSong song, long startedPlaying, String sessionKey)A track should only be scrobbled when the following conditions have been met: The track must be longer than 30 seconds. And the track has been played for at least half its duration, or for 4 minutes (whichever occurs earlier.) As soon as these conditions have been met, the scrobble request may be sent at any time.booleantrackScrobble(String name, String artist, String album, String albumArtist, int trackNumber, String mbid, int totalTime, long startedPlaying, String sessionKey)List<Tag>trackTopTags(String name, String artist, String mbid)Fetch the top tags for the given track from Last.fm.voidtrackUpdateNowPlaying(AudioSong song, String sessionKey)Update "now playing".voidtrackUpdateNowPlaying(String name, String artist, String album, String albumArtist, int trackNumber, String mbid, int totalTime, String sessionKey)Update "now playing".-
Methods inherited from class com.tagtraum.core.WebService
clearCache, getCache, getConnectTimeOut, getDomFactory, getJSONArray, getJSONArray, getJSONObject, getJSONObjectWithPayload, getMaxAge, getReadTimeOut, getRequestDelay, getSSLSocketFactory, getUserAgent, getXmlDocument, getXmlObject, isAcceptGzipContentEncoding, openConnection, openConnection, openInputStream, readURL, setAcceptGzipContentEncoding, setCache, setConnectTimeOut, setDomFactory, setMaxAge, setNextAllowedConnection, setReadTimeOut, setRequestDelay, setSSLSocketFactory, setUserAgent, toParameterString, useCacheOnly, waitForTurn
-
-
-
-
Method Detail
-
deriveMood
public static Mood deriveMood(List<Tag> tags)
Attempt to derive a mood from the given tags.- Parameters:
tags- tags- Returns:
- derived mood or null, if we were unable to derive one
-
createSession
public Session createSession(LastFM.Session session)
-
authGetToken
public String authGetToken() throws IOException
Obtains token necessary for authorization.- Returns:
- token
- Throws:
IOException- if we cannot get a token- See Also:
getAuthorizationURL(String)
-
authGetSession
public LastFM.Session authGetSession(String token) throws IOException
Gets a session key for the token. This is only possible after the user authorized the app viagetAuthorizationURL(String).
Session keys have an infinite lifetime by default. You are recommended to store the key securely. Users are able to revoke privileges for your application on their Last.fm settings screen, rendering session keys invalid.- Parameters:
token- token- Returns:
- session
- Throws:
IOException- if we cannot get a session- See Also:
authGetToken(),getAuthorizationURL(String)
-
getAuthorizationURL
public URL getAuthorizationURL(String token) throws IOException
Needed to authorize this application for a given user.- Parameters:
token- token- Returns:
- url
- Throws:
IOException- should the request for a token, necessary to build the URL, fail- See Also:
authGetToken(), http://www.last.fm/api/authspec
-
tagGetTopTracks
public List<AudioSong> tagGetTopTracks(String tag, int page) throws IOException
Get the top tracks for a given tag.- Parameters:
tag- tag- Returns:
- list of tracks
- Throws:
IOException- if something goes wrong
-
libraryAddTrack
public void libraryAddTrack(AudioSong song, String sessionKey) throws IOException
Adds a song to the user's library.- Parameters:
song- songsessionKey- (required) session key- Throws:
IOException- if we cannot add the track
-
libraryAddTrack
public void libraryAddTrack(String name, String artist, String sessionKey) throws IOException
Adds a song to the user's library.- Parameters:
name- (required) titleartist- (required) artistsessionKey- (required) session key- Throws:
IOException- if we cannot add the track- See Also:
libraryAddTrack(com.tagtraum.audiokern.AudioSong, String)
-
trackUpdateNowPlaying
public void trackUpdateNowPlaying(AudioSong song, String sessionKey) throws IOException
Update "now playing".- Parameters:
song- songsessionKey- key- Throws:
IOException- if "now playing" fails
-
trackUpdateNowPlaying
public void trackUpdateNowPlaying(String name, String artist, String album, String albumArtist, int trackNumber, String mbid, int totalTime, String sessionKey) throws IOException
Update "now playing".- Parameters:
name- nameartist- artistalbum- albumalbumArtist- albumArtisttrackNumber- trackNumbermbid- mbidtotalTime- totalTimesessionKey- session key- Throws:
IOException- if we cannot update "now playing"
-
trackScrobble
public boolean trackScrobble(AudioSong song, long startedPlaying, String sessionKey) throws IOException
A track should only be scrobbled when the following conditions have been met:- The track must be longer than 30 seconds.
- And the track has been played for at least half its duration, or for 4 minutes (whichever occurs earlier.)
- Parameters:
song- songstartedPlaying- (required) time in seconds since 1970sessionKey- (required) session key- Returns:
- true, if the scrobble was accepted
- Throws:
IOException- if scroblle fails- See Also:
trackScrobble(String, String, String, String, int, String, int, long, String),trackUpdateNowPlaying(com.tagtraum.audiokern.AudioSong, String)
-
trackScrobble
public boolean trackScrobble(String name, String artist, String album, String albumArtist, int trackNumber, String mbid, int totalTime, long startedPlaying, String sessionKey) throws IOException
- Parameters:
name- (required)artist- (required)album- albumalbumArtist- albumArtisttrackNumber- trackNumbermbid- mbidtotalTime- totalTimestartedPlaying- (required)sessionKey- (required)- Returns:
- true, if the scrobble was accepted
- Throws:
IOException- if scroblle fails- See Also:
trackScrobble(com.tagtraum.audiokern.AudioSong, long, String)
-
lookup
public List<AudioSong> lookup(AudioSong song) throws IOException
Description copied from interface:UberMusicLooks up metadata for a song.- Specified by:
lookupin interfaceUberMusic- Parameters:
song- song (query by example)- Returns:
- reference songs, empty list, if the metadata is unavailable
- Throws:
IOException- IOException
-
lookup
public List<AudioSong> lookup(String name, String artist, String mbid) throws IOException
- Throws:
IOException
-
getURL
public URL getURL(AudioSong song) throws IOException
Obtain a Last.fm-URL for the given song. If no URL can be found, the site-search URL is returned.- Parameters:
song- song- Returns:
- url
- Throws:
IOException- if we cannot get the URL
-
getTags
public List<Tag> getTags(AudioSong song) throws IOException
Fetch the top tags for the given song from Last.fm.- Specified by:
getTagsin interfaceTagProvider- Parameters:
song- song- Returns:
- list of tags
- Throws:
IOException- if top tags fails- See Also:
- track.getTopTags,
trackTopTags(String, String, String)
-
trackTopTags
public List<Tag> trackTopTags(String name, String artist, String mbid) throws IOException
Fetch the top tags for the given track from Last.fm.- Parameters:
name- track nameartist- artist namembid- musicbrainz track id, if available, otherwisenull- Returns:
- list of tags
- Throws:
IOException- if top tags fails- See Also:
- track.getTopTags
-
getArtists
public List<AudioArtist> getArtists(AudioSong song) throws IOException
Description copied from interface:UberMusicLookup artists for a song. The default implementation returns an empty list.- Specified by:
getArtistsin interfaceUberMusic- Parameters:
song- song- Returns:
- list of similar artists that seem to match the given song
- Throws:
IOException- IOException
-
getSimilarArtists
public List<Similarity<AudioArtist>> getSimilarArtists(AudioSong song) throws IOException
Description copied from interface:UberMusicTry to find similar artists for the given song. The default implementation returns an empty list.- Specified by:
getSimilarArtistsin interfaceUberMusic- Parameters:
song- song- Returns:
- list of similar artists
- Throws:
IOException- IOException
-
artistSimilar
public List<Similarity<AudioArtist>> artistSimilar(String artist, String mbid) throws IOException
Fetch similar artists for the given artist from Last.fm.- Parameters:
artist- artist namembid- artist musicbrainz id, if available- Returns:
- list of artists
- Throws:
IOException- if similar artists cannot be retrieved- See Also:
- artist.getSimilar
-
artistInfo
public AudioArtist artistInfo(String artist, String mbid) throws IOException
Fetch artists for the given artist from Last.fm.- Parameters:
artist- artist namembid- artist musicbrainz id, if available- Returns:
- artist
- Throws:
IOException- if similar artists cannot be retrieved- See Also:
- artist.getSimilar
-
handleStatusCode
protected void handleStatusCode(HttpURLConnection httpURLConnection, int statusCode) throws IOException
Description copied from class:WebServiceHandles standard responses to status codes.- Overrides:
handleStatusCodein classWebService- Parameters:
httpURLConnection- http url connectionstatusCode- status code- Throws:
IOException
-
getJSONObject
protected org.json.simple.JSONObject getJSONObject(String request, Charset responseCharset, long maximumAge, TimeUnit timeUnit) throws IOException, org.json.simple.parser.ParseException
Description copied from class:WebServiceUtility method to get a JSON dictionary from a webservice.- Overrides:
getJSONObjectin classWebService- Parameters:
request- request URLresponseCharset- character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignoredmaximumAge- up to this age the cache is consultedtimeUnit- max age time unit- Returns:
- JSON dictionary
- Throws:
IOException- if something goes wrongorg.json.simple.parser.ParseException- if the JSON is malformed
-
getJSONObject
protected org.json.simple.JSONObject getJSONObject(String request, Charset responseCharset, Map<String,String> parameters) throws IOException, org.json.simple.parser.ParseException
- Throws:
IOExceptionorg.json.simple.parser.ParseException
-
cacheResource
protected boolean cacheResource(URL url, byte[] content)
Description copied from class:WebServiceAllows turning caching for a specific resource and its content on and off.- Overrides:
cacheResourcein classWebService- Parameters:
url- urlcontent- resource content- Returns:
- by default this method returns
true
-
-