From 76e082159dbbf5b7bc14fc26ad6030bffed7484b Mon Sep 17 00:00:00 2001 From: wb9688 Date: Tue, 20 Feb 2018 16:24:43 +0100 Subject: [PATCH 1/2] Use OkHttp --- app/build.gradle | 3 + app/proguard-rules.pro | 7 ++ .../java/org/schabi/newpipe/DebugApp.java | 4 + .../java/org/schabi/newpipe/Downloader.java | 90 +++++-------------- 4 files changed, 36 insertions(+), 68 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 16609fbab..0244ae4b9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -93,4 +93,7 @@ dependencies { debugImplementation 'com.squareup.leakcanary:leakcanary-android: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' + + implementation 'com.squareup.okhttp3:okhttp:3.9.1' + debugImplementation 'com.facebook.stetho:stetho-okhttp3:1.5.0' } diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 1b2ac6835..fbe5ab23f 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -35,3 +35,10 @@ @icepick.* ; } -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 diff --git a/app/src/debug/java/org/schabi/newpipe/DebugApp.java b/app/src/debug/java/org/schabi/newpipe/DebugApp.java index ba1fd90cc..1ba837cdd 100644 --- a/app/src/debug/java/org/schabi/newpipe/DebugApp.java +++ b/app/src/debug/java/org/schabi/newpipe/DebugApp.java @@ -7,6 +7,7 @@ import android.support.annotation.NonNull; import android.support.multidex.MultiDex; import com.facebook.stetho.Stetho; +import com.facebook.stetho.okhttp3.StethoInterceptor; import com.squareup.leakcanary.AndroidHeapDumper; import com.squareup.leakcanary.DefaultLeakDirectoryProvider; import com.squareup.leakcanary.HeapDumper; @@ -17,6 +18,8 @@ import com.squareup.leakcanary.RefWatcher; import java.io.File; import java.util.concurrent.TimeUnit; +import okhttp3.OkHttpClient; + public class DebugApp extends App { private static final String TAG = DebugApp.class.toString(); @@ -30,6 +33,7 @@ public class DebugApp extends App { public void onCreate() { super.onCreate(); initStetho(); + Downloader.client = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).readTimeout(30, TimeUnit.SECONDS).build(); } private void initStetho() { diff --git a/app/src/main/java/org/schabi/newpipe/Downloader.java b/app/src/main/java/org/schabi/newpipe/Downloader.java index 77f12fa46..029c9c5a6 100644 --- a/app/src/main/java/org/schabi/newpipe/Downloader.java +++ b/app/src/main/java/org/schabi/newpipe/Downloader.java @@ -1,20 +1,15 @@ package org.schabi.newpipe; -import android.util.Log; - import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; -import org.schabi.newpipe.util.ExtractorHelper; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.InterruptedIOException; -import java.net.URL; import java.util.HashMap; -import java.util.Iterator; 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; + protected static OkHttpClient client = new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).build(); + private Downloader() { } @@ -92,14 +89,22 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { */ @Override public String download(String siteUrl, Map customProperties) throws IOException, ReCaptchaException { - URL url = new URL(siteUrl); - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - Iterator it = customProperties.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry) it.next(); - con.setRequestProperty((String) pair.getKey(), (String) pair.getValue()); + Request.Builder requestBuilder = new Request.Builder().url(siteUrl).addHeader("User-Agent", USER_AGENT).method("GET", null); + for (Map.Entry header : customProperties.entrySet()) { + requestBuilder = requestBuilder.addHeader(header.getKey(), header.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 public String download(String siteUrl) throws IOException, ReCaptchaException { - URL url = new URL(siteUrl); - 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(); + return download(siteUrl, new HashMap<>()); } } From a1aaa52c2a00c4288fcb57453b679946bce08da2 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Wed, 21 Feb 2018 00:24:43 +0100 Subject: [PATCH 2/2] add link to NewPipe blog --- app/src/main/java/org/schabi/newpipe/MainActivity.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index ac058f52c..e696f867f 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -23,6 +23,7 @@ package org.schabi.newpipe; import android.annotation.SuppressLint; import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -154,8 +155,9 @@ public class MainActivity extends AppCompatActivity { headerServiceView = findViewById(R.id.drawer_header_service_view); Button action = findViewById(R.id.drawer_header_action_button); action.setOnClickListener(view -> { - Toast.makeText(this, - R.string.drawer_header_action_paceholder_text, Toast.LENGTH_SHORT).show(); + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse("https://newpipe.schabi.org/blog/")); + startActivity(intent); drawer.closeDrawers(); }); }