Improve some aspects of the Downloader implementation
This commit is contained in:
parent
99e0f0c3e4
commit
1d2c616ce0
4 changed files with 61 additions and 30 deletions
|
@ -15,6 +15,8 @@ import com.squareup.leakcanary.LeakCanary;
|
||||||
import com.squareup.leakcanary.LeakDirectoryProvider;
|
import com.squareup.leakcanary.LeakDirectoryProvider;
|
||||||
import com.squareup.leakcanary.RefWatcher;
|
import com.squareup.leakcanary.RefWatcher;
|
||||||
|
|
||||||
|
import org.schabi.newpipe.extractor.Downloader;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@ -33,7 +35,12 @@ public class DebugApp extends App {
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
initStetho();
|
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() {
|
private void initStetho() {
|
||||||
|
|
|
@ -19,6 +19,7 @@ import org.acra.config.ACRAConfiguration;
|
||||||
import org.acra.config.ACRAConfigurationException;
|
import org.acra.config.ACRAConfigurationException;
|
||||||
import org.acra.config.ConfigurationBuilder;
|
import org.acra.config.ConfigurationBuilder;
|
||||||
import org.acra.sender.ReportSenderFactory;
|
import org.acra.sender.ReportSenderFactory;
|
||||||
|
import org.schabi.newpipe.extractor.Downloader;
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
import org.schabi.newpipe.extractor.NewPipe;
|
||||||
import org.schabi.newpipe.report.AcraReportSenderFactory;
|
import org.schabi.newpipe.report.AcraReportSenderFactory;
|
||||||
import org.schabi.newpipe.report.ErrorActivity;
|
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
|
// Initialize settings first because others inits can use its values
|
||||||
SettingsActivity.initSettings(this);
|
SettingsActivity.initSettings(this);
|
||||||
|
|
||||||
NewPipe.init(Downloader.getInstance());
|
NewPipe.init(getDownloader());
|
||||||
NewPipeDatabase.init(this);
|
NewPipeDatabase.init(this);
|
||||||
StateSaver.init(this);
|
StateSaver.init(this);
|
||||||
initNotificationChannel();
|
initNotificationChannel();
|
||||||
|
@ -94,6 +95,10 @@ public class App extends Application {
|
||||||
configureRxJavaErrorHandler();
|
configureRxJavaErrorHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Downloader getDownloader() {
|
||||||
|
return org.schabi.newpipe.Downloader.init(null);
|
||||||
|
}
|
||||||
|
|
||||||
private void configureRxJavaErrorHandler() {
|
private void configureRxJavaErrorHandler() {
|
||||||
// https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling
|
// https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling
|
||||||
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
|
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
package org.schabi.newpipe;
|
package org.schabi.newpipe;
|
||||||
|
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -10,6 +14,7 @@ import java.util.concurrent.TimeUnit;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
import okhttp3.ResponseBody;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -33,34 +38,38 @@ import okhttp3.Response;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class Downloader implements org.schabi.newpipe.extractor.Downloader {
|
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";
|
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() {
|
public static Downloader getInstance() {
|
||||||
if (instance == null) {
|
|
||||||
synchronized (Downloader.class) {
|
|
||||||
if (instance == null) {
|
|
||||||
instance = new Downloader();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void setCookies(String cookies) {
|
public String getCookies() {
|
||||||
Downloader.mCookies = cookies;
|
return mCookies;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized String getCookies() {
|
public void setCookies(String cookies) {
|
||||||
return Downloader.mCookies;
|
mCookies = cookies;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,22 +98,32 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String download(String siteUrl, Map<String, String> customProperties) throws IOException, ReCaptchaException {
|
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);
|
final Request.Builder requestBuilder = new Request.Builder()
|
||||||
for (Map.Entry<String, String> header : customProperties.entrySet()) {
|
.method("GET", null).url(siteUrl)
|
||||||
requestBuilder = requestBuilder.addHeader(header.getKey(), header.getValue());
|
.addHeader("User-Agent", USER_AGENT);
|
||||||
}
|
|
||||||
if (getCookies().length() > 0) {
|
|
||||||
requestBuilder = requestBuilder.addHeader("Cookie", getCookies());
|
|
||||||
}
|
|
||||||
Request request = requestBuilder.build();
|
|
||||||
|
|
||||||
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) {
|
if (response.code() == 429) {
|
||||||
throw new ReCaptchaException("reCaptcha Challenge requested");
|
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
|
@Override
|
||||||
public String download(String siteUrl) throws IOException, ReCaptchaException {
|
public String download(String siteUrl) throws IOException, ReCaptchaException {
|
||||||
return download(siteUrl, new HashMap<>());
|
return download(siteUrl, Collections.emptyMap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,7 +107,7 @@ public class ReCaptchaActivity extends AppCompatActivity {
|
||||||
// find cookies : s_gl & goojf and Add cookies to Downloader
|
// find cookies : s_gl & goojf and Add cookies to Downloader
|
||||||
if (find_access_cookies(cookies)) {
|
if (find_access_cookies(cookies)) {
|
||||||
// Give cookies to Downloader class
|
// Give cookies to Downloader class
|
||||||
Downloader.setCookies(mCookies);
|
Downloader.getInstance().setCookies(mCookies);
|
||||||
|
|
||||||
// Closing activity and return to parent
|
// Closing activity and return to parent
|
||||||
setResult(RESULT_OK);
|
setResult(RESULT_OK);
|
||||||
|
|
Loading…
Add table
Reference in a new issue