Add head request to the current downloader implementation
This commit is contained in:
parent
06f2144e4d
commit
250c0bb1e8
|
@ -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<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();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue