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);
}
});
}
Aggregations