Adress requested changes in YoutubeParsingHelper

This commit is contained in:
TiA4f8R 2021-06-06 15:39:45 +02:00
parent 657f165771
commit 632772d17f
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
3 changed files with 20 additions and 21 deletions

View File

@ -75,7 +75,7 @@ public class YoutubeParsingHelper {
private static final String[] HARDCODED_YOUTUBE_MUSIC_KEY = private static final String[] HARDCODED_YOUTUBE_MUSIC_KEY =
{"AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30", "67", "0.1"}; {"AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30", "67", "0.1"};
private static String[] youtubeMusicKeys; private static String[] youtubeMusicKey;
private static boolean keyAndVersionExtracted = false; private static boolean keyAndVersionExtracted = false;
private static Boolean areHardcodedClientVersionAndKeyValidValue = null; private static Boolean areHardcodedClientVersionAndKeyValidValue = null;
@ -341,7 +341,7 @@ public class YoutubeParsingHelper {
private static void extractClientVersionAndKey() throws IOException, ExtractionException { private static void extractClientVersionAndKey() throws IOException, ExtractionException {
// Don't extract the client version and the innertube key if it has been already extracted // Don't extract the client version and the innertube key if it has been already extracted
if (!keyAndVersionExtracted) return; if (keyAndVersionExtracted) return;
// Don't provide a search term in order to have a smaller response // Don't provide a search term in order to have a smaller response
final String url = "https://www.youtube.com/results?search_query="; final String url = "https://www.youtube.com/results?search_query=";
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
@ -405,7 +405,6 @@ public class YoutubeParsingHelper {
try { try {
key = Parser.matchGroup1("innertubeApiKey\":\"([0-9a-zA-Z_-]+?)\"", html); key = Parser.matchGroup1("innertubeApiKey\":\"([0-9a-zA-Z_-]+?)\"", html);
} catch (final Parser.RegexException e2) { } catch (final Parser.RegexException e2) {
keyAndVersionExtracted = false;
throw new ParsingException("Could not extract client version and key"); throw new ParsingException("Could not extract client version and key");
} }
} }
@ -421,7 +420,7 @@ public class YoutubeParsingHelper {
return clientVersion = HARDCODED_CLIENT_VERSION; return clientVersion = HARDCODED_CLIENT_VERSION;
} }
if (!keyAndVersionExtracted) extractClientVersionAndKey(); extractClientVersionAndKey();
return clientVersion; return clientVersion;
} }
@ -430,9 +429,11 @@ public class YoutubeParsingHelper {
*/ */
public static String getKey() throws IOException, ExtractionException { public static String getKey() throws IOException, ExtractionException {
if (!isNullOrEmpty(key)) return key; if (!isNullOrEmpty(key)) return key;
if (areHardcodedClientVersionAndKeyValid()) return key = HARDCODED_KEY; if (areHardcodedClientVersionAndKeyValid()) {
return key = HARDCODED_KEY;
}
if (!keyAndVersionExtracted) extractClientVersionAndKey(); extractClientVersionAndKey();
return key; return key;
} }
@ -465,7 +466,7 @@ public class YoutubeParsingHelper {
numberGenerator = random; numberGenerator = random;
} }
public static boolean areHardcodedYoutubeMusicKeysValid() throws IOException, public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
ReCaptchaException { ReCaptchaException {
final String url = final String url =
"https://music.youtube.com/youtubei/v1/music/get_search_suggestions?alt=json&key=" "https://music.youtube.com/youtubei/v1/music/get_search_suggestions?alt=json&key="
@ -510,17 +511,15 @@ public class YoutubeParsingHelper {
headers.put("Content-Type", Collections.singletonList("application/json")); headers.put("Content-Type", Collections.singletonList("application/json"));
final Response response = getDownloader().post(url, headers, json); final Response response = getDownloader().post(url, headers, json);
final String responseBody = response.responseBody();
final int responseCode = response.responseCode();
// Ensure to have a valid response // Ensure to have a valid response
return responseBody.length() > 500 && responseCode == 200; return response.responseBody().length() > 500 && response.responseCode() == 200;
} }
public static String[] getYoutubeMusicKeys() throws IOException, ReCaptchaException, public static String[] getYoutubeMusicKey() throws IOException, ReCaptchaException,
Parser.RegexException { Parser.RegexException {
if (youtubeMusicKeys != null && youtubeMusicKeys.length == 3) return youtubeMusicKeys; if (youtubeMusicKey != null && youtubeMusicKey.length == 3) return youtubeMusicKey;
if (areHardcodedYoutubeMusicKeysValid()) { if (isHardcodedYoutubeMusicKeyValid()) {
return youtubeMusicKeys = HARDCODED_YOUTUBE_MUSIC_KEY; return youtubeMusicKey = HARDCODED_YOUTUBE_MUSIC_KEY;
} }
final String url = "https://music.youtube.com/"; final String url = "https://music.youtube.com/";
@ -552,7 +551,7 @@ public class YoutubeParsingHelper {
} }
} }
return youtubeMusicKeys = new String[]{key, clientName, clientVersion}; return youtubeMusicKey = new String[]{key, clientName, clientVersion};
} }
@Nullable @Nullable

