Skip to content

Commit 3205514

Browse files
committed
Do some code improvements
Use final where possible, annotate some methods and parameters as Nonnull and format new code to be in the 100 characters limit per line.
1 parent 1c30a27 commit 3205514

4 files changed

Lines changed: 51 additions & 32 deletions

File tree

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
import javax.annotation.Nonnull;
1313

1414
/**
15-
* YouTube restricts streaming their media in multiple ways by requiring clients to apply a cipher function
16-
* on parameters of requests.
15+
* YouTube restricts streaming their media in multiple ways by requiring clients to apply a cipher
16+
* function on parameters of requests.
1717
* The cipher function is sent alongside as a JavaScript function.
1818
* <p>
19-
* This class handling fetching the JavaScript file in order to allow other classes to extract the needed functions.
19+
* This class handling fetching the JavaScript file in order to allow other classes to extract the
20+
* needed functions.
2021
*/
2122
public class YoutubeJavaScriptExtractor {
2223

@@ -27,14 +28,16 @@ private YoutubeJavaScriptExtractor() {
2728
}
2829

2930
/**
30-
* Extracts the JavaScript file. The result is cached, so subsequent calls use the result of previous calls.
31+
* Extracts the JavaScript file. The result is cached, so subsequent calls use the result of
32+
* previous calls.
3133
*
32-
* @param videoId Does not influence the result, but a valid video id may help in the chance that YouTube tracks it.
33-
* @return The whole javascript file as a string.
34+
* @param videoId Does not influence the result, but a valid video id may help in the chance
35+
* that YouTube tracks it.
36+
* @return The whole JavaScript file as a string.
3437
* @throws ParsingException If the extraction failed.
3538
*/
3639
@Nonnull
37-
public static String extractJavaScriptCode(String videoId) throws ParsingException {
40+
public static String extractJavaScriptCode(final String videoId) throws ParsingException {
3841
if (cachedJavaScriptCode == null) {
3942
final String playerJsUrl = YoutubeJavaScriptExtractor.cleanJavaScriptUrl(
4043
YoutubeJavaScriptExtractor.extractJavaScriptUrl(videoId));
@@ -45,18 +48,19 @@ public static String extractJavaScriptCode(String videoId) throws ParsingExcepti
4548
}
4649

4750
/**
48-
* Same as {@link YoutubeJavaScriptExtractor#extractJavaScriptCode(String)} but with a constant value for videoId.
51+
* Same as {@link YoutubeJavaScriptExtractor#extractJavaScriptCode(String)} but with a constant
52+
* value for videoId.
4953
* Possible because the videoId has no influence on the result.
5054
* <p>
51-
* In the off chance that YouTube tracks with which video id the request is made, it may make sense to pass in
52-
* video ids.
55+
* In the off chance that YouTube tracks with which video id the request is made, it may make
56+
* sense to pass in video ids.
5357
*/
5458
@Nonnull
5559
public static String extractJavaScriptCode() throws ParsingException {
5660
return extractJavaScriptCode("d4IGg5dqeO8");
5761
}
5862

59-
private static String extractJavaScriptUrl(String videoId) throws ParsingException {
63+
private static String extractJavaScriptUrl(final String videoId) throws ParsingException {
6064
try {
6165
final String embedUrl = "https://www.youtube.com/embed/" + videoId;
6266
final String embedPageContent = NewPipe.getDownloader()
@@ -84,7 +88,8 @@ private static String extractJavaScriptUrl(String videoId) throws ParsingExcepti
8488
throw new ParsingException("Embedded info did not provide YouTube player js url");
8589
}
8690

87-
private static String cleanJavaScriptUrl(String playerJsUrl) {
91+
@Nonnull
92+
private static String cleanJavaScriptUrl(@Nonnull final String playerJsUrl) {
8893
if (playerJsUrl.startsWith("//")) {
8994
return HTTPS + playerJsUrl;
9095
} else if (playerJsUrl.startsWith("/")) {
@@ -95,10 +100,12 @@ private static String cleanJavaScriptUrl(String playerJsUrl) {
95100
}
96101
}
97102

98-
private static String downloadJavaScriptCode(String playerJsUrl) throws ParsingException {
103+
@Nonnull
104+
private static String downloadJavaScriptCode(final String playerJsUrl)
105+
throws ParsingException {
99106
try {
100107
return NewPipe.getDownloader().get(playerJsUrl, Localization.DEFAULT).responseBody();
101-
} catch (Exception e) {
108+
} catch (final Exception e) {
102109
throw new ParsingException("Could not get player js code from url: " + playerJsUrl);
103110
}
104111
}

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.schabi.newpipe.extractor.utils.JavaScript;
55
import org.schabi.newpipe.extractor.utils.Parser;
66

7+
import javax.annotation.Nonnull;
78
import java.util.HashMap;
89
import java.util.Map;
910
import java.util.regex.Pattern;
@@ -33,11 +34,12 @@ public class YoutubeThrottlingDecrypter {
3334

3435
/**
3536
* <p>
36-
* Use this if you care about the off chance that YouTube tracks with which videoId the cipher is requested.
37+
* Use this if you care about the off chance that YouTube tracks with which videoId the cipher
38+
* is requested.
3739
* </p>
3840
* Otherwise use the no-arg constructor which uses a constant value.
3941
*/
40-
public YoutubeThrottlingDecrypter(String videoId) throws ParsingException {
42+
public YoutubeThrottlingDecrypter(final String videoId) throws ParsingException {
4143
final String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode(videoId);
4244

4345
functionName = parseDecodeFunctionName(playerJsCode);
@@ -51,17 +53,22 @@ public YoutubeThrottlingDecrypter() throws ParsingException {
5153
function = parseDecodeFunction(playerJsCode, functionName);
5254
}
5355

54-
private String parseDecodeFunctionName(String playerJsCode) throws Parser.RegexException {
55-
Pattern pattern = Pattern.compile("b=a\\.get\\(\"n\"\\)\\)&&\\(b=(\\w+)\\(b\\),a\\.set\\(\"n\",b\\)");
56+
private String parseDecodeFunctionName(final String playerJsCode)
57+
throws Parser.RegexException {
58+
Pattern pattern = Pattern.compile(
59+
"b=a\\.get\\(\"n\"\\)\\)&&\\(b=(\\w+)\\(b\\),a\\.set\\(\"n\",b\\)");
5660
return Parser.matchGroup1(pattern, playerJsCode);
5761
}
5862

59-
private String parseDecodeFunction(String playerJsCode, String functionName) throws Parser.RegexException {
60-
Pattern functionPattern = Pattern.compile(functionName + "=function(.*?;)\n", Pattern.DOTALL);
61-
return "function " + functionName + Parser.matchGroup1(functionPattern, playerJsCode);
63+
@Nonnull
64+
private String parseDecodeFunction(final String playerJsCode, final String functionName)
65+
throws Parser.RegexException {
66+
Pattern functionPattern = Pattern.compile(functionName + "=function(.*?;)\n",
67+
Pattern.DOTALL);
68+
return "function " + functionName + Parser.matchGroup1(functionPattern, playerJsCode);
6269
}
6370

64-
public String apply(String url) throws Parser.RegexException {
71+
public String apply(final String url) throws Parser.RegexException {
6572
if (containsNParam(url)) {
6673
String oldNParam = parseNParam(url);
6774
String newNParam = decryptNParam(oldNParam);
@@ -71,16 +78,16 @@ public String apply(String url) throws Parser.RegexException {
7178
}
7279
}
7380

74-
private boolean containsNParam(String url) {
81+
private boolean containsNParam(final String url) {
7582
return Parser.isMatch(N_PARAM_REGEX, url);
7683
}
7784

78-
private String parseNParam(String url) throws Parser.RegexException {
85+
private String parseNParam(final String url) throws Parser.RegexException {
7986
Pattern nValuePattern = Pattern.compile(N_PARAM_REGEX);
8087
return Parser.matchGroup1(nValuePattern, url);
8188
}
8289

83-
private String decryptNParam(String nParam) {
90+
private String decryptNParam(final String nParam) {
8491
if (nParams.containsKey(nParam)) {
8592
return nParams.get(nParam);
8693
}
@@ -89,7 +96,10 @@ private String decryptNParam(String nParam) {
8996
return decryptedNParam;
9097
}
9198

92-
private String replaceNParam(String url, String oldValue, String newValue) {
99+
@Nonnull
100+
private String replaceNParam(@Nonnull final String url,
101+
final String oldValue,
102+
final String newValue) {
93103
return url.replace(oldValue, newValue);
94104
}
95105
}

extractor/src/main/java/org/schabi/newpipe/extractor/utils/JavaScript.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ public class JavaScript {
99
private JavaScript() {
1010
}
1111

12-
public static String run(String function, String functionName, String... parameters) {
12+
public static String run(final String function,
13+
final String functionName,
14+
final String... parameters) {
1315
try {
14-
Context context = Context.enter();
16+
final Context context = Context.enter();
1517
context.setOptimizationLevel(-1);
16-
ScriptableObject scope = context.initSafeStandardObjects();
18+
final ScriptableObject scope = context.initSafeStandardObjects();
1719

1820
context.evaluateString(scope, function, functionName, 1, null);
19-
Function jsFunction = (Function) scope.get(functionName, scope);
20-
Object result = jsFunction.call(context, scope, scope, parameters);
21+
final Function jsFunction = (Function) scope.get(functionName, scope);
22+
final Object result = jsFunction.call(context, scope, scope, parameters);
2123
return result.toString();
2224
} finally {
2325
Context.exit();

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeJavaScriptExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void testExtractJavaScript__invalidVideoId__success() throws ParsingExcep
3838

3939
}
4040

41-
private void assertPlayerJsCode(String playerJsCode) {
41+
private void assertPlayerJsCode(final String playerJsCode) {
4242
assertThat(playerJsCode, allOf(
4343
containsString(" Copyright The Closure Library Authors.\n"
4444
+ " SPDX-License-Identifier: Apache-2.0"),

0 commit comments

Comments
 (0)