Search in sources :

Example 21 with WSResponse

use of com.github.hakko.musiccabinet.ws.lastfm.WSResponse in project musiccabinet by hakko.

the class ArtistTopTracksService method updateSearchIndex.

@Override
protected void updateSearchIndex() throws ApplicationException {
    Set<String> artistNames = webserviceHistoryService.getArtistNamesScheduledForUpdate(ARTIST_GET_TOP_TRACKS);
    setTotalOperations(artistNames.size());
    for (String artistName : artistNames) {
        try {
            WSResponse wsResponse = artistTopTracksClient.getTopTracks(new Artist(artistName));
            if (wsResponse.wasCallAllowed() && wsResponse.wasCallSuccessful()) {
                StringUtil stringUtil = new StringUtil(wsResponse.getResponseBody());
                ArtistTopTracksParser attParser = new ArtistTopTracksParserImpl(stringUtil.getInputStream());
                artistTopTracksDao.createTopTracks(attParser.getArtist(), attParser.getTopTracks());
            }
        } catch (ApplicationException e) {
            LOG.warn("Fetching top tracks for " + artistName + " failed.", e);
        }
        addFinishedOperation();
    }
}
Also used : Artist(com.github.hakko.musiccabinet.domain.model.music.Artist) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) ArtistTopTracksParser(com.github.hakko.musiccabinet.parser.lastfm.ArtistTopTracksParser) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) ArtistTopTracksParserImpl(com.github.hakko.musiccabinet.parser.lastfm.ArtistTopTracksParserImpl)

Example 22 with WSResponse

use of com.github.hakko.musiccabinet.ws.lastfm.WSResponse in project musiccabinet by hakko.

the class TagInfoService method updateSearchIndex.

@Override
protected void updateSearchIndex() throws ApplicationException {
    List<TagInfo> tagInfos = new ArrayList<>();
    Set<String> tags = getTagsForUpdate();
    setTotalOperations(tags.size());
    for (String tag : tags) {
        WSResponse wsResponse = tagInfoClient.getTagInfo(tag, lastFmSettingsService.getLang());
        if (wsResponse.wasCallAllowed() && wsResponse.wasCallSuccessful()) {
            StringUtil stringUtil = new StringUtil(wsResponse.getResponseBody());
            TagInfoParser tiParser = new TagInfoParserImpl(stringUtil.getInputStream());
            tagInfos.add(tiParser.getTagInfo());
        }
        addFinishedOperation();
    }
    tagInfoDao.createTagInfo(tagInfos);
}
Also used : TagInfoParser(com.github.hakko.musiccabinet.parser.lastfm.TagInfoParser) TagInfo(com.github.hakko.musiccabinet.domain.model.music.TagInfo) ArrayList(java.util.ArrayList) TagInfoParserImpl(com.github.hakko.musiccabinet.parser.lastfm.TagInfoParserImpl) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) StringUtil(com.github.hakko.musiccabinet.util.StringUtil)

Example 23 with WSResponse

use of com.github.hakko.musiccabinet.ws.lastfm.WSResponse in project musiccabinet by hakko.

the class UserRecommendedArtistsService method updateSearchIndex.

@Override
protected void updateSearchIndex() throws ApplicationException {
    List<LastFmUser> users = lastFmSettingsService.getLastFmUsers();
    setTotalOperations(users.size());
    List<UserRecommendedArtists> artists = new ArrayList<>();
    for (LastFmUser user : users) {
        try {
            WSResponse wsResponse = userRecommendedArtistsClient.getUserRecommendedArtists(user.getLastFmUsername());
            LOG.debug(wsResponse);
            if (wsResponse.wasCallAllowed() && wsResponse.wasCallSuccessful()) {
                StringUtil stringUtil = new StringUtil(wsResponse.getResponseBody());
                UserRecommendedArtistsParser parser = new UserRecommendedArtistsParserImpl(stringUtil.getInputStream());
                artists.add(new UserRecommendedArtists(user, parser.getArtists()));
            }
        } catch (ApplicationException e) {
            LOG.warn("Fetching top artist for " + user.getLastFmUsername() + " failed.", e);
        }
        addFinishedOperation();
    }
    dao.createUserRecommendedArtists(artists);
}
Also used : UserRecommendedArtists(com.github.hakko.musiccabinet.domain.model.aggr.UserRecommendedArtists) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) UserRecommendedArtistsParserImpl(com.github.hakko.musiccabinet.parser.lastfm.UserRecommendedArtistsParserImpl) UserRecommendedArtistsParser(com.github.hakko.musiccabinet.parser.lastfm.UserRecommendedArtistsParser) ArrayList(java.util.ArrayList) LastFmUser(com.github.hakko.musiccabinet.domain.model.library.LastFmUser) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) StringUtil(com.github.hakko.musiccabinet.util.StringUtil)

Example 24 with WSResponse

use of com.github.hakko.musiccabinet.ws.lastfm.WSResponse in project musiccabinet by hakko.

the class UserLovedTracksServiceTest method updatesStarredTracksWhenImportIsOnlyAllowedForOneUser.

/*
	 * User story: user1 has starred track2, user2 has starred track3 & track4.
	 * When importing loved tracks, user2 is unavailable and user1 has loved track1.
	 * Expected outcome: afterwards, user1 has starred track1 & 2 and user2 track3 & 4.
	 */
@Test
public void updatesStarredTracksWhenImportIsOnlyAllowedForOneUser() throws ApplicationException {
    lastFmDao.createOrUpdateLastFmUser(new LastFmUser(USER1));
    lastFmDao.createOrUpdateLastFmUser(new LastFmUser(USER2));
    LastFmUser user1 = lastFmDao.getLastFmUser(USER1), user2 = lastFmDao.getLastFmUser(USER2);
    Track track1 = new Track("Frank Ocean", "Lost"), track2 = new Track("Kate Bush", "Cloudbusting"), track3 = new Track("Adele", "Skyfall"), track4 = new Track("Kath Bloom", "Fall Again");
    File f1, f2, f3, f4;
    deleteLovedAndStarredTracks();
    submitFile(additionDao, asList(f1 = getFile(track1), f2 = getFile(track2), f3 = getFile(track3), f4 = getFile(track4)));
    int track1Id = browserDao.getTrackId(f1), track2Id = browserDao.getTrackId(f2), track3Id = browserDao.getTrackId(f3), track4Id = browserDao.getTrackId(f4);
    LastFmSettingsService lastFmSettingsService = mock(LastFmSettingsService.class);
    when(lastFmSettingsService.getLastFmUsers()).thenReturn(asList(user1, user2));
    when(lastFmSettingsService.isSyncStarredAndLovedTracks()).thenReturn(true);
    UserLovedTracksClient userLovedTracksClient = mock(UserLovedTracksClient.class);
    when(userLovedTracksClient.getUserLovedTracks(user1, (short) 0)).thenReturn(new WSResponse(new ResourceUtil(LOVED_TRACKS_FILE, UTF8).getContent()));
    when(userLovedTracksClient.getUserLovedTracks(user2, (short) 0)).thenReturn(new WSResponse(false, 403, "Forbidden"));
    UserLovedTracksService userLovedTracksService = new UserLovedTracksService();
    userLovedTracksService.setLastFmSettingsService(lastFmSettingsService);
    userLovedTracksService.setUserLovedTracksClient(userLovedTracksClient);
    userLovedTracksService.setUserLovedTracksDao(userLovedTracksDao);
    userLovedTracksService.setStarService(mock(StarService.class));
    userLovedTracksService.setTrackLoveClient(mock(TrackLoveClient.class));
    starDao.starTrack(user1, track2Id);
    starDao.starTrack(user2, track3Id);
    starDao.starTrack(user2, track4Id);
    userLovedTracksService.updateSearchIndex();
    assertEquals(sort(asList(track1Id, track2Id)), sort(starDao.getStarredTrackIds(user1)));
    assertEquals(sort(asList(track3Id, track4Id)), sort(starDao.getStarredTrackIds(user2)));
}
Also used : UserLovedTracksClient(com.github.hakko.musiccabinet.ws.lastfm.UserLovedTracksClient) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) LastFmUser(com.github.hakko.musiccabinet.domain.model.library.LastFmUser) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) TrackLoveClient(com.github.hakko.musiccabinet.ws.lastfm.TrackLoveClient) UnittestLibraryUtil.getFile(com.github.hakko.musiccabinet.util.UnittestLibraryUtil.getFile) File(com.github.hakko.musiccabinet.domain.model.library.File) UnittestLibraryUtil.submitFile(com.github.hakko.musiccabinet.util.UnittestLibraryUtil.submitFile) Track(com.github.hakko.musiccabinet.domain.model.music.Track) StarService(com.github.hakko.musiccabinet.service.StarService) Test(org.junit.Test)

