Skip to content

Commit 6b62091

Browse files
committed
moved cookie logic outside
1 parent fb14196 commit 6b62091

5 files changed

Lines changed: 76 additions & 15 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.schabi.newpipe.extractor;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
public class DownloadRequest {
8+
9+
private final String requestBody;
10+
private final Map<String, List<String>> requestHeaders;
11+
public static final DownloadRequest emptyRequest = new DownloadRequest(null, null);
12+
13+
public DownloadRequest(String requestBody, Map<String, List<String>> headers) {
14+
super();
15+
this.requestBody = requestBody;
16+
if(null != headers) {
17+
this.requestHeaders = headers;
18+
}else {
19+
this.requestHeaders = Collections.emptyMap();
20+
}
21+
}
22+
23+
public String getRequestBody() {
24+
return requestBody;
25+
}
26+
27+
public Map<String, List<String>> getRequestHeaders() {
28+
return requestHeaders;
29+
}
30+
31+
public void setRequestCookies(List<String> cookies){
32+
requestHeaders.put("Cookie", cookies);
33+
}
34+
35+
public List<String> getRequestCookies(){
36+
if(null == requestHeaders) return Collections.emptyList();
37+
List<String> cookies = requestHeaders.get("Cookie");
38+
if(null == cookies)
39+
return Collections.emptyList();
40+
else
41+
return cookies;
42+
}
43+
44+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.schabi.newpipe.extractor;
22

3+
import java.util.Collections;
34
import java.util.List;
45
import java.util.Map;
56

7+
import javax.annotation.Nonnull;
8+
69
public class DownloadResponse {
710
private final String responseBody;
811
private final Map<String, List<String>> responseHeaders;
@@ -20,5 +23,15 @@ public String getResponseBody() {
2023
public Map<String, List<String>> getResponseHeaders() {
2124
return responseHeaders;
2225
}
26+
27+
@Nonnull
28+
public List<String> getResponseCookies(){
29+
if(null == responseHeaders) return Collections.emptyList();
30+
List<String> cookies = responseHeaders.get("Set-Cookie");
31+
if(null == cookies)
32+
return Collections.emptyList();
33+
else
34+
return cookies;
35+
}
2336

2437
}

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

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

64-
DownloadResponse get(String siteUrl, Map<String, List<String>> requestHeaders)
64+
DownloadResponse get(String siteUrl, DownloadRequest request)
6565
throws IOException, ReCaptchaException;
6666

6767
DownloadResponse get(String siteUrl) throws IOException, ReCaptchaException;
6868

69-
DownloadResponse post(String siteUrl, String requestBody, Map<String, List<String>> requestHeaders)
69+
DownloadResponse post(String siteUrl, DownloadRequest request)
7070
throws IOException, ReCaptchaException;
7171
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99
import java.util.Map;
1010

11+
import org.schabi.newpipe.extractor.DownloadRequest;
1112
import org.schabi.newpipe.extractor.DownloadResponse;
1213
import org.schabi.newpipe.extractor.Downloader;
1314
import org.schabi.newpipe.extractor.NewPipe;
@@ -145,7 +146,7 @@ private void fetchTitle(JsonArray contents) {
145146
public void onFetchPage(Downloader downloader) throws IOException, ExtractionException {
146147
DownloadResponse response = downloader.get(getUrl());
147148
String responseBody = response.getResponseBody();
148-
cookies = response.getResponseHeaders().get("Set-Cookie");
149+
cookies = response.getResponseCookies();
149150
sessionToken = findValue(responseBody, "XSRF_TOKEN");
150151
String commentsToken = findValue(responseBody, "COMMENTS_TOKEN");
151152
initPage = getPage(getNextPageUrl(commentsToken));
@@ -168,9 +169,10 @@ private String makeAjaxRequest(String siteUrl) throws IOException, ReCaptchaExce
168169
requestHeaders.put("User-Agent", Arrays.asList(USER_AGENT));
169170
requestHeaders.put("X-YouTube-Client-Version", Arrays.asList("2.20180815"));
170171
requestHeaders.put("X-YouTube-Client-Name", Arrays.asList("1"));
171-
requestHeaders.put("Cookie", cookies);
172+
DownloadRequest request = new DownloadRequest(postData, requestHeaders);
173+
request.setRequestCookies(cookies);
172174

173-
return NewPipe.getDownloader().post(siteUrl, postData, requestHeaders).getResponseBody();
175+
return NewPipe.getDownloader().post(siteUrl, request).getResponseBody();
174176
}
175177

176178
private String getDataString(Map<String, String> params) throws UnsupportedEncodingException {

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import javax.net.ssl.HttpsURLConnection;
1414

15+
import org.schabi.newpipe.extractor.DownloadRequest;
1516
import org.schabi.newpipe.extractor.DownloadResponse;
1617
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
1718

@@ -172,11 +173,11 @@ public String download(String siteUrl) throws IOException, ReCaptchaException {
172173
}
173174

174175
@Override
175-
public DownloadResponse get(String siteUrl, Map<String, List<String>> requestHeaders)
176+
public DownloadResponse get(String siteUrl, DownloadRequest request)
176177
throws IOException, ReCaptchaException {
177178
URL url = new URL(siteUrl);
178179
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
179-
for (Map.Entry<String, List<String>> pair : requestHeaders.entrySet()) {
180+
for (Map.Entry<String, List<String>> pair : request.getRequestHeaders().entrySet()) {
180181
for(String value: pair.getValue()) {
181182
con.addRequestProperty(pair.getKey(), value);
182183
}
@@ -187,28 +188,29 @@ public DownloadResponse get(String siteUrl, Map<String, List<String>> requestHea
187188

188189
@Override
189190
public DownloadResponse get(String siteUrl) throws IOException, ReCaptchaException {
190-
return get(siteUrl, Collections.EMPTY_MAP);
191+
return get(siteUrl, DownloadRequest.emptyRequest);
191192
}
192193

193194
@Override
194-
public DownloadResponse post(String siteUrl, String requestBody, Map<String, List<String>> requestHeaders)
195+
public DownloadResponse post(String siteUrl, DownloadRequest request)
195196
throws IOException, ReCaptchaException {
196197
URL url = new URL(siteUrl);
197198
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
198199
con.setRequestMethod("POST");
199-
for (Map.Entry<String, List<String>> pair : requestHeaders.entrySet()) {
200+
for (Map.Entry<String, List<String>> pair : request.getRequestHeaders().entrySet()) {
200201
for(String value: pair.getValue()) {
201202
con.addRequestProperty(pair.getKey(), value);
202203
}
203204
}
204205
// set fields to default if not set already
205206
setDefaults(con);
206207

207-
byte[] postDataBytes = requestBody.toString().getBytes("UTF-8");
208-
con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
209-
210-
con.setDoOutput(true);
211-
con.getOutputStream().write(postDataBytes);
208+
if(null != request.getRequestBody()) {
209+
byte[] postDataBytes = request.getRequestBody().getBytes("UTF-8");
210+
con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
211+
con.setDoOutput(true);
212+
con.getOutputStream().write(postDataBytes);
213+
}
212214

213215
StringBuilder sb = new StringBuilder();
214216
try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {

0 commit comments

Comments
 (0)