Skip to content

Commit 250c0bb

Browse files
mauriciocolliTobiGr
authored andcommitted
Add head request to the current downloader implementation
1 parent 06f2144 commit 250c0bb

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/DownloadResponse.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@
77
import javax.annotation.Nonnull;
88

99
public class DownloadResponse {
10+
private final int responseCode;
1011
private final String responseBody;
1112
private final Map<String, List<String>> responseHeaders;
1213

13-
public DownloadResponse(String responseBody, Map<String, List<String>> headers) {
14+
public DownloadResponse(int responseCode, String responseBody, Map<String, List<String>> headers) {
1415
super();
16+
this.responseCode = responseCode;
1517
this.responseBody = responseBody;
1618
this.responseHeaders = headers;
1719
}
1820

21+
public int getResponseCode() {
22+
return responseCode;
23+
}
24+
1925
public String getResponseBody() {
2026
return responseBody;
2127
}
@@ -33,5 +39,4 @@ public List<String> getResponseCookies(){
3339
else
3440
return cookies;
3541
}
36-
3742
}

extractor/src/main/java/org/schabi/newpipe/extractor/Downloader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public interface Downloader {
6060
*/
6161
String download(String siteUrl) throws IOException, ReCaptchaException;
6262

63+
DownloadResponse head(String siteUrl) throws IOException, ReCaptchaException;
64+
6365
DownloadResponse get(String siteUrl, DownloadRequest request)
6466
throws IOException, ReCaptchaException;
6567

extractor/src/test/java/org/schabi/newpipe/Downloader.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,28 @@ public String download(String siteUrl) throws IOException, ReCaptchaException {
172172
return dl(con);
173173
}
174174

175+
@Override
176+
public DownloadResponse head(String siteUrl) throws IOException, ReCaptchaException {
177+
final HttpsURLConnection con = (HttpsURLConnection) new URL(siteUrl).openConnection();
178+
179+
try {
180+
con.setRequestMethod("HEAD");
181+
setDefaults(con);
182+
} catch (Exception e) {
183+
/*
184+
* HTTP 429 == Too Many Request Receive from Youtube.com = ReCaptcha challenge
185+
* request See : https://github.com/rg3/youtube-dl/issues/5138
186+
*/
187+
if (con.getResponseCode() == 429) {
188+
throw new ReCaptchaException("reCaptcha Challenge requested", con.getURL().toString());
189+
}
190+
191+
throw new IOException(con.getResponseCode() + " " + con.getResponseMessage(), e);
192+
}
193+
194+
return new DownloadResponse(con.getResponseCode(), null, con.getHeaderFields());
195+
}
196+
175197
@Override
176198
public DownloadResponse get(String siteUrl, DownloadRequest request)
177199
throws IOException, ReCaptchaException {
@@ -183,7 +205,7 @@ public DownloadResponse get(String siteUrl, DownloadRequest request)
183205
}
184206
}
185207
String responseBody = dl(con);
186-
return new DownloadResponse(responseBody, con.getHeaderFields());
208+
return new DownloadResponse(con.getResponseCode(), responseBody, con.getHeaderFields());
187209
}
188210

189211
@Override
@@ -219,6 +241,6 @@ public DownloadResponse post(String siteUrl, DownloadRequest request)
219241
sb.append(inputLine);
220242
}
221243
}
222-
return new DownloadResponse(sb.toString(), con.getHeaderFields());
244+
return new DownloadResponse(con.getResponseCode(), sb.toString(), con.getHeaderFields());
223245
}
224246
}

0 commit comments

Comments
 (0)