Search in sources :

Example 11 with StringUtil

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

the class MusicBrainzService method updateArtistIds.

protected void updateArtistIds() {
    List<Artist> missingArtists = artistDao.getMissingArtists();
    List<MBArtist> mbArtists = new ArrayList<>();
    mbids = missingArtists.size();
    for (Artist artist : artistDao.getMissingArtists()) {
        try {
            StringUtil response = new StringUtil(artistQueryClient.get(artist.getName()));
            ArtistQueryParser parser = new ArtistQueryParserImpl(response.getInputStream());
            if (parser.getArtist() != null) {
                mbArtists.add(parser.getArtist());
                if (mbArtists.size() > 100) {
                    artistDao.createArtists(mbArtists);
                    mbArtists.clear();
                }
            }
            ++mbid;
        } catch (ApplicationException e) {
            LOG.warn("Couldn't read mbid for " + artist.getName(), e);
        }
    }
    artistDao.createArtists(mbArtists);
}
Also used : Artist(com.github.hakko.musiccabinet.domain.model.music.Artist) MBArtist(com.github.hakko.musiccabinet.domain.model.music.MBArtist) MBArtist(com.github.hakko.musiccabinet.domain.model.music.MBArtist) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) ArrayList(java.util.ArrayList) ArtistQueryParser(com.github.hakko.musiccabinet.parser.musicbrainz.ArtistQueryParser) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) ArtistQueryParserImpl(com.github.hakko.musiccabinet.parser.musicbrainz.ArtistQueryParserImpl)

Example 12 with StringUtil

use of com.github.hakko.musiccabinet.util.StringUtil 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)

Example 13 with StringUtil

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

the class LastFmService method identifyLastFmUser.

public LastFmUser identifyLastFmUser(String token) throws ApplicationException {
    LOG.debug("identifyLastFmUser(" + token + ")");
    WSResponse wsResponse = authSessionClient.getAuthSession(token);
    if (wsResponse.wasCallAllowed() && wsResponse.wasCallSuccessful()) {
        StringUtil stringUtil = new StringUtil(wsResponse.getResponseBody());
        AuthSessionParser authSessionParser = new AuthSessionParserImpl(stringUtil.getInputStream());
        return authSessionParser.getLastFmUser();
    } else {
        LOG.debug("wsResponse: " + wsResponse.getResponseBody());
        throw new ApplicationException("Could not get session key for user! (code " + wsResponse.getErrorCode() + ", " + wsResponse.getErrorMessage() + ")");
    }
}
Also used : AuthSessionParser(com.github.hakko.musiccabinet.parser.lastfm.AuthSessionParser) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) AuthSessionParserImpl(com.github.hakko.musiccabinet.parser.lastfm.AuthSessionParserImpl) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) StringUtil(com.github.hakko.musiccabinet.util.StringUtil)

Example 14 with StringUtil

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

the class MusicBrainzService method updateArtistDiscographies.

protected void updateArtistDiscographies() {
    List<MBArtist> outdatedArtists = artistDao.getOutdatedArtists();
    List<MBRelease> mbReleases = new ArrayList<>();
    ReleaseParser parser;
    discographies = outdatedArtists.size();
    for (MBArtist artist : outdatedArtists) {
        try {
            int offset = 0;
            do {
                StringUtil response = new StringUtil(releaseClient.get(artist.getName(), artist.getMbid(), offset));
                parser = new ReleaseParserImpl(response.getInputStream());
                for (MBRelease album : parser.getReleases()) {
                    album.setArtistId(artist.getId());
                }
                mbReleases.addAll(parser.getReleases());
                offset += 100;
            } while (offset < parser.getTotalReleases());
            ++discography;
            if (mbReleases.size() > 1000) {
                albumDao.createAlbums(mbReleases);
                mbReleases.clear();
            }
        } catch (ApplicationException e) {
            LOG.warn("Couldn't read discography for " + artist.getName(), e);
        }
    }
    albumDao.createAlbums(mbReleases);
}
Also used : MBArtist(com.github.hakko.musiccabinet.domain.model.music.MBArtist) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) ArrayList(java.util.ArrayList) ReleaseParser(com.github.hakko.musiccabinet.parser.musicbrainz.ReleaseParser) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) ReleaseParserImpl(com.github.hakko.musiccabinet.parser.musicbrainz.ReleaseParserImpl) MBRelease(com.github.hakko.musiccabinet.domain.model.music.MBRelease)

Example 15 with StringUtil

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

the class ArtistInfoService method updateSearchIndex.

@Override
protected void updateSearchIndex() throws ApplicationException {
    Set<String> artistNames = webserviceHistoryService.getArtistNamesScheduledForUpdate(ARTIST_GET_INFO);
    List<ArtistInfo> artistInfos = new ArrayList<>(artistNames.size());
    setTotalOperations(artistNames.size());
    for (String artistName : artistNames) {
        try {
            WSResponse wsResponse = artistInfoClient.getArtistInfo(new Artist(artistName), lastFmSettingsService.getLang());
            if (wsResponse.wasCallAllowed() && wsResponse.wasCallSuccessful()) {
                StringUtil stringUtil = new StringUtil(wsResponse.getResponseBody());
                ArtistInfoParser aiParser = new ArtistInfoParserImpl(stringUtil.getInputStream());
                if (aiParser.getArtistInfo() != null) {
                    artistInfos.add(aiParser.getArtistInfo());
                } else {
                    LOG.warn("Artist info response for " + artistName + " not parsed correctly. Response was " + wsResponse.getResponseBody());
                }
                if (artistInfos.size() == BATCH_SIZE) {
                    artistInfoDao.createArtistInfo(artistInfos);
                    artistInfos.clear();
                }
            }
        } catch (ApplicationException e) {
            LOG.warn("Fetching artist info for " + artistName + " failed.", e);
        }
        addFinishedOperation();
    }
    artistInfoDao.createArtistInfo(artistInfos);
}
Also used : Artist(com.github.hakko.musiccabinet.domain.model.music.Artist) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) ArtistInfoParserImpl(com.github.hakko.musiccabinet.parser.lastfm.ArtistInfoParserImpl) ArrayList(java.util.ArrayList) ArtistInfoParser(com.github.hakko.musiccabinet.parser.lastfm.ArtistInfoParser) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) ArtistInfo(com.github.hakko.musiccabinet.domain.model.music.ArtistInfo)

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