Skip to content

Commit 8a9e137

Browse files
committed
Extract some code to getValidResponseBody()
1 parent 2af610e commit 8a9e137

2 files changed

Lines changed: 21 additions & 43 deletions

File tree

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

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import org.schabi.newpipe.extractor.InfoItem;
1010
import org.schabi.newpipe.extractor.StreamingService;
1111
import org.schabi.newpipe.extractor.downloader.Downloader;
12-
import org.schabi.newpipe.extractor.downloader.Response;
13-
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
1412
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
1513
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1614
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
@@ -31,6 +29,7 @@
3129
import javax.annotation.Nonnull;
3230

3331
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
32+
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getValidResponseBody;
3433
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
3534
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
3635
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ALBUMS;
@@ -112,23 +111,7 @@ public void onFetchPage(@Nonnull final Downloader downloader) throws IOException
112111
headers.put("Referer", Collections.singletonList("music.youtube.com"));
113112
headers.put("Content-Type", Collections.singletonList("application/json"));
114113

115-
final Response response = getDownloader().post(url, headers, json);
116-
117-
if (response.responseCode() == 404) {
118-
throw new ContentNotAvailableException("Not found" +
119-
" (\"" + response.responseCode() + " " + response.responseMessage() + "\")");
120-
}
121-
122-
final String responseBody = response.responseBody();
123-
if (responseBody.length() < 50) { // ensure to have a valid response
124-
throw new ParsingException("JSON response is too short");
125-
}
126-
127-
final String responseContentType = response.getHeader("Content-Type");
128-
if (responseContentType != null && responseContentType.toLowerCase().contains("text/html")) {
129-
throw new ParsingException("Got HTML document, expected JSON response" +
130-
" (latest url was: \"" + response.latestUrl() + "\")");
131-
}
114+
final String responseBody = getValidResponseBody(getDownloader().post(url, headers, json));
132115

133116
try {
134117
initialData = JsonParser.object().from(responseBody);
@@ -232,23 +215,7 @@ public InfoItemsPage<InfoItem> getPage(final String pageUrl) throws IOException,
232215
headers.put("Referer", Collections.singletonList("music.youtube.com"));
233216
headers.put("Content-Type", Collections.singletonList("application/json"));
234217

235-
final Response response = getDownloader().post(pageUrl, headers, json);
236-
237-
if (response.responseCode() == 404) {
238-
throw new ContentNotAvailableException("Not found" +
239-
" (\"" + response.responseCode() + " " + response.responseMessage() + "\")");
240-
}
241-
242-
final String responseBody = response.responseBody();
243-
if (responseBody.length() < 50) { // ensure to have a valid response
244-
throw new ParsingException("JSON response is too short");
245-
}
246-
247-
final String responseContentType = response.getHeader("Content-Type");
248-
if (responseContentType != null && responseContentType.toLowerCase().contains("text/html")) {
249-
throw new ParsingException("Got HTML document, expected JSON response" +
250-
" (latest url was: \"" + response.latestUrl() + "\")");
251-
}
218+
final String responseBody = getValidResponseBody(getDownloader().post(pageUrl, headers, json));
252219

253220
final JsonObject ajaxJson;
254221
try {

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeParsingHelper.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.IOException;
2222
import java.io.UnsupportedEncodingException;
23+
import java.net.MalformedURLException;
2324
import java.net.URL;
2425
import java.net.URLDecoder;
2526
import java.text.ParseException;
@@ -427,12 +428,8 @@ public static String fixThumbnailUrl(String thumbnailUrl) {
427428
return thumbnailUrl;
428429
}
429430

430-
public static JsonArray getJsonResponse(String url, Localization localization) throws IOException, ExtractionException {
431-
Map<String, List<String>> headers = new HashMap<>();
432-
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
433-
headers.put("X-YouTube-Client-Version", Collections.singletonList(getClientVersion()));
434-
final Response response = getDownloader().get(url, headers, localization);
435-
431+
public static String getValidResponseBody(final Response response)
432+
throws ParsingException, MalformedURLException {
436433
if (response.responseCode() == 404) {
437434
throw new ContentNotAvailableException("Not found" +
438435
" (\"" + response.responseCode() + " " + response.responseMessage() + "\")");
@@ -453,11 +450,24 @@ public static JsonArray getJsonResponse(String url, Localization localization) t
453450
}
454451

455452
final String responseContentType = response.getHeader("Content-Type");
456-
if (responseContentType != null && responseContentType.toLowerCase().contains("text/html")) {
453+
if (responseContentType != null
454+
&& responseContentType.toLowerCase().contains("text/html")) {
457455
throw new ParsingException("Got HTML document, expected JSON response" +
458456
" (latest url was: \"" + response.latestUrl() + "\")");
459457
}
460458

459+
return responseBody;
460+
}
461+
462+
public static JsonArray getJsonResponse(final String url, final Localization localization)
463+
throws IOException, ExtractionException {
464+
Map<String, List<String>> headers = new HashMap<>();
465+
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
466+
headers.put("X-YouTube-Client-Version", Collections.singletonList(getClientVersion()));
467+
final Response response = getDownloader().get(url, headers, localization);
468+
469+
final String responseBody = getValidResponseBody(response);
470+
461471
try {
462472
return JsonParser.array().from(responseBody);
463473
} catch (JsonParserException e) {
@@ -469,6 +479,7 @@ public static JsonArray getJsonResponse(String url, Localization localization) t
469479
* Shared alert detection function, multiple endpoints return the error similarly structured.
470480
* <p>
471481
* Will check if the object has an alert of the type "ERROR".
482+
* </p>
472483
*
473484
* @param initialData the object which will be checked if an alert is present
474485
* @throws ContentNotAvailableException if an alert is detected

0 commit comments

Comments
 (0)