From 73a6ff1f6003c776fb37bd8e7315045e69e710d2 Mon Sep 17 00:00:00 2001 From: GitStart <1501599+gitstart@users.noreply.github.com> Date: Sat, 28 Jan 2023 16:53:21 +0500 Subject: Remove subscribed podcasts from discover / suggestions (#6269) --- .../net/discovery/ItunesTopListLoader.java | 68 +++++++++++++--------- 1 file changed, 40 insertions(+), 28 deletions(-) (limited to 'net/discovery') 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> loadToplist(String country, int limit) { - return Single.create((SingleOnSubscribe>) emitter -> { - OkHttpClient client = AntennapodHttpClient.getHttpClient(); - String feedString; - String loadCountry = country; + public List loadToplist(String country, int limit, List 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 podcasts = parseFeed(feedString); - emitter.onSuccess(podcasts); - }) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()); + private static List removeSubscribed( + List suggestedPodcasts, List subscribedFeeds, int limit) { + Set subscribedPodcastsSet = new HashSet<>(); + for (Feed subscribedFeed : subscribedFeeds) { + String subscribedTitle = subscribedFeed.getTitle().trim() + " - " + subscribedFeed.getAuthor().trim(); + subscribedPodcastsSet.add(subscribedTitle); + } + List 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()) -- cgit v1.2.3