Example 25 with WSResponse

use of com.github.hakko.musiccabinet.ws.lastfm.WSResponse in project musiccabinet by hakko.

the class UserRecommendedArtistsParserTest method resourceFile2CorrectlyParsed.

@Test
public void resourceFile2CorrectlyParsed() throws ApplicationException {
    WSResponse wsResponse = new WSResponse(new ResourceUtil(USER_RECOMMENDED_ARTISTS_FILE2).getContent());
    UserRecommendedArtistsParser parser = new UserRecommendedArtistsParserImpl(new StringUtil(wsResponse.getResponseBody()).getInputStream());
    List<RecommendedArtist> artists = parser.getArtists();
    for (int i = 0; i < EXPECTED_ARTISTS2.size(); i++) {
        assertEquals(EXPECTED_ARTISTS2.get(i), artists.get(i));
    }
}
Also used : ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) RecommendedArtist(com.github.hakko.musiccabinet.domain.model.aggr.UserRecommendedArtists.RecommendedArtist) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) Test(org.junit.Test)

Aggregations

WSResponse (com.github.hakko.musiccabinet.ws.lastfm.WSResponse)25 StringUtil (com.github.hakko.musiccabinet.util.StringUtil)15 ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)11 ArrayList (java.util.ArrayList)8 LastFmUser (com.github.hakko.musiccabinet.domain.model.library.LastFmUser)7 Scrobble (com.github.hakko.musiccabinet.domain.model.aggr.Scrobble)5 Artist (com.github.hakko.musiccabinet.domain.model.music.Artist)5 ResourceUtil (com.github.hakko.musiccabinet.util.ResourceUtil)4 Track (com.github.hakko.musiccabinet.domain.model.music.Track)3 Before (org.junit.Before)3 ArtistUserTag (com.github.hakko.musiccabinet.domain.model.aggr.ArtistUserTag)2 UserRecommendedArtists (com.github.hakko.musiccabinet.domain.model.aggr.UserRecommendedArtists)2 UserTopArtists (com.github.hakko.musiccabinet.domain.model.aggr.UserTopArtists)2 Period (com.github.hakko.musiccabinet.domain.model.library.Period)2 UserRecommendedArtistsParserImpl (com.github.hakko.musiccabinet.parser.lastfm.UserRecommendedArtistsParserImpl)2 Test (org.junit.Test)2 PlayCountDao (com.github.hakko.musiccabinet.dao.PlayCountDao)1 GroupWeeklyArtistChart (com.github.hakko.musiccabinet.domain.model.aggr.GroupWeeklyArtistChart)1 TagTopArtists (com.github.hakko.musiccabinet.domain.model.aggr.TagTopArtists)1 UserLovedTracks (com.github.hakko.musiccabinet.domain.model.aggr.UserLovedTracks)1