Abstract from iTunes search provider lookup

This commit is contained in:
ByteHamster 2020-04-09 11:57:16 +02:00
parent 12781c9101
commit b146a1163c
14 changed files with 172 additions and 109 deletions

View File

@ -57,6 +57,7 @@ import de.danoeh.antennapod.core.util.URLChecker;
import de.danoeh.antennapod.core.util.syndication.FeedDiscoverer;
import de.danoeh.antennapod.core.util.syndication.HtmlToPlainText;
import de.danoeh.antennapod.dialog.AuthenticationDialog;
import de.danoeh.antennapod.discovery.PodcastSearcherRegistry;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
@ -84,7 +85,6 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
public static final String ARG_FEEDURL = "arg.feedurl";
// Optional argument: specify a title for the actionbar.
public static final String ARG_TITLE = "title";
private static final int RESULT_ERROR = 2;
private static final String TAG = "OnlineFeedViewActivity";
private volatile List<Feed> feeds;
@ -127,11 +127,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
if (feedUrl == null) {
Log.e(TAG, "feedUrl is null.");
new AlertDialog.Builder(OnlineFeedViewActivity.this)
.setNeutralButton(android.R.string.ok, (dialog, which) -> finish())
.setTitle(R.string.error_label)
.setMessage(R.string.null_value_podcast_error)
.show();
showNoPodcastFoundError();
} else {
Log.d(TAG, "Activity was started with url " + feedUrl);
setLoadingLayout();
@ -140,13 +136,26 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
feedUrl = feedUrl.replaceFirst("((www.)?(subscribeonandroid.com/))", "");
}
if (savedInstanceState == null) {
startFeedDownload(feedUrl, null, null);
lookupUrlAndDownload(feedUrl, null, null);
} else {
startFeedDownload(feedUrl, savedInstanceState.getString("username"), savedInstanceState.getString("password"));
lookupUrlAndDownload(feedUrl, savedInstanceState.getString("username"),
savedInstanceState.getString("password"));
}
}
}
private void showNoPodcastFoundError() {
new AlertDialog.Builder(OnlineFeedViewActivity.this)
.setNeutralButton(android.R.string.ok, (dialog, which) -> finish())
.setTitle(R.string.error_label)
.setMessage(R.string.null_value_podcast_error)
.setOnDismissListener(dialog1 -> {
setResult(RESULT_ERROR);
finish();
})
.show();
}
/**
* Displays a progress indicator.
*/
@ -198,10 +207,9 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
}
}
private void resetIntent(String url, String title) {
private void resetIntent(String url) {
Intent intent = new Intent();
intent.putExtra(ARG_FEEDURL, url);
intent.putExtra(ARG_TITLE, title);
setIntent(intent);
}
@ -226,6 +234,17 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item);
}
private void lookupUrlAndDownload(String url, String username, String password) {
download = PodcastSearcherRegistry.lookupUrl(url)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(lookedUpUrl -> startFeedDownload(lookedUpUrl, username, password),
error -> {
showNoPodcastFoundError();
Log.e(TAG, Log.getStackTraceString(error));
});
}
private void startFeedDownload(String url, String username, String password) {
Log.d(TAG, "Starting feed download");
url = URLChecker.prepareURL(url);
@ -580,7 +599,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
if (urls.size() == 1) {
// Skip dialog and display the item directly
resetIntent(urls.get(0), titles.get(0));
resetIntent(urls.get(0));
startFeedDownload(urls.get(0), null, null);
return true;
}
@ -589,7 +608,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
DialogInterface.OnClickListener onClickListener = (dialog, which) -> {
String selectedUrl = urls.get(which);
dialog.dismiss();
resetIntent(selectedUrl, titles.get(which));
resetIntent(selectedUrl);
FeedPreferences prefs = feed.getPreferences();
if(prefs != null) {
startFeedDownload(selectedUrl, prefs.getUsername(), prefs.getPassword());

View File

@ -19,25 +19,22 @@ import java.util.concurrent.CountDownLatch;
public class CombinedSearcher implements PodcastSearcher {
private static final String TAG = "CombinedSearcher";
private final List<Pair<PodcastSearcher, Float>> searchProviders = new ArrayList<>();
public CombinedSearcher(Context context) {
addProvider(new FyydPodcastSearcher(), 1.f);
addProvider(new ItunesPodcastSearcher(context), 1.f);
//addProvider(new GpodnetPodcastSearcher(), 0.6f);
}
private void addProvider(PodcastSearcher provider, float priority) {
searchProviders.add(new Pair<>(provider, priority));
}
public Single<List<PodcastSearchResult>> search(String query) {
ArrayList<Disposable> disposables = new ArrayList<>();
List<List<PodcastSearchResult>> singleResults = new ArrayList<>(Collections.nCopies(searchProviders.size(), null));
CountDownLatch latch = new CountDownLatch(searchProviders.size());
for (int i = 0; i < searchProviders.size(); i++) {
Pair<PodcastSearcher, Float> searchProviderInfo = searchProviders.get(i);
PodcastSearcher searcher = searchProviderInfo.first;
List<List<PodcastSearchResult>> singleResults = new ArrayList<>(
Collections.nCopies(PodcastSearcherRegistry.getSearchProviders().size(), null));
CountDownLatch latch = new CountDownLatch(PodcastSearcherRegistry.getSearchProviders().size());
for (int i = 0; i < PodcastSearcherRegistry.getSearchProviders().size(); i++) {
PodcastSearcherRegistry.SearcherInfo searchProviderInfo
= PodcastSearcherRegistry.getSearchProviders().get(i);
PodcastSearcher searcher = searchProviderInfo.searcher;
if (searchProviderInfo.weight <= 0.00001f) {
latch.countDown();
continue;
}
final int index = i;
disposables.add(searcher.search(query).subscribe(e -> {
singleResults.set(index, e);
@ -56,7 +53,9 @@ public class CombinedSearcher implements PodcastSearcher {
})
.doOnDispose(() -> {
for (Disposable disposable : disposables) {
disposable.dispose();
if (disposable != null) {
disposable.dispose();
}
}
})
.subscribeOn(Schedulers.io())
@ -67,7 +66,7 @@ public class CombinedSearcher implements PodcastSearcher {
HashMap<String, Float> resultRanking = new HashMap<>();
HashMap<String, PodcastSearchResult> urlToResult = new HashMap<>();
for (int i = 0; i < singleResults.size(); i++) {
float providerPriority = searchProviders.get(i).second;
float providerPriority = PodcastSearcherRegistry.getSearchProviders().get(i).weight;
List<PodcastSearchResult> providerResults = singleResults.get(i);
if (providerResults == null) {
continue;
@ -93,4 +92,14 @@ public class CombinedSearcher implements PodcastSearcher {
}
return results;
}
@Override
public Single<String> lookupUrl(String url) {
return PodcastSearcherRegistry.lookupUrl(url);
}
@Override
public boolean urlNeedsLookup(String url) {
return PodcastSearcherRegistry.urlNeedsLookup(url);
}
}

View File

@ -35,4 +35,14 @@ public class FyydPodcastSearcher implements PodcastSearcher {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
@Override
public Single<String> lookupUrl(String url) {
return Single.just(url);
}
@Override
public boolean urlNeedsLookup(String url) {
return false;
}
}

View File

@ -33,4 +33,14 @@ public class GpodnetPodcastSearcher implements PodcastSearcher {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
@Override
public Single<String> lookupUrl(String url) {
return Single.just(url);
}
@Override
public boolean urlNeedsLookup(String url) {
return false;
}
}

View File

@ -2,7 +2,6 @@ package de.danoeh.antennapod.discovery;
import android.content.Context;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
import io.reactivex.Single;
import io.reactivex.SingleOnSubscribe;
@ -23,12 +22,11 @@ import java.util.List;
public class ItunesPodcastSearcher implements PodcastSearcher {
private static final String ITUNES_API_URL = "https://itunes.apple.com/search?media=podcast&term=%s";
private final Context context;
public ItunesPodcastSearcher(Context context) {
this.context = context;
public ItunesPodcastSearcher() {
}
@Override
public Single<List<PodcastSearchResult>> search(String query) {
return Single.create((SingleOnSubscribe<List<PodcastSearchResult>>) subscriber -> {
String encodedQuery;
@ -56,11 +54,12 @@ public class ItunesPodcastSearcher implements PodcastSearcher {
for (int i = 0; i < j.length(); i++) {
JSONObject podcastJson = j.getJSONObject(i);
PodcastSearchResult podcast = PodcastSearchResult.fromItunes(podcastJson);
podcasts.add(podcast);
if (podcast.feedUrl != null) {
podcasts.add(podcast);
}
}
} else {
String prefix = context.getString(R.string.error_msg_prefix);
subscriber.onError(new IOException(prefix + response));
subscriber.onError(new IOException(response.toString()));
}
} catch (IOException | JSONException e) {
subscriber.onError(e);
@ -70,4 +69,31 @@ public class ItunesPodcastSearcher implements PodcastSearcher {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
@Override
public Single<String> lookupUrl(String url) {
return Single.create(emitter -> {
OkHttpClient client = AntennapodHttpClient.getHttpClient();
Request.Builder httpReq = new Request.Builder().url(url);
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 feedUrl = results.getString("feedUrl");
emitter.onSuccess(feedUrl);
} else {
emitter.onError(new IOException(response.toString()));
}
} catch (IOException | JSONException e) {
emitter.onError(e);
}
});
}
@Override
public boolean urlNeedsLookup(String url) {
return url.contains("itunes.apple.com");
}
}

View File

@ -4,7 +4,6 @@ import android.content.Context;
import android.util.Log;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
import io.reactivex.Single;
import io.reactivex.SingleOnSubscribe;
@ -49,35 +48,6 @@ public class ItunesTopListLoader {
.observeOn(AndroidSchedulers.mainThread());
}
public Single<String> getFeedUrl(PodcastSearchResult podcast) {
if (!podcast.feedUrl.contains("itunes.apple.com")) {
return Single.just(podcast.feedUrl)
.observeOn(AndroidSchedulers.mainThread());
}
return Single.create((SingleOnSubscribe<String>) emitter -> {
OkHttpClient client = AntennapodHttpClient.getHttpClient();
Request.Builder httpReq = new Request.Builder()
.url(podcast.feedUrl);
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 feedUrl = results.getString("feedUrl");
emitter.onSuccess(feedUrl);
} else {
String prefix = context.getString(R.string.error_msg_prefix);
emitter.onError(new IOException(prefix + response));
}
} catch (IOException | JSONException e) {
emitter.onError(e);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
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";
Log.d(TAG, "Feed URL " + String.format(url, country));

View File

@ -7,4 +7,8 @@ import java.util.List;
public interface PodcastSearcher {
Single<List<PodcastSearchResult>> search(String query);
Single<String> lookupUrl(String resultUrl);
boolean urlNeedsLookup(String resultUrl);
}

View File

@ -0,0 +1,52 @@
package de.danoeh.antennapod.discovery;
import io.reactivex.Single;
import java.util.ArrayList;
import java.util.List;
public class PodcastSearcherRegistry {
private static List<SearcherInfo> searchProviders;
private PodcastSearcherRegistry() {
}
public static List<SearcherInfo> getSearchProviders() {
if (searchProviders == null) {
searchProviders = new ArrayList<>();
searchProviders.add(new SearcherInfo(new FyydPodcastSearcher(), 1.f));
searchProviders.add(new SearcherInfo(new ItunesPodcastSearcher(), 1.f));
searchProviders.add(new SearcherInfo(new GpodnetPodcastSearcher(), 0.0f));
}
return searchProviders;
}
public static Single<String> lookupUrl(String url) {
for (PodcastSearcherRegistry.SearcherInfo searchProviderInfo : getSearchProviders()) {
if (searchProviderInfo.searcher.urlNeedsLookup(url)) {
return searchProviderInfo.searcher.lookupUrl(url);
}
}
return Single.just(url);
}
public static boolean urlNeedsLookup(String url) {
for (PodcastSearcherRegistry.SearcherInfo searchProviderInfo : getSearchProviders()) {
if (searchProviderInfo.searcher.urlNeedsLookup(url)) {
return true;
}
}
return false;
}
public static class SearcherInfo {
final PodcastSearcher searcher;
final float weight;
public SearcherInfo(PodcastSearcher searcher, float weight) {
this.searcher = searcher;
this.weight = weight;
}
}
}

View File

@ -89,7 +89,6 @@ public class AddFeedFragment extends Fragment {
private void addUrl(String url) {
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, url);
intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, getString(R.string.add_feed_label));
startActivity(intent);
}

View File

@ -77,7 +77,6 @@ public class CombinedSearchFragment extends Fragment {
PodcastSearchResult podcast = searchResults.get(position);
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, podcast.feedUrl);
intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, podcast.title);
startActivity(intent);
});
progressBar = root.findViewById(R.id.progressBar);

View File

@ -76,7 +76,6 @@ public class FyydSearchFragment extends Fragment {
PodcastSearchResult podcast = searchResults.get(position);
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, podcast.feedUrl);
intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, podcast.title);
startActivity(intent);
});
progressBar = root.findViewById(R.id.progressBar);

View File

@ -105,27 +105,9 @@ public class ItunesSearchFragment extends Fragment {
if (podcast.feedUrl == null) {
return;
}
gridView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
ItunesTopListLoader loader = new ItunesTopListLoader(getContext());
disposable = loader.getFeedUrl(podcast)
.subscribe(feedUrl -> {
progressBar.setVisibility(View.GONE);
gridView.setVisibility(View.VISIBLE);
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, feedUrl);
intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, "iTunes");
startActivity(intent);
}, error -> {
Log.e(TAG, Log.getStackTraceString(error));
progressBar.setVisibility(View.GONE);
gridView.setVisibility(View.VISIBLE);
String prefix = getString(R.string.error_msg_prefix);
new AlertDialog.Builder(getActivity())
.setMessage(prefix + " " + error.getMessage())
.setPositiveButton(android.R.string.ok, null)
.show();
});
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, podcast.feedUrl);
startActivity(intent);
});
progressBar = root.findViewById(R.id.progressBar);
txtvError = root.findViewById(R.id.txtvError);
@ -219,7 +201,7 @@ public class ItunesSearchFragment extends Fragment {
txtvEmpty.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
ItunesPodcastSearcher searcher = new ItunesPodcastSearcher(getContext());
ItunesPodcastSearcher searcher = new ItunesPodcastSearcher();
disposable = searcher.search(query).subscribe(podcasts -> {
progressBar.setVisibility(View.GONE);
updateData(podcasts);

View File

@ -106,23 +106,8 @@ public class QuickFeedDiscoveryFragment extends Fragment implements AdapterView.
if (podcast.feedUrl == null) {
return;
}
view.setAlpha(0.5f);
ItunesTopListLoader loader = new ItunesTopListLoader(getContext());
disposable = loader.getFeedUrl(podcast)
.subscribe(feedUrl -> {
view.setAlpha(1f);
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, feedUrl);
intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, getString(R.string.add_feed_label));
startActivity(intent);
}, error -> {
Log.e(TAG, Log.getStackTraceString(error));
view.setAlpha(1f);
String prefix = getString(R.string.error_msg_prefix);
new AlertDialog.Builder(getActivity())
.setMessage(prefix + " " + error.getMessage())
.setPositiveButton(android.R.string.ok, null)
.show();
});
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, podcast.feedUrl);
startActivity(intent);
}
}

View File

@ -104,7 +104,6 @@ public abstract class PodcastListFragment extends Fragment {
Log.d(TAG, "Selected podcast: " + selection.toString());
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, selection.getUrl());
intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, getString(R.string.gpodnet_main_label));
startActivity(intent);
}