Skip to content

Commit 9520317

Browse files
committed
Use Downloader's postWithContentType and postWithContentTypeJson methods in services and extractors
1 parent 773b671 commit 9520317

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
@@ -602,14 +602,13 @@ public static boolean areHardcodedClientVersionAndKeyValid()
602602

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

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

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

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

858-
final Response response = getDownloader().post(url, headers, json);
854+
final Response response = getDownloader().postWithContentTypeJson(url, headers, json);
859855
// Ensure to have a valid response
860856
return response.responseBody().length() > 500 && response.responseCode() == 200;
861857
}
@@ -1090,11 +1086,10 @@ public static JsonObject getJsonPostResponse(final String endpoint,
10901086
throws IOException, ExtractionException {
10911087
final Map<String, List<String>> headers = new HashMap<>();
10921088
addYouTubeHeaders(headers);
1093-
headers.put("Content-Type", singletonList("application/json"));
10941089

10951090
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1096-
getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key=" + getKey()
1097-
+ DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
1091+
getDownloader().postWithContentTypeJson(YOUTUBEI_V1_URL + endpoint + "?key="
1092+
+ getKey() + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
10981093
}
10991094

11001095
public static JsonObject getJsonAndroidPostResponse(
@@ -1125,13 +1120,12 @@ private static JsonObject getMobilePostResponse(
11251120
final Map<String, List<String>> headers = new HashMap<>();
11261121
headers.put("User-Agent", singletonList(userAgent));
11271122
headers.put("X-Goog-Api-Format-Version", singletonList("2"));
1128-
headers.put("Content-Type", singletonList("application/json"));
11291123

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

11331127
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1134-
getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
1128+
getDownloader().postWithContentTypeJson(isNullOrEmpty(endPartOfUrlRequest)
11351129
? baseEndpointUrl
11361130
: baseEndpointUrl + endPartOfUrlRequest,
11371131
headers, body, localization)));
@@ -1344,7 +1338,6 @@ public static Map<String, List<String>> getYoutubeMusicHeaders() {
13441338
headers.put("X-YouTube-Client-Version", Collections.singletonList(youtubeMusicKey[2]));
13451339
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
13461340
headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
1347-
headers.put("Content-Type", Collections.singletonList("application/json"));
13481341
return headers;
13491342
}
13501343

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
@@ -39,7 +39,6 @@
3939
import java.io.IOException;
4040
import java.net.URL;
4141
import java.nio.charset.StandardCharsets;
42-
import java.util.Collections;
4342
import java.util.HashMap;
4443
import java.util.List;
4544
import java.util.Map;
@@ -93,10 +92,10 @@ public void onFetchPage(@Nonnull final Downloader downloader)
9392
final Map<String, List<String>> headers = new HashMap<>();
9493
// Cookie is required due to consent
9594
addYouTubeHeaders(headers);
96-
headers.put("Content-Type", Collections.singletonList("application/json"));
9795

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

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

232-
final Response response = getDownloader().post(page.getUrl(), headers, page.getBody(),
233-
getExtractorLocalization());
230+
final Response response = getDownloader().postWithContentTypeJson(page.getUrl(), headers,
231+
page.getBody(), getExtractorLocalization());
234232
final JsonObject ajaxJson = JsonUtils.toJsonObject(getValidJsonResponseBody(response));
235233
final JsonObject playlistJson = ajaxJson.getObject("contents")
236234
.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
@@ -116,8 +116,8 @@ public void onFetchPage(@Nonnull final Downloader downloader)
116116
.end().done().getBytes(StandardCharsets.UTF_8);
117117
// @formatter:on
118118

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

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

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

250251
final JsonObject ajaxJson;
251252
try {

0 commit comments

Comments
 (0)