Skip to content

Commit 54d4551

Browse files
committed
Adress requested changes in YoutubeParsingHelper and update mobile client version
1 parent a59c2a3 commit 54d4551

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ private YoutubeParsingHelper() {
6868

6969
private static final String HARDCODED_CLIENT_VERSION = "2.20210601.07.00";
7070
private static final String HARDCODED_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8";
71-
private static final String[] MOBILE_YOUTUBE_KEYS = {"AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w",
72-
"16.20.35"};
71+
private static final String MOBILE_YOUTUBE_KEY = "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w";
72+
private static final String MOBILE_YOUTUBE_CLIENT_VERSION = "16.20.36";
7373
private static String clientVersion;
7474
private static String key;
7575

76-
private static final String[] HARDCODED_YOUTUBE_MUSIC_KEYS =
76+
private static final String[] HARDCODED_YOUTUBE_MUSIC_KEY =
7777
{"AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30", "67", "0.1"};
7878
private static String[] youtubeMusicKeys;
7979

@@ -401,13 +401,15 @@ private static void extractClientVersionAndKey() throws IOException, ExtractionE
401401

402402
try {
403403
key = Parser.matchGroup1("INNERTUBE_API_KEY\":\"([0-9a-zA-Z_-]+?)\"", html);
404+
keyAndVersionExtracted = true;
404405
} catch (final Parser.RegexException e) {
405406
try {
406407
key = Parser.matchGroup1("innertubeApiKey\":\"([0-9a-zA-Z_-]+?)\"", html);
408+
keyAndVersionExtracted = true;
407409
} catch (final Parser.RegexException ignored) {
408410
}
409411
}
410-
keyAndVersionExtracted = true;
412+
keyAndVersionExtracted = false;
411413
}
412414

413415
/**
@@ -469,15 +471,15 @@ public static boolean areHardcodedYoutubeMusicKeysValid() throws IOException,
469471
ReCaptchaException {
470472
final String url =
471473
"https://music.youtube.com/youtubei/v1/music/get_search_suggestions?alt=json&key="
472-
+ HARDCODED_YOUTUBE_MUSIC_KEYS[0];
474+
+ HARDCODED_YOUTUBE_MUSIC_KEY[0];
473475

474476
// @formatter:off
475477
byte[] json = JsonWriter.string()
476478
.object()
477479
.object("context")
478480
.object("client")
479481
.value("clientName", "WEB_REMIX")
480-
.value("clientVersion", HARDCODED_YOUTUBE_MUSIC_KEYS[2])
482+
.value("clientVersion", HARDCODED_YOUTUBE_MUSIC_KEY[2])
481483
.value("hl", "en-GB")
482484
.value("gl", "GB")
483485
.array("experimentIds").end()
@@ -502,9 +504,9 @@ public static boolean areHardcodedYoutubeMusicKeysValid() throws IOException,
502504

503505
final Map<String, List<String>> headers = new HashMap<>();
504506
headers.put("X-YouTube-Client-Name", Collections.singletonList(
505-
HARDCODED_YOUTUBE_MUSIC_KEYS[1]));
507+
HARDCODED_YOUTUBE_MUSIC_KEY[1]));
506508
headers.put("X-YouTube-Client-Version", Collections.singletonList(
507-
HARDCODED_YOUTUBE_MUSIC_KEYS[2]));
509+
HARDCODED_YOUTUBE_MUSIC_KEY[2]));
508510
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
509511
headers.put("Referer", Collections.singletonList("music.youtube.com"));
510512
headers.put("Content-Type", Collections.singletonList("application/json"));
@@ -520,7 +522,7 @@ public static String[] getYoutubeMusicKeys() throws IOException, ReCaptchaExcept
520522
Parser.RegexException {
521523
if (youtubeMusicKeys != null && youtubeMusicKeys.length == 3) return youtubeMusicKeys;
522524
if (areHardcodedYoutubeMusicKeysValid()) {
523-
return youtubeMusicKeys = HARDCODED_YOUTUBE_MUSIC_KEYS;
525+
return youtubeMusicKeys = HARDCODED_YOUTUBE_MUSIC_KEY;
524526
}
525527

526528
final String url = "https://music.youtube.com/";
@@ -756,13 +758,13 @@ public static JsonObject getJsonMobilePostResponse(final String endpoint,
756758
headers.put("Content-Type", Collections.singletonList("application/json"));
757759
// Spoofing an Android 11 device with the hardcoded version of the Android app
758760
headers.put("User-Agent", Collections.singletonList("com.google.android.youtube/"
759-
+ MOBILE_YOUTUBE_KEYS[1] + "Linux; U; Android 11; "
761+
+ MOBILE_YOUTUBE_CLIENT_VERSION + "Linux; U; Android 11; "
760762
+ contentCountry.getCountryCode() + ") gzip"));
761763
headers.put("x-goog-api-format-version", Collections.singletonList("2"));
762764

763765
final Response response = getDownloader().post(
764766
"https://youtubei.googleapis.com/youtubei/v1/" + endpoint + "?key="
765-
+ MOBILE_YOUTUBE_KEYS[0], headers, body, localization);
767+
+ MOBILE_YOUTUBE_KEY, headers, body, localization);
766768

767769
return JsonUtils.toJsonObject(getValidJsonResponseBody(response));
768770
}
@@ -812,7 +814,7 @@ public static JsonBuilder<JsonObject> prepareMobileJsonBuilder(final Localizatio
812814
.object("context")
813815
.object("client")
814816
.value("clientName", "ANDROID")
815-
.value("clientVersion", MOBILE_YOUTUBE_KEYS[1])
817+
.value("clientVersion", MOBILE_YOUTUBE_CLIENT_VERSION)
816818
.value("hl", localization.getLocalizationCode())
817819
.value("gl", contentCountry.getCountryCode())
818820
.end()

0 commit comments

Comments
 (0)