diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/DownloadResponse.java b/extractor/src/main/java/org/schabi/newpipe/extractor/DownloadResponse.java index 2165002a8..a2fc7e006 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/DownloadResponse.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/DownloadResponse.java @@ -7,15 +7,21 @@ import java.util.Map; import javax.annotation.Nonnull; public class DownloadResponse { + private final int responseCode; private final String responseBody; private final Map> responseHeaders; - public DownloadResponse(String responseBody, Map> headers) { + public DownloadResponse(int responseCode, String responseBody, Map> headers) { super(); + this.responseCode = responseCode; this.responseBody = responseBody; this.responseHeaders = headers; } + public int getResponseCode() { + return responseCode; + } + public String getResponseBody() { return responseBody; } @@ -33,5 +39,4 @@ public class DownloadResponse { else return cookies; } - } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/Downloader.java b/extractor/src/main/java/org/schabi/newpipe/extractor/Downloader.java index 335e8a93e..5f83c82b3 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/Downloader.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/Downloader.java @@ -60,6 +60,8 @@ public interface Downloader { */ String download(String siteUrl) throws IOException, ReCaptchaException; + DownloadResponse head(String siteUrl) throws IOException, ReCaptchaException; + DownloadResponse get(String siteUrl, DownloadRequest request) throws IOException, ReCaptchaException; diff --git a/extractor/src/test/java/org/schabi/newpipe/Downloader.java b/extractor/src/test/java/org/schabi/newpipe/Downloader.java index b57160b2a..1a7536ac4 100644 --- a/extractor/src/test/java/org/schabi/newpipe/Downloader.java +++ b/extractor/src/test/java/org/schabi/newpipe/Downloader.java @@ -172,6 +172,28 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { return dl(con); } + @Override + public DownloadResponse head(String siteUrl) throws IOException, ReCaptchaException { + final HttpsURLConnection con = (HttpsURLConnection) new URL(siteUrl).openConnection(); + + try { + con.setRequestMethod("HEAD"); + setDefaults(con); + } catch (Exception e) { + /* + * 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", con.getURL().toString()); + } + + throw new IOException(con.getResponseCode() + " " + con.getResponseMessage(), e); + } + + return new DownloadResponse(con.getResponseCode(), null, con.getHeaderFields()); + } + @Override public DownloadResponse get(String siteUrl, DownloadRequest request) throws IOException, ReCaptchaException { @@ -183,7 +205,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { } } String responseBody = dl(con); - return new DownloadResponse(responseBody, con.getHeaderFields()); + return new DownloadResponse(con.getResponseCode(), responseBody, con.getHeaderFields()); } @Override @@ -219,6 +241,6 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { sb.append(inputLine); } } - return new DownloadResponse(sb.toString(), con.getHeaderFields()); + return new DownloadResponse(con.getResponseCode(), sb.toString(), con.getHeaderFields()); } }