Class LastFM

    • Constructor Detail

      • LastFM

        public LastFM()
    • 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
      • 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 via getAuthorizationURL(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)
      • 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 - song
        sessionKey - (required) session key
        Throws:
        IOException - if we cannot add the track
      • trackUpdateNowPlaying

        public void trackUpdateNowPlaying​(AudioSong song,
                                          String sessionKey)
                                   throws IOException
        Update "now playing".
        Parameters:
        song - song
        sessionKey - 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 - name
        artist - artist
        album - album
        albumArtist - albumArtist
        trackNumber - trackNumber
        mbid - mbid
        totalTime - totalTime
        sessionKey - 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.)
        As soon as these conditions have been met, the scrobble request may be sent at any time. It is often most convenient to send a scrobble request when a track has finished playing. *
        Parameters:
        song - song
        startedPlaying - (required) time in seconds since 1970
        sessionKey - (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)
      • lookup

        public List<AudioSong> lookup​(AudioSong song)
                               throws IOException
        Description copied from interface: UberMusic
        Looks up metadata for a song.
        Specified by:
        lookup in interface UberMusic
        Parameters:
        song - song (query by example)
        Returns:
        reference songs, empty list, if the metadata is unavailable
        Throws:
        IOException - 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
      • 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 name
        artist - artist name
        mbid - musicbrainz track id, if available, otherwise null
        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: UberMusic
        Lookup artists for a song. The default implementation returns an empty list.
        Specified by:
        getArtists in interface UberMusic
        Parameters:
        song - song
        Returns:
        list of similar artists that seem to match the given song
        Throws:
        IOException - IOException
      • artistInfo

        public AudioArtist artistInfo​(String artist,
                                      String mbid)
                               throws IOException
        Fetch artists for the given artist from Last.fm.
        Parameters:
        artist - artist name
        mbid - artist musicbrainz id, if available
        Returns:
        artist
        Throws:
        IOException - if similar artists cannot be retrieved
        See Also:
        artist.getSimilar
      • 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: WebService
        Utility method to get a JSON dictionary from a webservice.
        Overrides:
        getJSONObject in class WebService
        Parameters:
        request - request URL
        responseCharset - character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignored
        maximumAge - up to this age the cache is consulted
        timeUnit - max age time unit
        Returns:
        JSON dictionary
        Throws:
        IOException - if something goes wrong
        org.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
        Description copied from class: WebService
        Utility method to post parameters and get a JSON dictionary result from a webservice.
        Overrides:
        getJSONObject in class WebService
        Parameters:
        request - request URL
        responseCharset - character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignored
        parameters - parameters to send
        Returns:
        JSON dictionary
        Throws:
        IOException - if something goes wrong
        org.json.simple.parser.ParseException - if the JSON is malformed
      • cacheResource

        protected boolean cacheResource​(URL url,
                                        byte[] content)
        Description copied from class: WebService
        Allows turning caching for a specific resource and its content on and off.
        Overrides:
        cacheResource in class WebService
        Parameters:
        url - url
        content - resource content
        Returns:
        by default this method returns true