Skip to content

Commit d97c9e0

Browse files
committed
[YouTube] Improve payloads and URLs of InnerTube requests
For every InnerTube request: - Always add a `request` object with the following properties: - "internalExperimentFlags" set to an empty array; - "useSsl" set to "true"; - "lockedSafetyMode" set to "false". - Use proper TODO comment to provide a way to enable restricted mode on every request and add it on requests on which it wasn't present. For YouTube Music: - Remove alt query parameter, as it is not used anymore by the website; - Add prettyPrint query parameter with false value on YouTube Music search continuations.
1 parent 8a9ebcc commit d97c9e0

2 files changed

Lines changed: 59 additions & 43 deletions

File tree

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

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,20 @@ public static boolean areHardcodedClientVersionAndKeyValid()
551551
.value("gl", "GB")
552552
.value("clientName", "WEB")
553553
.value("clientVersion", HARDCODED_CLIENT_VERSION)
554+
.value("platform", "DESKTOP")
555+
.end()
556+
.object("request")
557+
.array("internalExperimentFlags")
558+
.end()
559+
.value("useSsl", true)
560+
.end()
561+
.object("user")
562+
// TODO: provide a way to enable restricted mode with:
563+
// .value("enableSafetyMode", boolean)
564+
.value("lockedSafetyMode", false)
554565
.end()
555-
.object("user")
556-
.value("lockedSafetyMode", false)
557566
.end()
558567
.value("fetchLiveState", true)
559-
.end()
560568
.end().done().getBytes(StandardCharsets.UTF_8);
561569
// @formatter:on
562570

