Search in sources :

Example 1 with FeedUrlNotFoundException

use of de.danoeh.antennapod.core.feed.FeedUrlNotFoundException in project AntennaPod by AntennaPod.

the class ItunesPodcastSearcher method lookupUrl.

@Override
public Single<String> lookupUrl(String url) {
    Pattern pattern = Pattern.compile(PATTERN_BY_ID);
    Matcher matcher = pattern.matcher(url);
    final String lookupUrl = matcher.find() ? ("https://itunes.apple.com/lookup?id=" + matcher.group(1)) : url;
    return Single.create(emitter -> {
        OkHttpClient client = AntennapodHttpClient.getHttpClient();
        Request.Builder httpReq = new Request.Builder().url(lookupUrl);
        try {
            Response response = client.newCall(httpReq.build()).execute();
            if (response.isSuccessful()) {
                String resultString = response.body().string();
                JSONObject result = new JSONObject(resultString);
                JSONObject results = result.getJSONArray("results").getJSONObject(0);
                String feedUrlName = "feedUrl";
                if (!results.has(feedUrlName)) {
                    String artistName = results.getString("artistName");
                    String trackName = results.getString("trackName");
                    emitter.onError(new FeedUrlNotFoundException(artistName, trackName));
                    return;
                }
                String feedUrl = results.getString(feedUrlName);
                emitter.onSuccess(feedUrl);
            } else {
                emitter.onError(new IOException(response.toString()));
            }
        } catch (IOException | JSONException e) {
            emitter.onError(e);
        }
    });
}
Also used : Response(okhttp3.Response) Pattern(java.util.regex.Pattern) OkHttpClient(okhttp3.OkHttpClient) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) FeedUrlNotFoundException(de.danoeh.antennapod.core.feed.FeedUrlNotFoundException)

Aggregations

FeedUrlNotFoundException (de.danoeh.antennapod.core.feed.FeedUrlNotFoundException)1 IOException (java.io.IOException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1