Search in sources :

Example 1 with StringUtil

use of com.github.hakko.musiccabinet.util.StringUtil in project musiccabinet by hakko.

the class WSResponseTest method illegalControlCharactersAreChomped1.

/*
	 * Use an authentic response from last.fm, and assert that WSResponse
	 * silently replaces illegal XML characters in it.
	 */
@Test
public void illegalControlCharactersAreChomped1() throws ApplicationException {
    String ctrlCharResponse = new ResourceUtil(CTRL_CHAR_RESPONSE_1).getContent();
    WSResponse response = new WSResponse(ctrlCharResponse);
    // supposed to work, as WSResponse chomps illegal control characters
    new ArtistInfoParserImpl(new StringUtil(response.getResponseBody()).getInputStream());
    try {
        // supposed to fail, as it hasn't passed WSResponse
        new ArtistInfoParserImpl(new ResourceUtil(CTRL_CHAR_RESPONSE_1).getInputStream());
        Assert.fail();
    } catch (ApplicationException e) {
    }
}
Also used : ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) ArtistInfoParserImpl(com.github.hakko.musiccabinet.parser.lastfm.ArtistInfoParserImpl) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) Test(org.junit.Test)

Example 2 with StringUtil

use of com.github.hakko.musiccabinet.util.StringUtil in project musiccabinet by hakko.

the class WSResponseTest method illegalControlCharactersAreChomped2.

/*
	 * Use an authentic response from last.fm, and assert that WSResponse
	 * silently replaces illegal XML characters in it.
	 */
@Test
public void illegalControlCharactersAreChomped2() throws ApplicationException {
    String ctrlCharResponse = new ResourceUtil(CTRL_CHAR_RESPONSE_2).getContent();
    WSResponse response = new WSResponse(ctrlCharResponse);
    // supposed to work, as WSResponse chomps illegal control characters
    new ArtistTopTracksParserImpl(new StringUtil(response.getResponseBody()).getInputStream());
    try {
        // supposed to fail, as it hasn't passed WSResponse
        new ArtistTopTracksParserImpl(new ResourceUtil(CTRL_CHAR_RESPONSE_2).getInputStream());
        Assert.fail();
    } catch (ApplicationException e) {
    }
}
Also used : ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) ArtistTopTracksParserImpl(com.github.hakko.musiccabinet.parser.lastfm.ArtistTopTracksParserImpl) Test(org.junit.Test)

Example 3 with StringUtil

use of com.github.hakko.musiccabinet.util.StringUtil in project musiccabinet by hakko.

the class JdbcUserRecommendedArtistsDaoTest method loadFunctionDependency.

@Before
public void loadFunctionDependency() throws ApplicationException {
    PostgreSQLUtil.loadFunction(dao, UPDATE_USER_RECOMMENDED_ARTISTS);
    joanRec = new UserRecommendedArtists(joan, new UserRecommendedArtistsParserImpl(new ResourceUtil(JOAN_FILE).getInputStream()).getArtists());
    rjRec = new UserRecommendedArtists(rj, new UserRecommendedArtistsParserImpl(new ResourceUtil(RJ_FILE).getInputStream()).getArtists());
    String body = new WSResponse(new ResourceUtil(FTPAREA_FILE).getContent()).getResponseBody();
    ftpareaRec = new UserRecommendedArtists(ftparea, new UserRecommendedArtistsParserImpl(new StringUtil(body).getInputStream()).getArtists());
    createArtistMetaData();
}
Also used : UserRecommendedArtists(com.github.hakko.musiccabinet.domain.model.aggr.UserRecommendedArtists) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) UserRecommendedArtistsParserImpl(com.github.hakko.musiccabinet.parser.lastfm.UserRecommendedArtistsParserImpl) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) Before(org.junit.Before)

Example 4 with StringUtil

use of com.github.hakko.musiccabinet.util.StringUtil in project musiccabinet by hakko.

the class UserLovedTracksService method updateUserLovedTracks.

