Skip to content

Commit 0a5ad90

Browse files
authored
Merge pull request TeamNewPipe#901 from AudricV/remove-deprecated-code-yt-throttling-decrypter
[YouTube] Remove deprecated throttling decryption code
2 parents 76aad92 + 03d9a4f commit 0a5ad90

File tree

2 files changed

+18
-55
lines changed

2 files changed

+18
-55
lines changed

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

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,18 @@
3333
* </p>
3434
*
3535
*/
36-
public class YoutubeThrottlingDecrypter {
36+
public final class YoutubeThrottlingDecrypter {
3737

3838
private static final Pattern N_PARAM_PATTERN = Pattern.compile("[&?]n=([^&]+)");
39-
private static final Pattern FUNCTION_NAME_PATTERN = Pattern.compile(
39+
private static final Pattern DECRYPT_FUNCTION_NAME_PATTERN = Pattern.compile(
4040
"\\.get\\(\"n\"\\)\\)&&\\(b=([a-zA-Z0-9$]+)(?:\\[(\\d+)])?\\([a-zA-Z0-9]\\)");
4141

4242
private static final Map<String, String> N_PARAMS_CACHE = new HashMap<>();
43-
@SuppressWarnings("StaticVariableName") private static String FUNCTION;
44-
@SuppressWarnings("StaticVariableName") private static String FUNCTION_NAME;
43+
private static String decryptFunction;
44+
private static String decryptFunctionName;
4545

46-
private final String functionName;
47-
private final String function;
48-
49-
/**
50-
* <p>
51-
* Use this if you care about the off chance that YouTube tracks with which videoId the cipher
52-
* is requested.
53-
* </p>
54-
* Otherwise use the no-arg constructor which uses a constant value.
55-
*
56-
* @deprecated Use static function instead
57-
*/
58-
public YoutubeThrottlingDecrypter(final String videoId) throws ParsingException {
59-
final String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode(videoId);
60-
61-
functionName = parseDecodeFunctionName(playerJsCode);
62-
function = parseDecodeFunction(playerJsCode, functionName);
63-
}
64-
65-
/**
66-
* @deprecated Use static function instead
67-
*/
68-
public YoutubeThrottlingDecrypter() throws ParsingException {
69-
final String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode();
70-
71-
functionName = parseDecodeFunctionName(playerJsCode);
72-
function = parseDecodeFunction(playerJsCode, functionName);
46+
private YoutubeThrottlingDecrypter() {
47+
// No implementation
7348
}
7449

7550
/**
@@ -91,7 +66,7 @@ public YoutubeThrottlingDecrypter() throws ParsingException {
9166
* function. It can be a constant value of any existing video, but a
9267
* constant value is discouraged, because it could allow tracking.
9368
* @return A streaming URL with the decrypted parameter or the streaming URL itself if no
94-
* throttling parameter has been found
69+
* throttling parameter has been found.
9570
* @throws ParsingException If the streaming URL contains a throttling parameter and its
9671
* decryption failed
9772
*/
@@ -102,16 +77,16 @@ public static String apply(@Nonnull final String streamingUrl,
10277
}
10378

10479
try {
105-
if (FUNCTION == null) {
80+
if (decryptFunction == null) {
10681
final String playerJsCode
10782
= YoutubeJavaScriptExtractor.extractJavaScriptCode(videoId);
10883

109-
FUNCTION_NAME = parseDecodeFunctionName(playerJsCode);
110-
FUNCTION = parseDecodeFunction(playerJsCode, FUNCTION_NAME);
84+
decryptFunctionName = parseDecodeFunctionName(playerJsCode);
85+
decryptFunction = parseDecodeFunction(playerJsCode, decryptFunctionName);
11186
}
11287

11388
final String oldNParam = parseNParam(streamingUrl);
114-
final String newNParam = decryptNParam(FUNCTION, FUNCTION_NAME, oldNParam);
89+
final String newNParam = decryptNParam(decryptFunction, decryptFunctionName, oldNParam);
11590
return replaceNParam(streamingUrl, oldNParam, newNParam);
11691
} catch (final Exception e) {
11792
throw new ParsingException("Could not parse, decrypt or replace n parameter", e);
@@ -120,11 +95,10 @@ public static String apply(@Nonnull final String streamingUrl,
12095

12196
private static String parseDecodeFunctionName(final String playerJsCode)
12297
throws Parser.RegexException {
123-
final Matcher matcher = FUNCTION_NAME_PATTERN.matcher(playerJsCode);
124-
final boolean foundMatch = matcher.find();
125-
if (!foundMatch) {
98+
final Matcher matcher = DECRYPT_FUNCTION_NAME_PATTERN.matcher(playerJsCode);
99+
if (!matcher.find()) {
126100
throw new Parser.RegexException("Failed to find pattern \""
127-
+ FUNCTION_NAME_PATTERN + "\"");
101+
+ DECRYPT_FUNCTION_NAME_PATTERN + "\"");
128102
}
129103

130104
final String functionName = matcher.group(1);
@@ -166,17 +140,6 @@ private static String parseWithRegex(final String playerJsCode, final String fun
166140
return "function " + functionName + Parser.matchGroup1(functionPattern, playerJsCode);
167141
}
168142

169-
@Deprecated
170-
public String apply(final String url) throws ParsingException {
171-
if (containsNParam(url)) {
172-
final String oldNParam = parseNParam(url);
173-
final String newNParam = decryptNParam(function, functionName, oldNParam);
174-
return replaceNParam(url, oldNParam, newNParam);
175-
} else {
176-
return url;
177-
}
178-
}
179-
180143
private static boolean containsNParam(final String url) {
181144
return Parser.isMatch(N_PARAM_PATTERN, url);
182145
}
@@ -204,14 +167,14 @@ private static String replaceNParam(@Nonnull final String url,
204167
}
205168

206169
/**
207-
* @return the number of the cached "n" query parameters.
170+
* @return The number of the cached {@code n} query parameters.
208171
*/
209172
public static int getCacheSize() {
210173
return N_PARAMS_CACHE.size();
211174
}
212175

213176
/**
214-
* Clears all stored "n" query parameters.
177+
* Clears all stored {@code n} query parameters.
215178
*/
216179
public static void clearCache() {
217180
N_PARAMS_CACHE.clear();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,8 @@ public List<VideoStream> getVideoOnlyStreams() throws ExtractionException {
602602
}
603603

604604
/**
605-
* Try to decrypt a streaming URL and fallback to the given URL, because decryption may fail if
606-
* YouTube do breaking changes.
605+
* Try to decrypt a streaming URL and fall back to the given URL, because decryption may fail
606+
* if YouTube changes break something.
607607
*
608608
* <p>
609609
* This way a breaking change from YouTube does not result in a broken extractor.

0 commit comments

Comments
 (0)