diff options
Diffstat (limited to 'net/discovery')
-rw-r--r-- | net/discovery/src/main/java/de/danoeh/antennapod/net/discovery/ItunesTopListLoader.java | 68 |
1 files changed, 40 insertions, 28 deletions
diff --git a/net/discovery/src/main/java/de/danoeh/antennapod/net/discovery/ItunesTopListLoader.java b/net/discovery/src/main/java/de/danoeh/antennapod/net/discovery/ItunesTopListLoader.java index 53ea00235..0bfc5a863 100644 --- a/net/discovery/src/main/java/de/danoeh/antennapod/net/discovery/ItunesTopListLoader.java +++ b/net/discovery/src/main/java/de/danoeh/antennapod/net/discovery/ItunesTopListLoader.java @@ -2,12 +2,8 @@ package de.danoeh.antennapod.net.discovery; import android.content.Context; import android.util.Log; - import de.danoeh.antennapod.core.service.download.AntennapodHttpClient; -import io.reactivex.Single; -import io.reactivex.SingleOnSubscribe; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.schedulers.Schedulers; +import de.danoeh.antennapod.model.feed.Feed; import okhttp3.CacheControl; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -18,8 +14,10 @@ import org.json.JSONObject; import java.io.IOException; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.concurrent.TimeUnit; public class ItunesTopListLoader { @@ -30,39 +28,53 @@ public class ItunesTopListLoader { public static final String PREF_KEY_NEEDS_CONFIRM = "needs_confirm"; public static final String PREFS = "CountryRegionPrefs"; public static final String COUNTRY_CODE_UNSET = "99"; + private static final int NUM_LOADED = 25; public ItunesTopListLoader(Context context) { this.context = context; } - public Single<List<PodcastSearchResult>> loadToplist(String country, int limit) { - return Single.create((SingleOnSubscribe<List<PodcastSearchResult>>) emitter -> { - OkHttpClient client = AntennapodHttpClient.getHttpClient(); - String feedString; - String loadCountry = country; + public List<PodcastSearchResult> loadToplist(String country, int limit, List<Feed> subscribed) + throws JSONException, IOException { + OkHttpClient client = AntennapodHttpClient.getHttpClient(); + String feedString; + String loadCountry = country; + if (COUNTRY_CODE_UNSET.equals(country)) { + loadCountry = Locale.getDefault().getCountry(); + } + try { + feedString = getTopListFeed(client, loadCountry); + } catch (IOException e) { if (COUNTRY_CODE_UNSET.equals(country)) { - loadCountry = Locale.getDefault().getCountry(); - } - try { - feedString = getTopListFeed(client, loadCountry, limit); - } catch (IOException e) { - if (COUNTRY_CODE_UNSET.equals(country)) { - feedString = getTopListFeed(client, "US", limit); - } else { - emitter.onError(e); - return; - } + feedString = getTopListFeed(client, "US"); + } else { + throw e; } + } + return removeSubscribed(parseFeed(feedString), subscribed, limit); + } - List<PodcastSearchResult> podcasts = parseFeed(feedString); - emitter.onSuccess(podcasts); - }) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()); + private static List<PodcastSearchResult> removeSubscribed( + List<PodcastSearchResult> suggestedPodcasts, List<Feed> subscribedFeeds, int limit) { + Set<String> subscribedPodcastsSet = new HashSet<>(); + for (Feed subscribedFeed : subscribedFeeds) { + String subscribedTitle = subscribedFeed.getTitle().trim() + " - " + subscribedFeed.getAuthor().trim(); + subscribedPodcastsSet.add(subscribedTitle); + } + List<PodcastSearchResult> suggestedNotSubscribed = new ArrayList<>(); + for (PodcastSearchResult suggested : suggestedPodcasts) { + if (!subscribedPodcastsSet.contains(suggested.title.trim())) { + suggestedNotSubscribed.add(suggested); + } + if (suggestedNotSubscribed.size() == limit) { + return suggestedNotSubscribed; + } + } + return suggestedNotSubscribed; } - private String getTopListFeed(OkHttpClient client, String country, int limit) throws IOException { - String url = "https://itunes.apple.com/%s/rss/toppodcasts/limit=" + limit + "/explicit=true/json"; + private String getTopListFeed(OkHttpClient client, String country) throws IOException { + String url = "https://itunes.apple.com/%s/rss/toppodcasts/limit=" + NUM_LOADED + "/explicit=true/json"; Log.d(TAG, "Feed URL " + String.format(url, country)); Request.Builder httpReq = new Request.Builder() .cacheControl(new CacheControl.Builder().maxStale(1, TimeUnit.DAYS).build()) |