private void updateUserLovedTracks() throws ApplicationException {
    List<LastFmUser> users = lastFmSettingsService.getLastFmUsers();
    setTotalOperations(users.size());
    List<UserLovedTracks> userLovedTracks = new ArrayList<>();
    for (LastFmUser user : users) {
        short page = 0, totalPages = 0;
        List<Track> lovedTracks = new ArrayList<>();
        do {
            WSResponse wsResponse = userLovedTracksClient.getUserLovedTracks(user, page);
            if (wsResponse.wasCallAllowed() && wsResponse.wasCallSuccessful()) {
                StringUtil stringUtil = new StringUtil(wsResponse.getResponseBody());
                UserLovedTracksParser parser = new UserLovedTracksParserImpl(stringUtil.getInputStream());
                totalPages = parser.getTotalPages();
                lovedTracks.addAll(parser.getLovedTracks());
            }
        } while (++page < totalPages);
        addFinishedOperation();
        userLovedTracks.add(new UserLovedTracks(user.getLastFmUsername(), lovedTracks));
    }
    userLovedTracksDao.createLovedTracks(userLovedTracks);
    for (UserStarredTrack ust : userLovedTracksDao.getStarredButNotLovedTracks()) {
        trackLoveClient.love(ust.getStarredTrack(), ust.getLastFmUser());
    }
}
Also used : UserStarredTrack(com.github.hakko.musiccabinet.domain.model.aggr.UserStarredTrack) ArrayList(java.util.ArrayList) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) UserLovedTracks(com.github.hakko.musiccabinet.domain.model.aggr.UserLovedTracks) UserLovedTracksParserImpl(com.github.hakko.musiccabinet.parser.lastfm.UserLovedTracksParserImpl) LastFmUser(com.github.hakko.musiccabinet.domain.model.library.LastFmUser) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) UserLovedTracksParser(com.github.hakko.musiccabinet.parser.lastfm.UserLovedTracksParser) UserStarredTrack(com.github.hakko.musiccabinet.domain.model.aggr.UserStarredTrack) Track(com.github.hakko.musiccabinet.domain.model.music.Track)

Example 5 with StringUtil

use of com.github.hakko.musiccabinet.util.StringUtil in project musiccabinet by hakko.

the class UserTopArtistsService method updateSearchIndex.

@Override
protected void updateSearchIndex() throws ApplicationException {
    List<LastFmUser> users = lastFmSettingsService.getLastFmUsers();
    setTotalOperations(users.size() * Period.values().length);
    List<UserTopArtists> userTopArtists = new ArrayList<>();
    for (LastFmUser user : users) {
        for (Period period : Period.values()) {
            try {
                WSResponse wsResponse = userTopArtistsClient.getUserTopArtists(user, period);
                if (wsResponse.wasCallAllowed() && wsResponse.wasCallSuccessful()) {
                    StringUtil stringUtil = new StringUtil(wsResponse.getResponseBody());
                    UserTopArtistsParser parser = new UserTopArtistsParserImpl(stringUtil.getInputStream());
                    userTopArtists.add(new UserTopArtists(user, period, parser.getArtists()));
                }
            } catch (ApplicationException e) {
                LOG.warn("Fetching top artist for " + user.getLastFmUsername() + ", " + period.getDescription() + " failed.", e);
            }
            addFinishedOperation();
        }
    }
    userTopArtistsDao.createUserTopArtists(userTopArtists);
}
Also used : UserTopArtistsParserImpl(com.github.hakko.musiccabinet.parser.lastfm.UserTopArtistsParserImpl) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) UserTopArtistsParser(com.github.hakko.musiccabinet.parser.lastfm.UserTopArtistsParser) ArrayList(java.util.ArrayList) LastFmUser(com.github.hakko.musiccabinet.domain.model.library.LastFmUser) Period(com.github.hakko.musiccabinet.domain.model.library.Period) UserTopArtists(com.github.hakko.musiccabinet.domain.model.aggr.UserTopArtists) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) StringUtil(com.github.hakko.musiccabinet.util.StringUtil)

Aggregations

StringUtil (com.github.hakko.musiccabinet.util.StringUtil)19 WSResponse (com.github.hakko.musiccabinet.ws.lastfm.WSResponse)15 ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)14 ArrayList (java.util.ArrayList)10 Artist (com.github.hakko.musiccabinet.domain.model.music.Artist)5 ResourceUtil (com.github.hakko.musiccabinet.util.ResourceUtil)4 LastFmUser (com.github.hakko.musiccabinet.domain.model.library.LastFmUser)3 Test (org.junit.Test)3 UserRecommendedArtists (com.github.hakko.musiccabinet.domain.model.aggr.UserRecommendedArtists)2 MBArtist (com.github.hakko.musiccabinet.domain.model.music.MBArtist)2 ArtistInfoParserImpl (com.github.hakko.musiccabinet.parser.lastfm.ArtistInfoParserImpl)2 ArtistTopTracksParserImpl (com.github.hakko.musiccabinet.parser.lastfm.ArtistTopTracksParserImpl)2 UserRecommendedArtistsParserImpl (com.github.hakko.musiccabinet.parser.lastfm.UserRecommendedArtistsParserImpl)2 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 RecommendedArtist (com.github.hakko.musiccabinet.domain.model.aggr.UserRecommendedArtists.RecommendedArtist)1 UserStarredTrack (com.github.hakko.musiccabinet.domain.model.aggr.UserStarredTrack)1 UserTopArtists (com.github.hakko.musiccabinet.domain.model.aggr.UserTopArtists)1 LastFmGroup (com.github.hakko.musiccabinet.domain.model.library.LastFmGroup)1