Search in sources :

Example 6 with Scrobble

use of com.github.hakko.musiccabinet.domain.model.aggr.Scrobble in project musiccabinet by hakko.

the class ScrobbleService method receive.

@SuppressWarnings("unchecked")
protected void receive() throws ApplicationException {
    Message<Scrobble> message;
    while ((message = (Message<Scrobble>) scrobbleChannel.receive()) != null) {
        Scrobble scrobble = message.getPayload();
        Scrobble previous = getPrevious(scrobble);
        if (previous != null && tooClose(scrobble, previous) && scrobble.getTrack().getId() == previous.getTrack().getId()) {
            LOG.debug("Same track was scrobbled just recently, ignore.");
        } else {
            addScrobble(scrobble);
            WSResponse wsResponse = nowPlayingClient.updateNowPlaying(scrobble);
            if (!wsResponse.wasCallSuccessful()) {
                LOG.debug("Could not update now playing status at last.fm.");
                LOG.debug("Nowplaying response: " + wsResponse);
            }
        }
    }
}
Also used : Scrobble(com.github.hakko.musiccabinet.domain.model.aggr.Scrobble) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse)

Example 7 with Scrobble

use of com.github.hakko.musiccabinet.domain.model.aggr.Scrobble in project musiccabinet by hakko.

the class ScrobbleService method addScrobble.

private void addScrobble(Scrobble scrobble) {
    ConcurrentLinkedDeque<Scrobble> deque = userScrobbles.get(scrobble.getLastFmUser());
    Scrobble tail;
    while ((tail = deque.peekLast()) != null && tooClose(tail, scrobble)) {
        // indicates the occurrence of a previous track that was played for a few
        // seconds, and that should be removed
        deque.pollLast();
    }
    deque.add(scrobble);
}
Also used : Scrobble(com.github.hakko.musiccabinet.domain.model.aggr.Scrobble)

Example 8 with Scrobble

use of com.github.hakko.musiccabinet.domain.model.aggr.Scrobble in project musiccabinet by hakko.

the class ScrobbleService method scrobbleTracks.

protected void scrobbleTracks() throws ApplicationException {
    scrobbleFailedTracks();
    Scrobble head;
    for (LastFmUser lastFmUser : userScrobbles.keySet()) {
        ConcurrentLinkedDeque<Scrobble> deque = userScrobbles.get(lastFmUser);
        while ((head = deque.peekFirst()) != null && !tooClose(head, new DateTime())) {
            playCountDao.addPlayCount(head.getLastFmUser(), head.getTrack());
            WSResponse wsResponse = scrobbleClient.scrobble(head);
            if (!wsResponse.wasCallSuccessful()) {
                LOG.warn("scrobbling " + head + " failed! Add for re-sending.");
                LOG.debug("Scrobble response: " + wsResponse);
                failedScrobbles.add(head);
            }
            deque.pollFirst();
        }
    }
}
Also used : LastFmUser(com.github.hakko.musiccabinet.domain.model.library.LastFmUser) Scrobble(com.github.hakko.musiccabinet.domain.model.aggr.Scrobble) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) DateTime(org.joda.time.DateTime)

Example 9 with Scrobble

use of com.github.hakko.musiccabinet.domain.model.aggr.Scrobble in project musiccabinet by hakko.

the class ScrobbleService method scrobble.

/* Async method that registers scrobbles and delegates submissions, in case last.fm is down.
	 *
	 * Submission: Whether this is a "scrobble" or a "now playing" notification.
	 */
public void scrobble(String lastFmUsername, Track track, boolean submission) {
    LastFmUser lastFmUser = lastFmDao.getLastFmUser(lastFmUsername);
    Scrobble scrobble = new Scrobble(lastFmUser, track, submission);
    scrobbleChannel.send(new GenericMessage<Scrobble>(scrobble));
    if (!started.getAndSet(true)) {
        startScrobblingService();
    }
}
Also used : LastFmUser(com.github.hakko.musiccabinet.domain.model.library.LastFmUser) Scrobble(com.github.hakko.musiccabinet.domain.model.aggr.Scrobble)

Example 10 with Scrobble

use of com.github.hakko.musiccabinet.domain.model.aggr.Scrobble in project musiccabinet by hakko.

the class ScrobbleService method scrobbleFailedTracks.

protected void scrobbleFailedTracks() throws ApplicationException {
    while (failedScrobbles.size() > 0) {
        LOG.debug("Queue of failed scrobbles consists of " + failedScrobbles.size() + " elements.");
        Scrobble firstFailed = failedScrobbles.get(0);
        WSResponse wsResponse = scrobbleClient.scrobble(firstFailed);
        if (wsResponse.wasCallSuccessful()) {
            LOG.debug("Failed scrobble was re-sent.");
            failedScrobbles.remove(0);
        } else {
            LOG.debug("Failed scrobble could not be re-sent. Wait a minute before trying again.");
            LOG.debug("Response: " + wsResponse);
            return;
        }
    }
}
Also used : Scrobble(com.github.hakko.musiccabinet.domain.model.aggr.Scrobble) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse)

Aggregations

Scrobble (com.github.hakko.musiccabinet.domain.model.aggr.Scrobble)12 Test (org.junit.Test)5 Message (org.springframework.integration.Message)5 WSResponse (com.github.hakko.musiccabinet.ws.lastfm.WSResponse)4 PollableChannel (org.springframework.integration.core.PollableChannel)4 GenericMessage (org.springframework.integration.message.GenericMessage)4 LastFmUser (com.github.hakko.musiccabinet.domain.model.library.LastFmUser)3 ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)2 Track (com.github.hakko.musiccabinet.domain.model.music.Track)1 NameValuePair (org.apache.http.NameValuePair)1 DateTime (org.joda.time.DateTime)1