@@ -770,7 +778,7 @@ public static void setNumberGenerator(final Random random) {
770778
public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
771779
ReCaptchaException {
772780
final String url =
773-
"https://music.youtube.com/youtubei/v1/music/get_search_suggestions?alt=json&key="
781+
"https://music.youtube.com/youtubei/v1/music/get_search_suggestions?key="
774782
+ HARDCODED_YOUTUBE_MUSIC_KEY[0] + DISABLE_PRETTY_PRINT_PARAMETER;
775783

776784
// @formatter:off
@@ -782,19 +790,17 @@ public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
782790
.value("clientVersion", HARDCODED_YOUTUBE_MUSIC_KEY[2])
783791
.value("hl", "en-GB")
784792
.value("gl", "GB")
785-
.array("experimentIds").end()
786-
.value("experimentsToken", "")
787-
.object("locationInfo").end()
788-
.object("musicAppInfo").end()
793+
.value("platform", "DESKTOP")
789794
.end()
790-
.object("capabilities").end()
791795
.object("request")
792-
.array("internalExperimentFlags").end()
793-
.object("sessionIndex").end()
796+
.array("internalExperimentFlags")
797+
.end()
798+
.value("useSsl", true)
794799
.end()
795-
.object("activePlayers").end()
796800
.object("user")
797-
.value("enableSafetyMode", false)
801+
// TODO: provide a way to enable restricted mode with:
802+
// .value("enableSafetyMode", boolean)
803+
.value("lockedSafetyMode", false)
798804
.end()
799805
.end()
800806
.value("input", "")
@@ -1309,8 +1315,8 @@ public static JsonBuilder<JsonObject> prepareDesktopJsonBuilder(
13091315
.value("useSsl", true)
13101316
.end()
13111317
.object("user")
1312-
// TO DO: provide a way to enable restricted mode with:
1313-
// .value("enableSafetyMode", boolean)
1318+
// TODO: provide a way to enable restricted mode with:
1319+
// .value("enableSafetyMode", boolean)
13141320
.value("lockedSafetyMode", false)
13151321
.end()
13161322
.end();
@@ -1345,9 +1351,14 @@ public static JsonBuilder<JsonObject> prepareAndroidMobileJsonBuilder(
13451351
.value("hl", localization.getLocalizationCode())
13461352
.value("gl", contentCountry.getCountryCode())
13471353
.end()
1354+
.object("request")
1355+
.array("internalExperimentFlags")
1356+
.end()
1357+
.value("useSsl", true)
1358+
.end()
13481359
.object("user")
1349-
// TO DO: provide a way to enable restricted mode with:
1350-
// .value("enableSafetyMode", boolean)
1360+
// TODO: provide a way to enable restricted mode with:
1361+
// .value("enableSafetyMode", boolean)
13511362
.value("lockedSafetyMode", false)
13521363
.end()
13531364
.end();
@@ -1380,9 +1391,14 @@ public static JsonBuilder<JsonObject> prepareIosMobileJsonBuilder(
13801391
.value("hl", localization.getLocalizationCode())
13811392
.value("gl", contentCountry.getCountryCode())
13821393
.end()
1394+
.object("request")
1395+
.array("internalExperimentFlags")
1396+
.end()
1397+
.value("useSsl", true)
1398+
.end()
13831399
.object("user")
1384-
// TO DO: provide a way to enable restricted mode with:
1385-
// .value("enableSafetyMode", boolean)
1400+
// TODO: provide a way to enable restricted mode with:
1401+
// .value("enableSafetyMode", boolean)
13861402
.value("lockedSafetyMode", false)
13871403
.end()
13881404
.end();
@@ -1408,9 +1424,14 @@ public static JsonBuilder<JsonObject> prepareTvHtml5EmbedJsonBuilder(
14081424
.object("thirdParty")
14091425
.value("embedUrl", "https://www.youtube.com/watch?v=" + videoId)
14101426
.end()
1427+
.object("request")
1428+
.array("internalExperimentFlags")
1429+
.end()
1430+
.value("useSsl", true)
1431+
.end()
14111432
.object("user")
1412-
// TO DO: provide a way to enable restricted mode with:
1413-
// .value("enableSafetyMode", boolean)
1433+
// TODO: provide a way to enable restricted mode with:
1434+
// .value("enableSafetyMode", boolean)
14141435
.value("lockedSafetyMode", false)
14151436
.end()
14161437
.end();

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

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void onFetchPage(@Nonnull final Downloader downloader)
5454
throws IOException, ExtractionException {
5555
final String[] youtubeMusicKeys = YoutubeParsingHelper.getYoutubeMusicKey();
5656

57-
final String url = "https://music.youtube.com/youtubei/v1/search?alt=json&key="
57+
final String url = "https://music.youtube.com/youtubei/v1/search?key="
5858
+ youtubeMusicKeys[0] + DISABLE_PRETTY_PRINT_PARAMETER;
5959

6060
final String params;
@@ -89,20 +89,17 @@ public void onFetchPage(@Nonnull final Downloader downloader)
8989
.value("clientVersion", youtubeMusicKeys[2])
9090
.value("hl", "en-GB")
9191
.value("gl", getExtractorContentCountry().getCountryCode())
92-
.array("experimentIds").end()
93-
.value("experimentsToken", "")
94-
.object("locationInfo").end()
95-
.object("musicAppInfo").end()
92+
.value("platform", "DESKTOP")
9693
.end()
97-
.object("capabilities").end()
9894
.object("request")
99-
.array("internalExperimentFlags").end()
100-
.object("sessionIndex").end()
95+
.array("internalExperimentFlags")
96+
.end()
97+
.value("useSsl", true)
10198
.end()
102-
.object("activePlayers").end()
10399
.object("user")
104-
// TO DO: provide a way to enable restricted mode with:
105-
.value("enableSafetyMode", false)
100+
// TODO: provide a way to enable restricted mode with:
101+
// .value("enableSafetyMode", boolean)
102+
.value("lockedSafetyMode", false)
106103
.end()
107104
.end()
108105
.value("query", getSearchString())
@@ -219,20 +216,18 @@ public InfoItemsPage<InfoItem> getPage(final Page page)
219216
.value("clientVersion", youtubeMusicKeys[2])
220217
.value("hl", "en-GB")
221218
.value("gl", getExtractorContentCountry().getCountryCode())
222-
.array("experimentIds").end()
223-
.value("experimentsToken", "")
219+
.value("platform", "DESKTOP")
224220
.value("utcOffsetMinutes", 0)
225-
.object("locationInfo").end()
226-
.object("musicAppInfo").end()
227221
.end()
228-
.object("capabilities").end()
229222
.object("request")
230-
.array("internalExperimentFlags").end()
231-
.object("sessionIndex").end()
223+
.array("internalExperimentFlags")
224+
.end()
225+
.value("useSsl", true)
232226
.end()
233-
.object("activePlayers").end()
234227
.object("user")
235-
.value("enableSafetyMode", false)
228+
// TODO: provide a way to enable restricted mode with:
229+
// .value("enableSafetyMode", boolean)
230+
.value("lockedSafetyMode", false)
236231
.end()
237232
.end()
238233
.end().done().getBytes(StandardCharsets.UTF_8);
@@ -310,7 +305,7 @@ private Page getNextPageFrom(final JsonArray continuations)
310305
final String continuation = nextContinuationData.getString("continuation");
311306

312307
return new Page("https://music.youtube.com/youtubei/v1/search?ctoken=" + continuation
313-
+ "&continuation=" + continuation + "&alt=json" + "&key="
314-
+ YoutubeParsingHelper.getYoutubeMusicKey()[0]);
308+
+ "&continuation=" + continuation + "&key="
309+
+ YoutubeParsingHelper.getYoutubeMusicKey()[0] + DISABLE_PRETTY_PRINT_PARAMETER);
315310
}
316311
}

0 commit comments

Comments
 (0)