Use Downloader's postWithContentType and postWithContentTypeJson methods in services and extractors

This commit is contained in:
AudricV 2022-07-15 20:56:37 +02:00 committed by Stypox
parent b2862f3cd1
commit 3891542ca1
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
5 changed files with 24 additions and 34 deletions

View File

@ -61,10 +61,9 @@ public final class BandcampExtractorHelper {
*/ */
public static JsonObject getArtistDetails(final String id) throws ParsingException { public static JsonObject getArtistDetails(final String id) throws ParsingException {
try { try {
return JsonParser.object().from(NewPipe.getDownloader().post( return JsonParser.object().from(NewPipe.getDownloader().postWithContentTypeJson(
BASE_API_URL + "/mobile/22/band_details", BASE_API_URL + "/mobile/22/band_details",
Collections.singletonMap("Content-Type", Collections.emptyMap(),
Collections.singletonList("application/json")),
JsonWriter.string() JsonWriter.string()
.object() .object()
.value("band_id", id) .value("band_id", id)

View File

@ -42,10 +42,9 @@ public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem>
public void onFetchPage(@Nonnull final Downloader downloader) public void onFetchPage(@Nonnull final Downloader downloader)
throws IOException, ExtractionException { throws IOException, ExtractionException {
try { try {
json = JsonParser.object().from(getDownloader().post( json = JsonParser.object().from(getDownloader().postWithContentTypeJson(
FEATURED_API_URL, FEATURED_API_URL,
Collections.singletonMap("Content-Type", Collections.emptyMap(),
Collections.singletonList("application/json")),
"{\"platform\":\"\",\"version\":0}".getBytes(StandardCharsets.UTF_8)) "{\"platform\":\"\",\"version\":0}".getBytes(StandardCharsets.UTF_8))
.responseBody()); .responseBody());
} catch (final JsonParserException e) { } catch (final JsonParserException e) {

View File

@ -556,14 +556,13 @@ public final class YoutubeParsingHelper {
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
headers.put("X-YouTube-Client-Name", singletonList("1")); headers.put("X-YouTube-Client-Name", singletonList("1"));
headers.put("X-YouTube-Client-Version", headers.put("X-YouTube-Client-Version", singletonList(HARDCODED_CLIENT_VERSION));
singletonList(HARDCODED_CLIENT_VERSION));
headers.put("Content-Type", Collections.singletonList("application/json"));
// This endpoint is fetched by the YouTube website to get the items of its main menu and is // This endpoint is fetched by the YouTube website to get the items of its main menu and is
// pretty lightweight (around 30kB) // pretty lightweight (around 30kB)
final Response response = getDownloader().post(YOUTUBEI_V1_URL + "guide?key=" final Response response = getDownloader().postWithContentTypeJson(
+ HARDCODED_KEY + DISABLE_PRETTY_PRINT_PARAMETER, headers, body); YOUTUBEI_V1_URL + "guide?key=" + HARDCODED_KEY + DISABLE_PRETTY_PRINT_PARAMETER,
headers, body);
final String responseBody = response.responseBody(); final String responseBody = response.responseBody();
final int responseCode = response.responseCode(); final int responseCode = response.responseCode();
@ -801,15 +800,12 @@ public final class YoutubeParsingHelper {
// @formatter:on // @formatter:on
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
headers.put("X-YouTube-Client-Name", singletonList( headers.put("X-YouTube-Client-Name", singletonList(HARDCODED_YOUTUBE_MUSIC_KEY[1]));
HARDCODED_YOUTUBE_MUSIC_KEY[1])); headers.put("X-YouTube-Client-Version", singletonList(HARDCODED_YOUTUBE_MUSIC_KEY[2]));
headers.put("X-YouTube-Client-Version", singletonList(
HARDCODED_YOUTUBE_MUSIC_KEY[2]));
headers.put("Origin", singletonList("https://music.youtube.com")); headers.put("Origin", singletonList("https://music.youtube.com"));
headers.put("Referer", singletonList("https://music.youtube.com")); headers.put("Referer", singletonList("https://music.youtube.com"));
headers.put("Content-Type", singletonList("application/json"));
final Response response = getDownloader().post(url, headers, json); final Response response = getDownloader().postWithContentTypeJson(url, headers, json);
// Ensure to have a valid response // Ensure to have a valid response
return response.responseBody().length() > 500 && response.responseCode() == 200; return response.responseBody().length() > 500 && response.responseCode() == 200;
} }
@ -1150,11 +1146,10 @@ public final class YoutubeParsingHelper {
throws IOException, ExtractionException { throws IOException, ExtractionException {
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
addYouTubeHeaders(headers); addYouTubeHeaders(headers);
headers.put("Content-Type", singletonList("application/json"));
return JsonUtils.toJsonObject(getValidJsonResponseBody( return JsonUtils.toJsonObject(getValidJsonResponseBody(
getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key=" + getKey() getDownloader().postWithContentTypeJson(YOUTUBEI_V1_URL + endpoint + "?key="
+ DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization))); + getKey() + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
} }
public static JsonObject getJsonAndroidPostResponse( public static JsonObject getJsonAndroidPostResponse(
@ -1185,13 +1180,12 @@ public final class YoutubeParsingHelper {
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
headers.put("User-Agent", singletonList(userAgent)); headers.put("User-Agent", singletonList(userAgent));
headers.put("X-Goog-Api-Format-Version", singletonList("2")); headers.put("X-Goog-Api-Format-Version", singletonList("2"));
headers.put("Content-Type", singletonList("application/json"));
final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
+ DISABLE_PRETTY_PRINT_PARAMETER; + DISABLE_PRETTY_PRINT_PARAMETER;
return JsonUtils.toJsonObject(getValidJsonResponseBody( return JsonUtils.toJsonObject(getValidJsonResponseBody(
getDownloader().post(isNullOrEmpty(endPartOfUrlRequest) getDownloader().postWithContentTypeJson(isNullOrEmpty(endPartOfUrlRequest)
? baseEndpointUrl ? baseEndpointUrl
: baseEndpointUrl + endPartOfUrlRequest, : baseEndpointUrl + endPartOfUrlRequest,
headers, body, localization))); headers, body, localization)));
@ -1404,7 +1398,6 @@ public final class YoutubeParsingHelper {
headers.put("X-YouTube-Client-Version", Collections.singletonList(youtubeMusicKey[2])); headers.put("X-YouTube-Client-Version", Collections.singletonList(youtubeMusicKey[2]));
headers.put("Origin", Collections.singletonList("https://music.youtube.com")); headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
headers.put("Referer", Collections.singletonList("https://music.youtube.com")); headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
headers.put("Content-Type", Collections.singletonList("application/json"));
return headers; return headers;
} }

View File

@ -38,7 +38,6 @@ import org.schabi.newpipe.extractor.utils.JsonUtils;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -92,10 +91,10 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
// Cookie is required due to consent // Cookie is required due to consent
addYouTubeHeaders(headers); addYouTubeHeaders(headers);
headers.put("Content-Type", Collections.singletonList("application/json"));
final Response response = getDownloader().post(YOUTUBEI_V1_URL + "next?key=" + getKey() final Response response = getDownloader().postWithContentTypeJson(
+ DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization); YOUTUBEI_V1_URL + "next?key=" + getKey() + DISABLE_PRETTY_PRINT_PARAMETER,
headers, body, localization);
initialData = JsonUtils.toJsonObject(getValidJsonResponseBody(response)); initialData = JsonUtils.toJsonObject(getValidJsonResponseBody(response));
playlistData = initialData playlistData = initialData
@ -226,10 +225,9 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
// Cookie is required due to consent // Cookie is required due to consent
addYouTubeHeaders(headers); addYouTubeHeaders(headers);
headers.put("Content-Type", Collections.singletonList("application/json"));
final Response response = getDownloader().post(page.getUrl(), headers, page.getBody(), final Response response = getDownloader().postWithContentTypeJson(page.getUrl(), headers,
getExtractorLocalization()); page.getBody(), getExtractorLocalization());
final JsonObject ajaxJson = JsonUtils.toJsonObject(getValidJsonResponseBody(response)); final JsonObject ajaxJson = JsonUtils.toJsonObject(getValidJsonResponseBody(response));
final JsonObject playlistJson = ajaxJson.getObject("contents") final JsonObject playlistJson = ajaxJson.getObject("contents")
.getObject("twoColumnWatchNextResults").getObject("playlist").getObject("playlist"); .getObject("twoColumnWatchNextResults").getObject("playlist").getObject("playlist");

View File

@ -115,8 +115,8 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
.end().done().getBytes(StandardCharsets.UTF_8); .end().done().getBytes(StandardCharsets.UTF_8);
// @formatter:on // @formatter:on
final String responseBody = getValidJsonResponseBody(getDownloader().post(url, final String responseBody = getValidJsonResponseBody(
getYoutubeMusicHeaders(), json)); getDownloader().postWithContentTypeJson(url, getYoutubeMusicHeaders(), json));
try { try {
initialData = JsonParser.object().from(responseBody); initialData = JsonParser.object().from(responseBody);
@ -243,8 +243,9 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
.end().done().getBytes(StandardCharsets.UTF_8); .end().done().getBytes(StandardCharsets.UTF_8);
// @formatter:on // @formatter:on
final String responseBody = getValidJsonResponseBody(getDownloader().post(page.getUrl(), final String responseBody = getValidJsonResponseBody(
getYoutubeMusicHeaders(), json)); getDownloader().postWithContentTypeJson(
page.getUrl(), getYoutubeMusicHeaders(), json));
final JsonObject ajaxJson; final JsonObject ajaxJson;
try { try {