View File

@ -40,7 +40,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
@Override @Override
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException, public void onFetchPage(@Nonnull final Downloader downloader) throws IOException,
ExtractionException { ExtractionException {
final String[] youtubeMusicKeys = YoutubeParsingHelper.getYoutubeMusicKeys(); final String[] youtubeMusicKeys = YoutubeParsingHelper.getYoutubeMusicKey();
final String url = "https://music.youtube.com/youtubei/v1/search?alt=json&key=" + youtubeMusicKeys[0]; final String url = "https://music.youtube.com/youtubei/v1/search?alt=json&key=" + youtubeMusicKeys[0];
@ -184,15 +184,15 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
} }
@Override @Override
public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException, public InfoItemsPage<InfoItem> getPage(final Page page)
ExtractionException { throws IOException, ExtractionException {
if (page == null || isNullOrEmpty(page.getUrl())) { if (page == null || isNullOrEmpty(page.getUrl())) {
throw new IllegalArgumentException("Page doesn't contain an URL"); throw new IllegalArgumentException("Page doesn't contain an URL");
} }
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId()); final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
final String[] youtubeMusicKeys = YoutubeParsingHelper.getYoutubeMusicKeys(); final String[] youtubeMusicKeys = YoutubeParsingHelper.getYoutubeMusicKey();
// @formatter:off // @formatter:off
byte[] json = JsonWriter.string() byte[] json = JsonWriter.string()
@ -540,6 +540,6 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
return new Page("https://music.youtube.com/youtubei/v1/search?ctoken=" + continuation return new Page("https://music.youtube.com/youtubei/v1/search?ctoken=" + continuation
+ "&continuation=" + continuation + "&itct=" + clickTrackingParams + "&alt=json" + "&continuation=" + continuation + "&itct=" + clickTrackingParams + "&alt=json"
+ "&key=" + YoutubeParsingHelper.getYoutubeMusicKeys()[0]); + "&key=" + YoutubeParsingHelper.getYoutubeMusicKey()[0]);
} }
} }

View File

@ -33,7 +33,7 @@ public class YoutubeParsingHelperTest {
@Test @Test
public void testAreHardcodedYoutubeMusicKeysValid() throws IOException, ExtractionException { public void testAreHardcodedYoutubeMusicKeysValid() throws IOException, ExtractionException {
assertTrue("Hardcoded YouTube Music keys are not valid anymore", assertTrue("Hardcoded YouTube Music keys are not valid anymore",
YoutubeParsingHelper.areHardcodedYoutubeMusicKeysValid()); YoutubeParsingHelper.isHardcodedYoutubeMusicKeyValid());
} }
@Test @Test