2017-09-03 08:04:18 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Mauricio Colli <mauriciocolli@outlook.com>
|
|
|
|
* Extractors.java is part of NewPipe
|
|
|
|
*
|
|
|
|
* License: GPL-3.0+
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.MainActivity;
|
|
|
|
import org.schabi.newpipe.extractor.Info;
|
|
|
|
import org.schabi.newpipe.extractor.ListExtractor.NextItemsResult;
|
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.channel.ChannelInfo;
|
2017-09-23 17:39:04 +02:00
|
|
|
import org.schabi.newpipe.extractor.kiosk.KioskInfo;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
|
|
|
|
import org.schabi.newpipe.extractor.search.SearchEngine;
|
|
|
|
import org.schabi.newpipe.extractor.search.SearchResult;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
|
|
|
|
|
|
|
import java.io.InterruptedIOException;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
|
|
|
import io.reactivex.Maybe;
|
|
|
|
import io.reactivex.MaybeSource;
|
|
|
|
import io.reactivex.Single;
|
|
|
|
import io.reactivex.annotations.NonNull;
|
|
|
|
import io.reactivex.functions.Consumer;
|
|
|
|
import io.reactivex.functions.Function;
|
|
|
|
|
|
|
|
public final class ExtractorHelper {
|
|
|
|
private static final String TAG = ExtractorHelper.class.getSimpleName();
|
|
|
|
private static final InfoCache cache = InfoCache.getInstance();
|
|
|
|
|
|
|
|
private ExtractorHelper() {
|
|
|
|
//no instance
|
|
|
|
}
|
|
|
|
|
2017-10-08 21:04:37 +02:00
|
|
|
private static void checkServiceId(int serviceId) {
|
|
|
|
if(serviceId == Constants.NO_SERVICE_ID) {
|
|
|
|
throw new IllegalArgumentException("serviceId is NO_SERVICE_ID");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 13:57:31 +01:00
|
|
|
public static Single<SearchResult> searchFor(final int serviceId,
|
|
|
|
final String query,
|
|
|
|
final int pageNumber,
|
|
|
|
final String contentCountry,
|
|
|
|
final SearchEngine.Filter filter) {
|
2017-10-08 21:04:37 +02:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 13:57:31 +01:00
|
|
|
return Single.fromCallable(() ->
|
|
|
|
SearchResult.getSearchResult(NewPipe.getService(serviceId).getSearchEngine(),
|
|
|
|
query, pageNumber, contentCountry, filter)
|
|
|
|
);
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:57:31 +01:00
|
|
|
public static Single<NextItemsResult> getMoreSearchItems(final int serviceId,
|
|
|
|
final String query,
|
|
|
|
final int nextPageNumber,
|
|
|
|
final String searchLanguage,
|
|
|
|
final SearchEngine.Filter filter) {
|
2017-10-08 21:04:37 +02:00
|
|
|
checkServiceId(serviceId);
|
2017-09-03 08:04:18 +02:00
|
|
|
return searchFor(serviceId, query, nextPageNumber, searchLanguage, filter)
|
2018-01-20 13:57:31 +01:00
|
|
|
.map((@NonNull SearchResult searchResult) ->
|
|
|
|
new NextItemsResult(searchResult.resultList,
|
|
|
|
nextPageNumber + "",
|
|
|
|
searchResult.errors));
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:57:31 +01:00
|
|
|
public static Single<List<String>> suggestionsFor(final int serviceId,
|
|
|
|
final String query,
|
|
|
|
final String contentCountry) {
|
2017-10-08 21:04:37 +02:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 13:57:31 +01:00
|
|
|
return Single.fromCallable(() ->
|
|
|
|
NewPipe.getService(serviceId)
|
|
|
|
.getSuggestionExtractor()
|
|
|
|
.suggestionList(query, contentCountry));
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:57:31 +01:00
|
|
|
public static Single<StreamInfo> getStreamInfo(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
boolean forceLoad) {
|
2017-10-08 21:04:37 +02:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 13:57:31 +01:00
|
|
|
return checkCache(forceLoad, serviceId, url, Single.fromCallable(() ->
|
|
|
|
StreamInfo.getInfo(NewPipe.getService(serviceId), url)));
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:57:31 +01:00
|
|
|
public static Single<ChannelInfo> getChannelInfo(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
boolean forceLoad) {
|
2017-10-08 21:04:37 +02:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 13:57:31 +01:00
|
|
|
return checkCache(forceLoad, serviceId, url, Single.fromCallable(() ->
|
|
|
|
ChannelInfo.getInfo(NewPipe.getService(serviceId), url)));
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:57:31 +01:00
|
|
|
public static Single<NextItemsResult> getMoreChannelItems(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
final String nextStreamsUrl) {
|
2017-10-08 21:04:37 +02:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 13:57:31 +01:00
|
|
|
return Single.fromCallable(() ->
|
|
|
|
ChannelInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl));
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:57:31 +01:00
|
|
|
public static Single<PlaylistInfo> getPlaylistInfo(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
boolean forceLoad) {
|
2017-10-08 21:04:37 +02:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 13:57:31 +01:00
|
|
|
return checkCache(forceLoad, serviceId, url, Single.fromCallable(() ->
|
|
|
|
PlaylistInfo.getInfo(NewPipe.getService(serviceId), url)));
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:57:31 +01:00
|
|
|
public static Single<NextItemsResult> getMorePlaylistItems(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
final String nextStreamsUrl) {
|
2017-10-08 21:04:37 +02:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 13:57:31 +01:00
|
|
|
return Single.fromCallable(() ->
|
|
|
|
PlaylistInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl));
|
2017-09-23 17:39:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:57:31 +01:00
|
|
|
public static Single<KioskInfo> getKioskInfo(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
final String contentCountry,
|
|
|
|
boolean forceLoad) {
|
|
|
|
return checkCache(forceLoad, serviceId, url, Single.fromCallable(() ->
|
|
|
|
KioskInfo.getInfo(NewPipe.getService(serviceId), url, contentCountry)));
|
2017-09-23 17:39:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:57:31 +01:00
|
|
|
public static Single<NextItemsResult> getMoreKioskItems(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
final String nextStreamsUrl,
|
|
|
|
final String contentCountry) {
|
|
|
|
return Single.fromCallable(() ->
|
|
|
|
KioskInfo.getMoreItems(NewPipe.getService(serviceId),
|
|
|
|
url, nextStreamsUrl, contentCountry));
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
/**
|
2018-01-20 13:57:31 +01:00
|
|
|
* Check if we can load it from the cache (forceLoad parameter), if we can't,
|
|
|
|
* load from the network (Single loadFromNetwork)
|
2017-09-03 08:04:18 +02:00
|
|
|
* and put the results in the cache.
|
|
|
|
*/
|
2018-01-20 13:57:31 +01:00
|
|
|
private static <I extends Info> Single<I> checkCache(boolean forceLoad,
|
|
|
|
int serviceId,
|
|
|
|
String url,
|
|
|
|
Single<I> loadFromNetwork) {
|
2017-10-08 21:04:37 +02:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 13:57:31 +01:00
|
|
|
loadFromNetwork = loadFromNetwork.doOnSuccess((@NonNull I i) -> cache.putInfo(i));
|
2017-09-03 08:04:18 +02:00
|
|
|
|
|
|
|
Single<I> load;
|
|
|
|
if (forceLoad) {
|
|
|
|
cache.removeInfo(serviceId, url);
|
|
|
|
load = loadFromNetwork;
|
|
|
|
} else {
|
2018-01-20 13:57:31 +01:00
|
|
|
load = Maybe.concat(ExtractorHelper.<I>loadFromCache(serviceId, url),
|
|
|
|
loadFromNetwork.toMaybe())
|
2017-09-03 08:04:18 +02:00
|
|
|
.firstElement() //Take the first valid
|
|
|
|
.toSingle();
|
|
|
|
}
|
|
|
|
|
|
|
|
return load;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default implementation uses the {@link InfoCache} to get cached results
|
|
|
|
*/
|
|
|
|
public static <I extends Info> Maybe<I> loadFromCache(final int serviceId, final String url) {
|
2017-10-08 21:04:37 +02:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 13:57:31 +01:00
|
|
|
return Maybe.defer(() -> {
|
2017-09-03 08:04:18 +02:00
|
|
|
//noinspection unchecked
|
|
|
|
I info = (I) cache.getFromKey(serviceId, url);
|
|
|
|
if (MainActivity.DEBUG) Log.d(TAG, "loadFromCache() called, info > " + info);
|
|
|
|
|
|
|
|
// Only return info if it's not null (it is cached)
|
|
|
|
if (info != null) {
|
|
|
|
return Maybe.just(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Maybe.empty();
|
2018-01-20 13:57:31 +01:00
|
|
|
});
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if throwable have the cause that can be assignable from the causes to check.
|
|
|
|
*
|
|
|
|
* @see Class#isAssignableFrom(Class)
|
|
|
|
*/
|
2018-01-20 13:57:31 +01:00
|
|
|
public static boolean hasAssignableCauseThrowable(Throwable throwable,
|
|
|
|
Class<?>... causesToCheck) {
|
2017-09-03 08:04:18 +02:00
|
|
|
// Check if getCause is not the same as cause (the getCause is already the root),
|
|
|
|
// as it will cause a infinite loop if it is
|
|
|
|
Throwable cause, getCause = throwable;
|
|
|
|
|
2018-01-04 07:53:31 +01:00
|
|
|
// Check if throwable is a subclass of any of the filtered classes
|
|
|
|
final Class throwableClass = throwable.getClass();
|
2017-09-03 08:04:18 +02:00
|
|
|
for (Class<?> causesEl : causesToCheck) {
|
2018-01-04 07:53:31 +01:00
|
|
|
if (causesEl.isAssignableFrom(throwableClass)) {
|
2017-09-03 08:04:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 07:53:31 +01:00
|
|
|
// Iteratively checks if the root cause of the throwable is a subclass of the filtered class
|
2017-09-03 08:04:18 +02:00
|
|
|
while ((cause = throwable.getCause()) != null && getCause != cause) {
|
|
|
|
getCause = cause;
|
2018-01-04 07:53:31 +01:00
|
|
|
final Class causeClass = cause.getClass();
|
2017-09-03 08:04:18 +02:00
|
|
|
for (Class<?> causesEl : causesToCheck) {
|
2018-01-04 07:53:31 +01:00
|
|
|
if (causesEl.isAssignableFrom(causeClass)) {
|
2017-09-03 08:04:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if throwable have the exact cause from one of the causes to check.
|
|
|
|
*/
|
|
|
|
public static boolean hasExactCauseThrowable(Throwable throwable, Class<?>... causesToCheck) {
|
|
|
|
// Check if getCause is not the same as cause (the getCause is already the root),
|
|
|
|
// as it will cause a infinite loop if it is
|
|
|
|
Throwable cause, getCause = throwable;
|
|
|
|
|
|
|
|
for (Class<?> causesEl : causesToCheck) {
|
|
|
|
if (throwable.getClass().equals(causesEl)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((cause = throwable.getCause()) != null && getCause != cause) {
|
|
|
|
getCause = cause;
|
|
|
|
for (Class<?> causesEl : causesToCheck) {
|
|
|
|
if (cause.getClass().equals(causesEl)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if throwable have Interrupted* exception as one of its causes.
|
|
|
|
*/
|
|
|
|
public static boolean isInterruptedCaused(Throwable throwable) {
|
2018-01-20 13:57:31 +01:00
|
|
|
return ExtractorHelper.hasExactCauseThrowable(throwable,
|
|
|
|
InterruptedIOException.class,
|
|
|
|
InterruptedException.class);
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
2017-12-29 15:02:23 +01:00
|
|
|
|
|
|
|
public static String toUpperCase(String value) {
|
|
|
|
StringBuilder sb = new StringBuilder(value);
|
|
|
|
for (int index = 0; index < sb.length(); index++) {
|
|
|
|
char c = sb.charAt(index);
|
|
|
|
if (Character.isLowerCase(c)) {
|
|
|
|
sb.setCharAt(index, Character.toUpperCase(c));
|
|
|
|
} else {
|
|
|
|
sb.setCharAt(index, Character.toLowerCase(c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|