Skip to content

Commit 4c689f1

Browse files
committed
Use Downloader's postWithContentType and postWithContentTypeJson methods in services and extractors
1 parent 5e97664 commit 4c689f1

5 files changed

Lines changed: 27 additions & 35 deletions

File tree

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

598598
final Map<String, List<String>> headers = new HashMap<>();
599599
headers.put("X-YouTube-Client-Name", singletonList("1"));
600-
headers.put("X-YouTube-Client-Version",
601-
singletonList(HARDCODED_CLIENT_VERSION));
602-
headers.put("Content-Type", Collections.singletonList("application/json"));
600+
headers.put("X-YouTube-Client-Version", singletonList(HARDCODED_CLIENT_VERSION));
603601

604602
// This endpoint is fetched by the YouTube website to get the items of its main menu and is
605603
// pretty lightweight (around 30kB)
606-
final Response response = getDownloader().post(YOUTUBEI_V1_URL + "guide?key="
607-
+ HARDCODED_KEY + DISABLE_PRETTY_PRINT_PARAMETER, headers, body);
604+
final Response response = getDownloader().postWithContentTypeJson(
605+
YOUTUBEI_V1_URL + "guide?key=" + HARDCODED_KEY + DISABLE_PRETTY_PRINT_PARAMETER,
606+
headers, body);
608607
final String responseBody = response.responseBody();
609608
final int responseCode = response.responseCode();
610609

@@ -821,15 +820,12 @@ public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
821820
// @formatter:on
822821

823822
final Map<String, List<String>> headers = new HashMap<>();
824-
headers.put("X-YouTube-Client-Name", singletonList(
825-
HARDCODED_YOUTUBE_MUSIC_KEY[1]));
826-
headers.put("X-YouTube-Client-Version", singletonList(
827-
HARDCODED_YOUTUBE_MUSIC_KEY[2]));
823+
headers.put("X-YouTube-Client-Name", singletonList(HARDCODED_YOUTUBE_MUSIC_KEY[1]));
824+
headers.put("X-YouTube-Client-Version", singletonList(HARDCODED_YOUTUBE_MUSIC_KEY[2]));
828825
headers.put("Origin", singletonList("https://music.youtube.com"));
829826
headers.put("Referer", singletonList("https://music.youtube.com"));
830-
headers.put("Content-Type", singletonList("application/json"));
831827

832-
final Response response = getDownloader().post(url, headers, json);
828+
final Response response = getDownloader().postWithContentTypeJson(url, headers, json);
833829
// Ensure to have a valid response
834830
return response.responseBody().length() > 500 && response.responseCode() == 200;
835831
}
@@ -1064,11 +1060,10 @@ public static JsonObject getJsonPostResponse(final String endpoint,
10641060
throws IOException, ExtractionException {
10651061
final Map<String, List<String>> headers = new HashMap<>();
10661062
addYouTubeHeaders(headers);
1067-
headers.put("Content-Type", singletonList("application/json"));
10681063

10691064
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1070-
getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key=" + getKey()
1071-
+ DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
1065+
getDownloader().postWithContentTypeJson(YOUTUBEI_V1_URL + endpoint + "?key="
1066+
+ getKey() + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
10721067
}
10731068

10741069
public static JsonObject getJsonAndroidPostResponse(
@@ -1099,13 +1094,12 @@ private static JsonObject getMobilePostResponse(
10991094
final Map<String, List<String>> headers = new HashMap<>();
11001095
headers.put("User-Agent", singletonList(userAgent));
11011096
headers.put("X-Goog-Api-Format-Version", singletonList("2"));
1102-
headers.put("Content-Type", singletonList("application/json"));
11031097

11041098
final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
11051099
+ DISABLE_PRETTY_PRINT_PARAMETER;
11061100

11071101
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1108-
getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
1102+
getDownloader().postWithContentTypeJson(isNullOrEmpty(endPartOfUrlRequest)
11091103
? baseEndpointUrl
11101104
: baseEndpointUrl + endPartOfUrlRequest,
11111105
headers, body, localization)));
@@ -1293,7 +1287,6 @@ public static Map<String, List<String>> getYoutubeMusicHeaders() {
12931287
headers.put("X-YouTube-Client-Version", Collections.singletonList(youtubeMusicKey[2]));
12941288
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
12951289
headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
1296-
headers.put("Content-Type", Collections.singletonList("application/json"));
12971290
return headers;
12981291
}
12991292

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.net.URL;
4040
import java.nio.charset.StandardCharsets;
4141
import java.util.Arrays;
42-
import java.util.Collections;
4342
import java.util.HashMap;
4443
import java.util.List;
4544
import java.util.Map;
@@ -92,10 +91,10 @@ public void onFetchPage(@Nonnull final Downloader downloader)
9291

9392
final Map<String, List<String>> headers = new HashMap<>();
9493
addClientInfoHeaders(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.getObject("contents")
@@ -220,14 +219,15 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException
220219
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
221220

222221
final Map<String, List<String>> headers = new HashMap<>();
223-
headers.put("Content-Type", Collections.singletonList("application/json"));
224-
225222
addClientInfoHeaders(headers);
223+
224+
// Suppress usage of a singleton list warning in order to allow adding more cookies to
225+
// the request, such as a reCAPTCHA one
226226
//noinspection ArraysAsListWithZeroOrOneArgument
227227
headers.put("Cookie", Arrays.asList(page.getCookies().get(COOKIE_NAME)));
228228

229-
final Response response = getDownloader().post(page.getUrl(), headers, page.getBody(),
230-
getExtractorLocalization());
229+
final Response response = getDownloader().postWithContentTypeJson(page.getUrl(), headers,
230+
page.getBody(), getExtractorLocalization());
231231
final JsonObject ajaxJson = JsonUtils.toJsonObject(getValidJsonResponseBody(response));
232232
final JsonObject playlistJson = ajaxJson.getObject("contents")
233233
.getObject("twoColumnWatchNextResults")

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);
@@ -246,8 +246,9 @@ public InfoItemsPage<InfoItem> getPage(final Page page)
246246
.end().done().getBytes(StandardCharsets.UTF_8);
247247
// @formatter:on
248248

249-
final String responseBody = getValidJsonResponseBody(getDownloader().post(page.getUrl(),
250-
getYoutubeMusicHeaders(), json));
249+
final String responseBody = getValidJsonResponseBody(
250+
getDownloader().postWithContentTypeJson(
251+
page.getUrl(), getYoutubeMusicHeaders(), json));
251252

252253
final JsonObject ajaxJson;
253254
try {

0 commit comments

Comments
 (0)