1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-09-27 14:48:23 +00:00

Improve some aspects of the Downloader implementation

This commit is contained in:
Mauricio Colli 2018-02-21 08:05:23 -03:00
parent 99e0f0c3e4
commit 1d2c616ce0
No known key found for this signature in database
GPG Key ID: F200BFD6F29DDD85
4 changed files with 61 additions and 30 deletions

View File

@ -15,6 +15,8 @@ import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.LeakDirectoryProvider;
import com.squareup.leakcanary.RefWatcher;
import org.schabi.newpipe.extractor.Downloader;
import java.io.File;
import java.util.concurrent.TimeUnit;
@ -33,7 +35,12 @@ public class DebugApp extends App {
public void onCreate() {
super.onCreate();
initStetho();
Downloader.client = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).readTimeout(30, TimeUnit.SECONDS).build();
}
@Override
protected Downloader getDownloader() {
return org.schabi.newpipe.Downloader.init(new OkHttpClient.Builder()
.addNetworkInterceptor(new StethoInterceptor()));
}
private void initStetho() {

View File

@ -19,6 +19,7 @@ import org.acra.config.ACRAConfiguration;
import org.acra.config.ACRAConfigurationException;
import org.acra.config.ConfigurationBuilder;
import org.acra.sender.ReportSenderFactory;
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.report.AcraReportSenderFactory;
import org.schabi.newpipe.report.ErrorActivity;
@ -83,7 +84,7 @@ public class App extends Application {
// Initialize settings first because others inits can use its values
SettingsActivity.initSettings(this);
NewPipe.init(Downloader.getInstance());
NewPipe.init(getDownloader());
NewPipeDatabase.init(this);
StateSaver.init(this);
initNotificationChannel();
@ -94,6 +95,10 @@ public class App extends Application {
configureRxJavaErrorHandler();
}
protected Downloader getDownloader() {
return org.schabi.newpipe.Downloader.init(null);
}
private void configureRxJavaErrorHandler() {
// https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

View File

@ -1,8 +1,12 @@
package org.schabi.newpipe;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -10,6 +14,7 @@ import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
/*
@ -33,34 +38,38 @@ import okhttp3.Response;
*/
public class Downloader implements org.schabi.newpipe.extractor.Downloader {
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";
private static String mCookies = "";
private static Downloader instance = null;
private static Downloader instance;
private String mCookies;
private OkHttpClient client;
protected static OkHttpClient client = new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).build();
private Downloader(OkHttpClient.Builder builder) {
this.client = builder
.readTimeout(30, TimeUnit.SECONDS)
//.cache(new Cache(new File(context.getExternalCacheDir(), "okhttp"), 16 * 1024 * 1024))
.build();
}
private Downloader() {
/**
* It's recommended to call exactly once in the entire lifetime of the application.
*
* @param builder if null, default builder will be used
*/
public static Downloader init(@Nullable OkHttpClient.Builder builder) {
return instance = new Downloader(builder != null ? builder : new OkHttpClient.Builder());
}
public static Downloader getInstance() {
if (instance == null) {
synchronized (Downloader.class) {
if (instance == null) {
instance = new Downloader();
}
}
}
return instance;
}
public static synchronized void setCookies(String cookies) {
Downloader.mCookies = cookies;
public String getCookies() {
return mCookies;
}
public static synchronized String getCookies() {
return Downloader.mCookies;
public void setCookies(String cookies) {
mCookies = cookies;
}
/**
@ -89,22 +98,32 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader {
*/
@Override
public String download(String siteUrl, Map<String, String> customProperties) throws IOException, ReCaptchaException {
Request.Builder requestBuilder = new Request.Builder().url(siteUrl).addHeader("User-Agent", USER_AGENT).method("GET", null);
for (Map.Entry<String, String> header : customProperties.entrySet()) {
requestBuilder = requestBuilder.addHeader(header.getKey(), header.getValue());
}
if (getCookies().length() > 0) {
requestBuilder = requestBuilder.addHeader("Cookie", getCookies());
}
Request request = requestBuilder.build();
final Request.Builder requestBuilder = new Request.Builder()
.method("GET", null).url(siteUrl)
.addHeader("User-Agent", USER_AGENT);
Response response = client.newCall(request).execute();
for (Map.Entry<String, String> header : customProperties.entrySet()) {
requestBuilder.addHeader(header.getKey(), header.getValue());
}
if (!TextUtils.isEmpty(mCookies)) {
requestBuilder.addHeader("Cookie", mCookies);
}
final Request request = requestBuilder.build();
final Response response = client.newCall(request).execute();
final ResponseBody body = response.body();
if (response.code() == 429) {
throw new ReCaptchaException("reCaptcha Challenge requested");
}
return response.body().string();
if (body == null) {
response.close();
return null;
}
return body.string();
}
/**
@ -116,6 +135,6 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader {
*/
@Override
public String download(String siteUrl) throws IOException, ReCaptchaException {
return download(siteUrl, new HashMap<>());
return download(siteUrl, Collections.emptyMap());
}
}

View File

@ -107,7 +107,7 @@ public class ReCaptchaActivity extends AppCompatActivity {
// find cookies : s_gl & goojf and Add cookies to Downloader
if (find_access_cookies(cookies)) {
// Give cookies to Downloader class
Downloader.setCookies(mCookies);
Downloader.getInstance().setCookies(mCookies);
// Closing activity and return to parent
setResult(RESULT_OK);