2017-09-03 06:04:18 +00: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;
|
|
|
|
|
2018-01-23 00:40:00 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Handler;
|
2017-09-03 06:04:18 +00:00
|
|
|
import android.util.Log;
|
2018-01-23 00:40:00 +00:00
|
|
|
import android.widget.Toast;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
import org.schabi.newpipe.MainActivity;
|
2018-01-23 00:40:00 +00:00
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
import org.schabi.newpipe.ReCaptchaActivity;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.extractor.Info;
|
2018-09-23 01:32:19 +00:00
|
|
|
import org.schabi.newpipe.extractor.InfoItem;
|
2018-03-18 15:37:49 +00:00
|
|
|
import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
2018-12-23 21:07:27 +00:00
|
|
|
import org.schabi.newpipe.extractor.SuggestionExtractor;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.extractor.channel.ChannelInfo;
|
2018-09-23 01:32:19 +00:00
|
|
|
import org.schabi.newpipe.extractor.comments.CommentsInfo;
|
2018-01-23 00:40:00 +00:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
2017-09-23 15:39:04 +00:00
|
|
|
import org.schabi.newpipe.extractor.kiosk.KioskInfo;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
|
2018-07-08 12:27:12 +00:00
|
|
|
import org.schabi.newpipe.extractor.search.SearchInfo;
|
2018-07-08 15:46:21 +00:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
2018-01-23 00:40:00 +00:00
|
|
|
import org.schabi.newpipe.report.ErrorActivity;
|
|
|
|
import org.schabi.newpipe.report.UserAction;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2018-01-23 00:40:00 +00:00
|
|
|
import java.io.IOException;
|
2017-09-03 06:04:18 +00:00
|
|
|
import java.io.InterruptedIOException;
|
2018-12-23 21:07:27 +00:00
|
|
|
import java.util.Collections;
|
2017-09-03 06:04:18 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import io.reactivex.Maybe;
|
|
|
|
import io.reactivex.Single;
|
|
|
|
|
|
|
|
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 19:04:37 +00:00
|
|
|
private static void checkServiceId(int serviceId) {
|
2018-09-02 23:22:59 +00:00
|
|
|
if (serviceId == Constants.NO_SERVICE_ID) {
|
2017-10-08 19:04:37 +00:00
|
|
|
throw new IllegalArgumentException("serviceId is NO_SERVICE_ID");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 12:27:12 +00:00
|
|
|
public static Single<SearchInfo> searchFor(final int serviceId,
|
2018-07-10 14:26:42 +00:00
|
|
|
final String searchString,
|
|
|
|
final List<String> contentFilter,
|
2018-10-05 14:19:21 +00:00
|
|
|
final String sortFilter) {
|
2017-10-08 19:04:37 +00:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 12:57:31 +00:00
|
|
|
return Single.fromCallable(() ->
|
2018-07-10 14:26:42 +00:00
|
|
|
SearchInfo.getInfo(NewPipe.getService(serviceId),
|
|
|
|
NewPipe.getService(serviceId)
|
2018-08-05 12:20:25 +00:00
|
|
|
.getSearchQHFactory()
|
2018-10-05 14:19:21 +00:00
|
|
|
.fromQuery(searchString, contentFilter, sortFilter)));
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:37:49 +00:00
|
|
|
public static Single<InfoItemsPage> getMoreSearchItems(final int serviceId,
|
2018-07-10 14:26:42 +00:00
|
|
|
final String searchString,
|
|
|
|
final List<String> contentFilter,
|
|
|
|
final String sortFilter,
|
2018-10-05 14:19:21 +00:00
|
|
|
final String pageUrl) {
|
2017-10-08 19:04:37 +00:00
|
|
|
checkServiceId(serviceId);
|
2018-07-08 12:27:12 +00:00
|
|
|
return Single.fromCallable(() ->
|
2018-07-10 14:26:42 +00:00
|
|
|
SearchInfo.getMoreItems(NewPipe.getService(serviceId),
|
|
|
|
NewPipe.getService(serviceId)
|
2018-08-05 12:20:25 +00:00
|
|
|
.getSearchQHFactory()
|
2018-07-10 14:26:42 +00:00
|
|
|
.fromQuery(searchString, contentFilter, sortFilter),
|
|
|
|
pageUrl));
|
2018-07-08 12:27:12 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 12:57:31 +00:00
|
|
|
public static Single<List<String>> suggestionsFor(final int serviceId,
|
2018-10-05 14:19:21 +00:00
|
|
|
final String query) {
|
2017-10-08 19:04:37 +00:00
|
|
|
checkServiceId(serviceId);
|
2018-12-23 21:07:27 +00:00
|
|
|
return Single.fromCallable(() -> {
|
|
|
|
SuggestionExtractor extractor = NewPipe.getService(serviceId)
|
|
|
|
.getSuggestionExtractor();
|
|
|
|
return extractor != null
|
|
|
|
? extractor.suggestionList(query)
|
|
|
|
: Collections.emptyList();
|
|
|
|
});
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 12:57:31 +00:00
|
|
|
public static Single<StreamInfo> getStreamInfo(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
boolean forceLoad) {
|
2017-10-08 19:04:37 +00:00
|
|
|
checkServiceId(serviceId);
|
2018-09-23 01:32:19 +00:00
|
|
|
return checkCache(forceLoad, serviceId, url, InfoItem.InfoType.STREAM, Single.fromCallable(() ->
|
2018-01-20 12:57:31 +00:00
|
|
|
StreamInfo.getInfo(NewPipe.getService(serviceId), url)));
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 12:57:31 +00:00
|
|
|
public static Single<ChannelInfo> getChannelInfo(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
boolean forceLoad) {
|
2017-10-08 19:04:37 +00:00
|
|
|
checkServiceId(serviceId);
|
2018-09-23 01:32:19 +00:00
|
|
|
return checkCache(forceLoad, serviceId, url, InfoItem.InfoType.CHANNEL, Single.fromCallable(() ->
|
2018-01-20 12:57:31 +00:00
|
|
|
ChannelInfo.getInfo(NewPipe.getService(serviceId), url)));
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:37:49 +00:00
|
|
|
public static Single<InfoItemsPage> getMoreChannelItems(final int serviceId,
|
2018-09-02 23:22:59 +00:00
|
|
|
final String url,
|
|
|
|
final String nextStreamsUrl) {
|
2017-10-08 19:04:37 +00:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 12:57:31 +00:00
|
|
|
return Single.fromCallable(() ->
|
|
|
|
ChannelInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl));
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 01:32:19 +00:00
|
|
|
public static Single<CommentsInfo> getCommentsInfo(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
boolean forceLoad) {
|
|
|
|
checkServiceId(serviceId);
|
|
|
|
return checkCache(forceLoad, serviceId, url, InfoItem.InfoType.COMMENT, Single.fromCallable(() ->
|
|
|
|
CommentsInfo.getInfo(NewPipe.getService(serviceId), url)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Single<InfoItemsPage> getMoreCommentItems(final int serviceId,
|
|
|
|
final CommentsInfo info,
|
|
|
|
final String nextPageUrl) {
|
|
|
|
checkServiceId(serviceId);
|
|
|
|
return Single.fromCallable(() ->
|
|
|
|
CommentsInfo.getMoreItems(NewPipe.getService(serviceId), info, nextPageUrl));
|
|
|
|
}
|
|
|
|
|
2018-01-20 12:57:31 +00:00
|
|
|
public static Single<PlaylistInfo> getPlaylistInfo(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
boolean forceLoad) {
|
2017-10-08 19:04:37 +00:00
|
|
|
checkServiceId(serviceId);
|
2018-09-23 01:32:19 +00:00
|
|
|
return checkCache(forceLoad, serviceId, url, InfoItem.InfoType.PLAYLIST, Single.fromCallable(() ->
|
2018-01-20 12:57:31 +00:00
|
|
|
PlaylistInfo.getInfo(NewPipe.getService(serviceId), url)));
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:37:49 +00:00
|
|
|
public static Single<InfoItemsPage> getMorePlaylistItems(final int serviceId,
|
2018-09-02 23:22:59 +00:00
|
|
|
final String url,
|
|
|
|
final String nextStreamsUrl) {
|
2017-10-08 19:04:37 +00:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 12:57:31 +00:00
|
|
|
return Single.fromCallable(() ->
|
|
|
|
PlaylistInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl));
|
2017-09-23 15:39:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 12:57:31 +00:00
|
|
|
public static Single<KioskInfo> getKioskInfo(final int serviceId,
|
|
|
|
final String url,
|
|
|
|
boolean forceLoad) {
|
2018-09-23 01:32:19 +00:00
|
|
|
return checkCache(forceLoad, serviceId, url, InfoItem.InfoType.PLAYLIST, Single.fromCallable(() ->
|
2018-10-05 14:19:21 +00:00
|
|
|
KioskInfo.getInfo(NewPipe.getService(serviceId), url)));
|
2017-09-23 15:39:04 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:37:49 +00:00
|
|
|
public static Single<InfoItemsPage> getMoreKioskItems(final int serviceId,
|
2018-01-20 12:57:31 +00:00
|
|
|
final String url,
|
2018-10-05 14:19:21 +00:00
|
|
|
final String nextStreamsUrl) {
|
2018-01-20 12:57:31 +00:00
|
|
|
return Single.fromCallable(() ->
|
|
|
|
KioskInfo.getMoreItems(NewPipe.getService(serviceId),
|
2018-10-05 14:19:21 +00:00
|
|
|
url, nextStreamsUrl));
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
/**
|
2018-01-20 12:57:31 +00: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 06:04:18 +00:00
|
|
|
* and put the results in the cache.
|
|
|
|
*/
|
2018-01-20 12:57:31 +00:00
|
|
|
private static <I extends Info> Single<I> checkCache(boolean forceLoad,
|
|
|
|
int serviceId,
|
|
|
|
String url,
|
2018-09-23 01:32:19 +00:00
|
|
|
InfoItem.InfoType infoType,
|
2018-01-20 12:57:31 +00:00
|
|
|
Single<I> loadFromNetwork) {
|
2017-10-08 19:04:37 +00:00
|
|
|
checkServiceId(serviceId);
|
2018-09-23 01:32:19 +00:00
|
|
|
loadFromNetwork = loadFromNetwork.doOnSuccess(info -> cache.putInfo(serviceId, url, info, infoType));
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
Single<I> load;
|
|
|
|
if (forceLoad) {
|
2018-09-23 01:32:19 +00:00
|
|
|
cache.removeInfo(serviceId, url, infoType);
|
2017-09-03 06:04:18 +00:00
|
|
|
load = loadFromNetwork;
|
|
|
|
} else {
|
2018-09-29 10:16:47 +00:00
|
|
|
load = Maybe.concat(ExtractorHelper.loadFromCache(serviceId, url, infoType),
|
2018-01-20 12:57:31 +00:00
|
|
|
loadFromNetwork.toMaybe())
|
2017-09-03 06:04:18 +00:00
|
|
|
.firstElement() //Take the first valid
|
|
|
|
.toSingle();
|
|
|
|
}
|
|
|
|
|
|
|
|
return load;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default implementation uses the {@link InfoCache} to get cached results
|
|
|
|
*/
|
2018-09-23 01:32:19 +00:00
|
|
|
public static <I extends Info> Maybe<I> loadFromCache(final int serviceId, final String url, InfoItem.InfoType infoType) {
|
2017-10-08 19:04:37 +00:00
|
|
|
checkServiceId(serviceId);
|
2018-01-20 12:57:31 +00:00
|
|
|
return Maybe.defer(() -> {
|
2018-09-02 23:22:59 +00:00
|
|
|
//noinspection unchecked
|
2018-09-23 01:32:19 +00:00
|
|
|
I info = (I) cache.getFromKey(serviceId, url, infoType);
|
2018-09-02 23:22:59 +00:00
|
|
|
if (MainActivity.DEBUG) Log.d(TAG, "loadFromCache() called, info > " + info);
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2018-09-02 23:22:59 +00:00
|
|
|
// Only return info if it's not null (it is cached)
|
|
|
|
if (info != null) {
|
|
|
|
return Maybe.just(info);
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2018-09-02 23:22:59 +00:00
|
|
|
return Maybe.empty();
|
|
|
|
});
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 10:00:47 +00:00
|
|
|
public static boolean isCached(final int serviceId, final String url, InfoItem.InfoType infoType) {
|
|
|
|
return null != loadFromCache(serviceId, url, infoType).blockingGet();
|
|
|
|
}
|
|
|
|
|
2018-01-23 00:40:00 +00:00
|
|
|
/**
|
|
|
|
* A simple and general error handler that show a Toast for known exceptions, and for others, opens the report error activity with the (optional) error message.
|
|
|
|
*/
|
|
|
|
public static void handleGeneralException(Context context, int serviceId, String url, Throwable exception, UserAction userAction, String optionalErrorMessage) {
|
|
|
|
final Handler handler = new Handler(context.getMainLooper());
|
|
|
|
|
|
|
|
handler.post(() -> {
|
|
|
|
if (exception instanceof ReCaptchaException) {
|
|
|
|
Toast.makeText(context, R.string.recaptcha_request_toast, Toast.LENGTH_LONG).show();
|
|
|
|
// Starting ReCaptcha Challenge Activity
|
|
|
|
Intent intent = new Intent(context, ReCaptchaActivity.class);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
context.startActivity(intent);
|
|
|
|
} else if (exception instanceof IOException) {
|
|
|
|
Toast.makeText(context, R.string.network_error, Toast.LENGTH_LONG).show();
|
|
|
|
} else if (exception instanceof ContentNotAvailableException) {
|
|
|
|
Toast.makeText(context, R.string.content_not_available, Toast.LENGTH_LONG).show();
|
|
|
|
} else {
|
|
|
|
int errorId = exception instanceof YoutubeStreamExtractor.DecryptException ? R.string.youtube_signature_decryption_error :
|
|
|
|
exception instanceof ParsingException ? R.string.parsing_error : R.string.general_error;
|
|
|
|
ErrorActivity.reportError(handler, context, exception, MainActivity.class, null, ErrorActivity.ErrorInfo.make(userAction,
|
|
|
|
serviceId == -1 ? "none" : NewPipe.getNameOfService(serviceId), url + (optionalErrorMessage == null ? "" : optionalErrorMessage), errorId));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
/**
|
|
|
|
* Check if throwable have the cause that can be assignable from the causes to check.
|
|
|
|
*
|
|
|
|
* @see Class#isAssignableFrom(Class)
|
|
|
|
*/
|
2018-01-20 12:57:31 +00:00
|
|
|
public static boolean hasAssignableCauseThrowable(Throwable throwable,
|
|
|
|
Class<?>... causesToCheck) {
|
2017-09-03 06:04:18 +00: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 06:53:31 +00:00
|
|
|
// Check if throwable is a subclass of any of the filtered classes
|
|
|
|
final Class throwableClass = throwable.getClass();
|
2017-09-03 06:04:18 +00:00
|
|
|
for (Class<?> causesEl : causesToCheck) {
|
2018-01-04 06:53:31 +00:00
|
|
|
if (causesEl.isAssignableFrom(throwableClass)) {
|
2017-09-03 06:04:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 06:53:31 +00:00
|
|
|
// Iteratively checks if the root cause of the throwable is a subclass of the filtered class
|
2017-09-03 06:04:18 +00:00
|
|
|
while ((cause = throwable.getCause()) != null && getCause != cause) {
|
|
|
|
getCause = cause;
|
2018-01-04 06:53:31 +00:00
|
|
|
final Class causeClass = cause.getClass();
|
2017-09-03 06:04:18 +00:00
|
|
|
for (Class<?> causesEl : causesToCheck) {
|
2018-01-04 06:53:31 +00:00
|
|
|
if (causesEl.isAssignableFrom(causeClass)) {
|
2017-09-03 06:04:18 +00: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 12:57:31 +00:00
|
|
|
return ExtractorHelper.hasExactCauseThrowable(throwable,
|
|
|
|
InterruptedIOException.class,
|
|
|
|
InterruptedException.class);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
}
|