From 4e6722f201569df3b01af5347a1c4f1336c814e2 Mon Sep 17 00:00:00 2001 From: Ritvik Saraf <13ritvik@gmail.com> Date: Thu, 27 Sep 2018 04:20:57 +0530 Subject: [PATCH] updated extractor and downloader --- app/build.gradle | 2 +- .../java/org/schabi/newpipe/Downloader.java | 22 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0b264d8c6..0eef1e2d0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -55,7 +55,7 @@ dependencies { exclude module: 'support-annotations' } - implementation 'com.github.yausername:NewPipeExtractor:fb14196' + implementation 'com.github.yausername:NewPipeExtractor:6b62091' testImplementation 'junit:junit:4.12' testImplementation 'org.mockito:mockito-core:2.8.9' diff --git a/app/src/main/java/org/schabi/newpipe/Downloader.java b/app/src/main/java/org/schabi/newpipe/Downloader.java index d51d31e11..528141c49 100644 --- a/app/src/main/java/org/schabi/newpipe/Downloader.java +++ b/app/src/main/java/org/schabi/newpipe/Downloader.java @@ -3,6 +3,7 @@ package org.schabi.newpipe; import android.support.annotation.Nullable; import android.text.TextUtils; +import org.schabi.newpipe.extractor.DownloadRequest; import org.schabi.newpipe.extractor.DownloadResponse; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; @@ -186,10 +187,11 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { @Override - public DownloadResponse get(String siteUrl, Map> requestHeaders) throws IOException, ReCaptchaException { + public DownloadResponse get(String siteUrl, DownloadRequest request) throws IOException, ReCaptchaException { final Request.Builder requestBuilder = new Request.Builder() .method("GET", null).url(siteUrl); + Map> requestHeaders = request.getRequestHeaders(); // set custom headers in request for (Map.Entry> pair : requestHeaders.entrySet()) { for(String value : pair.getValue()){ @@ -205,8 +207,8 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { requestBuilder.addHeader("Cookie", mCookies); } - final Request request = requestBuilder.build(); - final Response response = client.newCall(request).execute(); + final Request okRequest = requestBuilder.build(); + final Response response = client.newCall(okRequest).execute(); final ResponseBody body = response.body(); if (response.code() == 429) { @@ -223,12 +225,13 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { @Override public DownloadResponse get(String siteUrl) throws IOException, ReCaptchaException { - return get(siteUrl, Collections.emptyMap()); + return get(siteUrl, DownloadRequest.emptyRequest); } @Override - public DownloadResponse post(String siteUrl, String requestBody, Map> requestHeaders) throws IOException, ReCaptchaException { + public DownloadResponse post(String siteUrl, DownloadRequest request) throws IOException, ReCaptchaException { + Map> requestHeaders = request.getRequestHeaders(); if(null == requestHeaders.get("Content-Type") || requestHeaders.get("Content-Type").isEmpty()){ // content type header is required. maybe throw an exception here return null; @@ -236,7 +239,10 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { String contentType = requestHeaders.get("Content-Type").get(0); - RequestBody okRequestBody = RequestBody.create(MediaType.parse(contentType), requestBody); + RequestBody okRequestBody = null; + if(null != request.getRequestBody()){ + okRequestBody = RequestBody.create(MediaType.parse(contentType), request.getRequestBody()); + } final Request.Builder requestBuilder = new Request.Builder() .method("POST", okRequestBody).url(siteUrl); @@ -255,8 +261,8 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { requestBuilder.addHeader("Cookie", mCookies); } - final Request request = requestBuilder.build(); - final Response response = client.newCall(request).execute(); + final Request okRequest = requestBuilder.build(); + final Response response = client.newCall(okRequest).execute(); final ResponseBody body = response.body(); if (response.code() == 429) {