Skip to content

Commit 9db2b00

Browse files
committed
Use Downloader's postWithContentType and postWithContentTypeJson methods in services and extractors
1 parent 73df3ba commit 9db2b00

File tree

5 files changed

+24
-34
lines changed

5 files changed

+24
-34
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ public static String getStreamUrlFromIds(final long bandId,
6161
*/
6262
public static JsonObject getArtistDetails(final String id) throws ParsingException {
6363
try {
64-
return JsonParser.object().from(NewPipe.getDownloader().post(
64+
return JsonParser.object().from(NewPipe.getDownloader().postWithContentTypeJson(
6565
BASE_API_URL + "/mobile/22/band_details",
66-
Collections.singletonMap("Content-Type",
67-
Collections.singletonList("application/json")),
66+
Collections.emptyMap(),
6867
JsonWriter.string()
6968
.object()
7069
.value("band_id", id)

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ public BandcampFeaturedExtractor(final StreamingService streamingService,
4242
public void onFetchPage(@Nonnull final Downloader downloader)
4343
throws IOException, ExtractionException {
4444
try {
45-
json = JsonParser.object().from(getDownloader().post(
45+
json = JsonParser.object().from(getDownloader().postWithContentTypeJson(
4646
FEATURED_API_URL,
47-
Collections.singletonMap("Content-Type",
48-
Collections.singletonList("application/json")),
47+
Collections.emptyMap(),
4948
"{\"platform\":\"\",\"version\":0}".getBytes(StandardCharsets.UTF_8))
5049
.responseBody());
5150
} catch (final JsonParserException e) {

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,13 @@ public static boolean areHardcodedClientVersionAndKeyValid()
601601

602602
final Map<String, List<String>> headers = new HashMap<>();
603603
headers.put("X-YouTube-Client-Name", singletonList("1"));
604-
headers.put("X-YouTube-Client-Version",
605-
singletonList(HARDCODED_CLIENT_VERSION));
606-
headers.put("Content-Type", Collections.singletonList("application/json"));
604+
headers.put("X-YouTube-Client-Version", singletonList(HARDCODED_CLIENT_VERSION));
607605

608606
// This endpoint is fetched by the YouTube website to get the items of its main menu and is
609607
// pretty lightweight (around 30kB)
610-
final Response response = getDownloader().post(YOUTUBEI_V1_URL + "guide?key="
611-
+ HARDCODED_KEY + DISABLE_PRETTY_PRINT_PARAMETER, headers, body);
608+
final Response response = getDownloader().postWithContentTypeJson(
609+
YOUTUBEI_V1_URL + "guide?key=" + HARDCODED_KEY + DISABLE_PRETTY_PRINT_PARAMETER,
610+
headers, body);
612611
final String responseBody = response.responseBody();
613612
final int responseCode = response.responseCode();
614613

@@ -846,15 +845,12 @@ public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
846845
// @formatter:on
847846

848847
final Map<String, List<String>> headers = new HashMap<>();
849-
headers.put("X-YouTube-Client-Name", singletonList(
850-
HARDCODED_YOUTUBE_MUSIC_KEY[1]));
851-
headers.put("X-YouTube-Client-Version", singletonList(
852-
HARDCODED_YOUTUBE_MUSIC_KEY[2]));
848+
headers.put("X-YouTube-Client-Name", singletonList(HARDCODED_YOUTUBE_MUSIC_KEY[1]));
849+
headers.put("X-YouTube-Client-Version", singletonList(HARDCODED_YOUTUBE_MUSIC_KEY[2]));
853850
headers.put("Origin", singletonList("https://music.youtube.com"));
854851
headers.put("Referer", singletonList("https://music.youtube.com"));
855-
headers.put("Content-Type", singletonList("application/json"));
856852

857-
final Response response = getDownloader().post(url, headers, json);
853+
final Response response = getDownloader().postWithContentTypeJson(url, headers, json);
858854
// Ensure to have a valid response
859855
return response.responseBody().length() > 500 && response.responseCode() == 200;
860856
}
@@ -1089,11 +1085,10 @@ public static JsonObject getJsonPostResponse(final String endpoint,
10891085
throws IOException, ExtractionException {
10901086
final Map<String, List<String>> headers = new HashMap<>();
10911087
addYouTubeHeaders(headers);
1092-
headers.put("Content-Type", singletonList("application/json"));
10931088

10941089
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1095-
getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key=" + getKey()
1096-
+ DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
1090+
getDownloader().postWithContentTypeJson(YOUTUBEI_V1_URL + endpoint + "?key="
1091+
+ getKey() + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
10971092
}
10981093

10991094
public static JsonObject getJsonAndroidPostResponse(
@@ -1124,13 +1119,12 @@ private static JsonObject getMobilePostResponse(
11241119
final Map<String, List<String>> headers = new HashMap<>();
11251120
headers.put("User-Agent", singletonList(userAgent));
11261121
headers.put("X-Goog-Api-Format-Version", singletonList("2"));
1127-
headers.put("Content-Type", singletonList("application/json"));
11281122

11291123
final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
11301124
+ DISABLE_PRETTY_PRINT_PARAMETER;
11311125

11321126
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1133-
getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
1127+
getDownloader().postWithContentTypeJson(isNullOrEmpty(endPartOfUrlRequest)
11341128
? baseEndpointUrl
11351129
: baseEndpointUrl + endPartOfUrlRequest,
11361130
headers, body, localization)));
@@ -1343,7 +1337,6 @@ public static Map<String, List<String>> getYoutubeMusicHeaders() {
13431337
headers.put("X-YouTube-Client-Version", Collections.singletonList(youtubeMusicKey[2]));
13441338
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
13451339
headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
1346-
headers.put("Content-Type", Collections.singletonList("application/json"));
13471340
return headers;
13481341
}
13491342

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.io.IOException;
3939
import java.net.URL;
4040
import java.nio.charset.StandardCharsets;
41-
import java.util.Collections;
4241
import java.util.HashMap;
4342
import java.util.List;
4443
import java.util.Map;
@@ -92,10 +91,10 @@ public void onFetchPage(@Nonnull final Downloader downloader)
9291
final Map<String, List<String>> headers = new HashMap<>();
9392
// Cookie is required due to consent
9493
addYouTubeHeaders(headers);
95-
headers.put("Content-Type", Collections.singletonList("application/json"));
9694

97-
final Response response = getDownloader().post(YOUTUBEI_V1_URL + "next?key=" + getKey()
98-
+ DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization);
95+
final Response response = getDownloader().postWithContentTypeJson(
96+
YOUTUBEI_V1_URL + "next?key=" + getKey() + DISABLE_PRETTY_PRINT_PARAMETER,
97+
headers, body, localization);
9998

10099
initialData = JsonUtils.toJsonObject(getValidJsonResponseBody(response));
101100
playlistData = initialData
@@ -226,10 +225,9 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException
226225
final Map<String, List<String>> headers = new HashMap<>();
227226
// Cookie is required due to consent
228227
addYouTubeHeaders(headers);
229-
headers.put("Content-Type", Collections.singletonList("application/json"));
230228

231-
final Response response = getDownloader().post(page.getUrl(), headers, page.getBody(),
232-
getExtractorLocalization());
229+
final Response response = getDownloader().postWithContentTypeJson(page.getUrl(), headers,
230+
page.getBody(), getExtractorLocalization());
233231
final JsonObject ajaxJson = JsonUtils.toJsonObject(getValidJsonResponseBody(response));
234232
final JsonObject playlistJson = ajaxJson.getObject("contents")
235233
.getObject("twoColumnWatchNextResults").getObject("playlist").getObject("playlist");

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ public void onFetchPage(@Nonnull final Downloader downloader)
115115
.end().done().getBytes(StandardCharsets.UTF_8);
116116
// @formatter:on
117117

118-
final String responseBody = getValidJsonResponseBody(getDownloader().post(url,
119-
getYoutubeMusicHeaders(), json));
118+
final String responseBody = getValidJsonResponseBody(
119+
getDownloader().postWithContentTypeJson(url, getYoutubeMusicHeaders(), json));
120120

121121
try {
122122
initialData = JsonParser.object().from(responseBody);
@@ -243,8 +243,9 @@ public InfoItemsPage<InfoItem> getPage(final Page page)
243243
.end().done().getBytes(StandardCharsets.UTF_8);
244244
// @formatter:on
245245

246-
final String responseBody = getValidJsonResponseBody(getDownloader().post(page.getUrl(),
247-
getYoutubeMusicHeaders(), json));
246+
final String responseBody = getValidJsonResponseBody(
247+
getDownloader().postWithContentTypeJson(
248+
page.getUrl(), getYoutubeMusicHeaders(), json));
248249

249250
final JsonObject ajaxJson;
250251
try {

0 commit comments

Comments
 (0)