From cb64a480cd4302dc2e244430d5b2a4967391240f Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Tue, 16 Aug 2022 15:01:05 +0200 Subject: [PATCH] [YouTube] Remove deprecated code in YoutubeThrottlingDecrypter YoutubeThrottlingDecrypter is now a non-instantiable final class and non-static attributes have been removed. The static attributes of this class have been renamed, in order to respect the naming specification used. --- .../youtube/YoutubeThrottlingDecrypter.java | 69 +++++-------------- 1 file changed, 16 insertions(+), 53 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingDecrypter.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingDecrypter.java index cb807dc17..1b0ea486a 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingDecrypter.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingDecrypter.java @@ -33,43 +33,18 @@ import java.util.regex.Pattern; *

* */ -public class YoutubeThrottlingDecrypter { +public final class YoutubeThrottlingDecrypter { private static final Pattern N_PARAM_PATTERN = Pattern.compile("[&?]n=([^&]+)"); - private static final Pattern FUNCTION_NAME_PATTERN = Pattern.compile( + private static final Pattern DECRYPT_FUNCTION_NAME_PATTERN = Pattern.compile( "\\.get\\(\"n\"\\)\\)&&\\(b=([a-zA-Z0-9$]+)(?:\\[(\\d+)])?\\([a-zA-Z0-9]\\)"); private static final Map N_PARAMS_CACHE = new HashMap<>(); - @SuppressWarnings("StaticVariableName") private static String FUNCTION; - @SuppressWarnings("StaticVariableName") private static String FUNCTION_NAME; + private static String decryptFunction; + private static String decryptFunctionName; - private final String functionName; - private final String function; - - /** - *

- * Use this if you care about the off chance that YouTube tracks with which videoId the cipher - * is requested. - *

- * Otherwise use the no-arg constructor which uses a constant value. - * - * @deprecated Use static function instead - */ - public YoutubeThrottlingDecrypter(final String videoId) throws ParsingException { - final String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode(videoId); - - functionName = parseDecodeFunctionName(playerJsCode); - function = parseDecodeFunction(playerJsCode, functionName); - } - - /** - * @deprecated Use static function instead - */ - public YoutubeThrottlingDecrypter() throws ParsingException { - final String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode(); - - functionName = parseDecodeFunctionName(playerJsCode); - function = parseDecodeFunction(playerJsCode, functionName); + private YoutubeThrottlingDecrypter() { + // No implementation } /** @@ -91,7 +66,7 @@ public class YoutubeThrottlingDecrypter { * function. It can be a constant value of any existing video, but a * constant value is discouraged, because it could allow tracking. * @return A streaming URL with the decrypted parameter or the streaming URL itself if no - * throttling parameter has been found + * throttling parameter has been found. * @throws ParsingException If the streaming URL contains a throttling parameter and its * decryption failed */ @@ -102,16 +77,16 @@ public class YoutubeThrottlingDecrypter { } try { - if (FUNCTION == null) { + if (decryptFunction == null) { final String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode(videoId); - FUNCTION_NAME = parseDecodeFunctionName(playerJsCode); - FUNCTION = parseDecodeFunction(playerJsCode, FUNCTION_NAME); + decryptFunctionName = parseDecodeFunctionName(playerJsCode); + decryptFunction = parseDecodeFunction(playerJsCode, decryptFunctionName); } final String oldNParam = parseNParam(streamingUrl); - final String newNParam = decryptNParam(FUNCTION, FUNCTION_NAME, oldNParam); + final String newNParam = decryptNParam(decryptFunction, decryptFunctionName, oldNParam); return replaceNParam(streamingUrl, oldNParam, newNParam); } catch (final Exception e) { throw new ParsingException("Could not parse, decrypt or replace n parameter", e); @@ -120,11 +95,10 @@ public class YoutubeThrottlingDecrypter { private static String parseDecodeFunctionName(final String playerJsCode) throws Parser.RegexException { - final Matcher matcher = FUNCTION_NAME_PATTERN.matcher(playerJsCode); - final boolean foundMatch = matcher.find(); - if (!foundMatch) { + final Matcher matcher = DECRYPT_FUNCTION_NAME_PATTERN.matcher(playerJsCode); + if (!matcher.find()) { throw new Parser.RegexException("Failed to find pattern \"" - + FUNCTION_NAME_PATTERN + "\""); + + DECRYPT_FUNCTION_NAME_PATTERN + "\""); } final String functionName = matcher.group(1); @@ -166,17 +140,6 @@ public class YoutubeThrottlingDecrypter { return "function " + functionName + Parser.matchGroup1(functionPattern, playerJsCode); } - @Deprecated - public String apply(final String url) throws ParsingException { - if (containsNParam(url)) { - final String oldNParam = parseNParam(url); - final String newNParam = decryptNParam(function, functionName, oldNParam); - return replaceNParam(url, oldNParam, newNParam); - } else { - return url; - } - } - private static boolean containsNParam(final String url) { return Parser.isMatch(N_PARAM_PATTERN, url); } @@ -204,14 +167,14 @@ public class YoutubeThrottlingDecrypter { } /** - * @return the number of the cached "n" query parameters. + * @return The number of the cached {@code n} query parameters. */ public static int getCacheSize() { return N_PARAMS_CACHE.size(); } /** - * Clears all stored "n" query parameters. + * Clears all stored {@code n} query parameters. */ public static void clearCache() { N_PARAMS_CACHE.clear();