Add head request to the current downloader implementation

This commit is contained in:
Mauricio Colli 2019-10-22 13:21:23 -03:00 committed by Tobias Groza
parent 06f2144e4d
commit 250c0bb1e8
3 changed files with 33 additions and 4 deletions

View File

@ -7,15 +7,21 @@ import java.util.Map;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public class DownloadResponse { public class DownloadResponse {
private final int responseCode;
private final String responseBody; private final String responseBody;
private final Map<String, List<String>> responseHeaders; private final Map<String, List<String>> responseHeaders;
public DownloadResponse(String responseBody, Map<String, List<String>> headers) { public DownloadResponse(int responseCode, String responseBody, Map<String, List<String>> headers) {
super(); super();
this.responseCode = responseCode;
this.responseBody = responseBody; this.responseBody = responseBody;
this.responseHeaders = headers; this.responseHeaders = headers;
} }
public int getResponseCode() {
return responseCode;
}
public String getResponseBody() { public String getResponseBody() {
return responseBody; return responseBody;
} }
@ -33,5 +39,4 @@ public class DownloadResponse {
else else
return cookies; return cookies;
} }
} }

View File

@ -60,6 +60,8 @@ public interface Downloader {
*/ */
String download(String siteUrl) throws IOException, ReCaptchaException; String download(String siteUrl) throws IOException, ReCaptchaException;
DownloadResponse head(String siteUrl) throws IOException, ReCaptchaException;
DownloadResponse get(String siteUrl, DownloadRequest request) DownloadResponse get(String siteUrl, DownloadRequest request)
throws IOException, ReCaptchaException; throws IOException, ReCaptchaException;

View File

@ -172,6 +172,28 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader {
return dl(con); 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 @Override
public DownloadResponse get(String siteUrl, DownloadRequest request) public DownloadResponse get(String siteUrl, DownloadRequest request)
throws IOException, ReCaptchaException { throws IOException, ReCaptchaException {
@ -183,7 +205,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader {
} }
} }
String responseBody = dl(con); String responseBody = dl(con);
return new DownloadResponse(responseBody, con.getHeaderFields()); return new DownloadResponse(con.getResponseCode(), responseBody, con.getHeaderFields());
} }
@Override @Override
@ -219,6 +241,6 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader {
sb.append(inputLine); sb.append(inputLine);
} }
} }
return new DownloadResponse(sb.toString(), con.getHeaderFields()); return new DownloadResponse(con.getResponseCode(), sb.toString(), con.getHeaderFields());
} }
} }