Search in sources :

Example 26 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException in project musiccabinet by hakko.

the class TrackSimilarityClientTest method validateParameters.

@Test
public void validateParameters() throws ApplicationException {
    final String method = TrackSimilarityClient.METHOD;
    final String artistName = "madonna";
    final String trackName = "ray of light";
    new TrackSimilarityClient() {

        @Override
        protected WSResponse executeWSRequest(WebserviceInvocation wi, List<NameValuePair> params) throws ApplicationException {
            assertEquals(Calltype.TRACK_GET_SIMILAR, wi.getCallType());
            assertTrue(trackName.equals(wi.getTrack().getName()));
            assertTrue(artistName.equals(wi.getTrack().getArtist().getName()));
            assertHasParameter(params, PARAM_METHOD, method);
            assertHasParameter(params, PARAM_ARTIST, artistName);
            assertHasParameter(params, PARAM_TRACK, trackName);
            return null;
        }

        @Override
        protected WebserviceHistoryService getHistoryService() {
            return Mockito.mock(WebserviceHistoryService.class);
        }
    }.getTrackSimilarity(new Track(new Artist(artistName), trackName));
}
Also used : Artist(com.github.hakko.musiccabinet.domain.model.music.Artist) NameValuePair(org.apache.http.NameValuePair) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) WebserviceHistoryService(com.github.hakko.musiccabinet.service.lastfm.WebserviceHistoryService) WebserviceInvocation(com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation) Track(com.github.hakko.musiccabinet.domain.model.music.Track) Test(org.junit.Test)

Example 27 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException 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 28 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException 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 29 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException in project musiccabinet by hakko.

the class AbstractSAXParserImpl method parseFromStream.

protected void parseFromStream(InputStream source, DefaultHandler handler) throws ApplicationException {
    try (InputStream autoClosingStream = source) {
        SAXParser saxParser = getSAXParser();
        saxParser.parse(source, handler);
    } catch (IOException e) {
        throw new ApplicationException("Could not read data from stream!", e);
    } catch (SAXException e) {
        throw new ApplicationException("Could not parse data from stream!", e);
    } catch (ParserConfigurationException e) {
        throw new ApplicationException("Could not set up SAX parser!", e);
    }
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 30 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException 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

ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)43 Test (org.junit.Test)20 NameValuePair (org.apache.http.NameValuePair)18 StringUtil (com.github.hakko.musiccabinet.util.StringUtil)14 WebserviceInvocation (com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation)11 WSResponse (com.github.hakko.musiccabinet.ws.lastfm.WSResponse)11 Artist (com.github.hakko.musiccabinet.domain.model.music.Artist)10 WebserviceHistoryService (com.github.hakko.musiccabinet.service.lastfm.WebserviceHistoryService)10 ArrayList (java.util.ArrayList)8 LastFmUser (com.github.hakko.musiccabinet.domain.model.library.LastFmUser)7 IOException (java.io.IOException)6 Track (com.github.hakko.musiccabinet.domain.model.music.Track)4 ArtistUserTag (com.github.hakko.musiccabinet.domain.model.aggr.ArtistUserTag)2 Scrobble (com.github.hakko.musiccabinet.domain.model.aggr.Scrobble)2 LastFmGroup (com.github.hakko.musiccabinet.domain.model.library.LastFmGroup)2 Period (com.github.hakko.musiccabinet.domain.model.library.Period)2 Album (com.github.hakko.musiccabinet.domain.model.music.Album)2 MBArtist (com.github.hakko.musiccabinet.domain.model.music.MBArtist)2 Tag (com.github.hakko.musiccabinet.domain.model.music.Tag)2 ArtistInfoParserImpl (com.github.hakko.musiccabinet.parser.lastfm.ArtistInfoParserImpl)2