mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-23 08:30:44 +00:00
Use OkHttp
This commit is contained in:
parent
d36c371c1d
commit
76e082159d
@ -93,4 +93,7 @@ dependencies {
|
|||||||
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
|
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
|
||||||
betaImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
|
betaImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
|
||||||
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
|
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
|
||||||
|
|
||||||
|
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
|
||||||
|
debugImplementation 'com.facebook.stetho:stetho-okhttp3:1.5.0'
|
||||||
}
|
}
|
||||||
|
7
app/proguard-rules.pro
vendored
7
app/proguard-rules.pro
vendored
@ -35,3 +35,10 @@
|
|||||||
@icepick.* <fields>;
|
@icepick.* <fields>;
|
||||||
}
|
}
|
||||||
-keepnames class * { @icepick.State *;}
|
-keepnames class * { @icepick.State *;}
|
||||||
|
|
||||||
|
# Rules for OkHttp. Copy paste from https://github.com/square/okhttp
|
||||||
|
-dontwarn okhttp3.**
|
||||||
|
-dontwarn okio.**
|
||||||
|
-dontwarn javax.annotation.**
|
||||||
|
# A resource is loaded with a relative path so the package of this class must be preserved.
|
||||||
|
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
|
||||||
|
@ -7,6 +7,7 @@ import android.support.annotation.NonNull;
|
|||||||
import android.support.multidex.MultiDex;
|
import android.support.multidex.MultiDex;
|
||||||
|
|
||||||
import com.facebook.stetho.Stetho;
|
import com.facebook.stetho.Stetho;
|
||||||
|
import com.facebook.stetho.okhttp3.StethoInterceptor;
|
||||||
import com.squareup.leakcanary.AndroidHeapDumper;
|
import com.squareup.leakcanary.AndroidHeapDumper;
|
||||||
import com.squareup.leakcanary.DefaultLeakDirectoryProvider;
|
import com.squareup.leakcanary.DefaultLeakDirectoryProvider;
|
||||||
import com.squareup.leakcanary.HeapDumper;
|
import com.squareup.leakcanary.HeapDumper;
|
||||||
@ -17,6 +18,8 @@ import com.squareup.leakcanary.RefWatcher;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
|
||||||
public class DebugApp extends App {
|
public class DebugApp extends App {
|
||||||
private static final String TAG = DebugApp.class.toString();
|
private static final String TAG = DebugApp.class.toString();
|
||||||
|
|
||||||
@ -30,6 +33,7 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initStetho() {
|
private void initStetho() {
|
||||||
|
@ -1,20 +1,15 @@
|
|||||||
package org.schabi.newpipe;
|
package org.schabi.newpipe;
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
||||||
import org.schabi.newpipe.util.ExtractorHelper;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.InterruptedIOException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -44,6 +39,8 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader {
|
|||||||
|
|
||||||
private static Downloader instance = null;
|
private static Downloader instance = null;
|
||||||
|
|
||||||
|
protected static OkHttpClient client = new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).build();
|
||||||
|
|
||||||
private Downloader() {
|
private Downloader() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,14 +89,22 @@ 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 {
|
||||||
URL url = new URL(siteUrl);
|
Request.Builder requestBuilder = new Request.Builder().url(siteUrl).addHeader("User-Agent", USER_AGENT).method("GET", null);
|
||||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
for (Map.Entry<String, String> header : customProperties.entrySet()) {
|
||||||
Iterator it = customProperties.entrySet().iterator();
|
requestBuilder = requestBuilder.addHeader(header.getKey(), header.getValue());
|
||||||
while (it.hasNext()) {
|
|
||||||
Map.Entry pair = (Map.Entry) it.next();
|
|
||||||
con.setRequestProperty((String) pair.getKey(), (String) pair.getValue());
|
|
||||||
}
|
}
|
||||||
return dl(con);
|
if (getCookies().length() > 0) {
|
||||||
|
requestBuilder = requestBuilder.addHeader("Cookie", getCookies());
|
||||||
|
}
|
||||||
|
Request request = requestBuilder.build();
|
||||||
|
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
|
||||||
|
if (response.code() == 429) {
|
||||||
|
throw new ReCaptchaException("reCaptcha Challenge requested");
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.body().string();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,57 +116,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 {
|
||||||
URL url = new URL(siteUrl);
|
return download(siteUrl, new HashMap<>());
|
||||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
|
||||||
//HttpsURLConnection con = NetCipher.getHttpsURLConnection(url);
|
|
||||||
return dl(con);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Common functionality between download(String url) and download(String url, String language)
|
|
||||||
*/
|
|
||||||
private static String dl(HttpsURLConnection con) throws IOException, ReCaptchaException {
|
|
||||||
StringBuilder response = new StringBuilder();
|
|
||||||
BufferedReader in = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
con.setReadTimeout(30 * 1000);// 30s
|
|
||||||
con.setRequestMethod("GET");
|
|
||||||
con.setRequestProperty("User-Agent", USER_AGENT);
|
|
||||||
|
|
||||||
if (getCookies().length() > 0) {
|
|
||||||
con.setRequestProperty("Cookie", getCookies());
|
|
||||||
}
|
|
||||||
|
|
||||||
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
|
||||||
|
|
||||||
String inputLine;
|
|
||||||
while ((inputLine = in.readLine()) != null) {
|
|
||||||
response.append(inputLine);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.e("Downloader", "dl() ----- Exception thrown → " + e.getClass().getName());
|
|
||||||
|
|
||||||
if (ExtractorHelper.isInterruptedCaused(e)) {
|
|
||||||
throw new InterruptedIOException(e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* HTTP 429 == Too Many Request
|
|
||||||
* Receive from Youtube.com = ReCaptcha challenge request
|
|
||||||
* See : https://github.com/rg3/youtube-dl/issues/5138
|
|
||||||
*/
|
|
||||||
if (con.getResponseCode() == 429) {
|
|
||||||
throw new ReCaptchaException("reCaptcha Challenge requested");
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IOException(con.getResponseCode() + " " + con.getResponseMessage(), e);
|
|
||||||
} finally {
|
|
||||||
if (in != null) {
|
|
||||||
in.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user