Skip to content

Commit 3a3d1d7

Browse files
committed
Make YoutubeJavaScriptExtractor and JavaScript methods static
Also address review and rewrite some comments
1 parent a683c8d commit 3a3d1d7

5 files changed

Lines changed: 41 additions & 39 deletions

File tree

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,51 @@
1212
import javax.annotation.Nonnull;
1313

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

2323
private static final String HTTPS = "https:";
24-
private static String cachedJavascriptCode;
24+
private static String cachedJavaScriptCode;
25+
26+
private YoutubeJavaScriptExtractor() {
27+
}
2528

2629
/**
2730
* Extracts the JavaScript file. The result is cached, so subsequent calls use the result of previous calls.
2831
*
29-
* @param videoId Does not influence the result, but a valid video id can prevent tracking
32+
* @param videoId Does not influence the result, but a valid video id may help in the chance that YouTube tracks it.
3033
* @return The whole javascript file as a string.
3134
* @throws ParsingException If the extraction failed.
3235
*/
3336
@Nonnull
34-
public static String extractJavascriptCode(String videoId) throws ParsingException {
35-
if (cachedJavascriptCode == null) {
36-
final YoutubeJavascriptExtractor extractor = new YoutubeJavascriptExtractor();
37-
String playerJsUrl = extractor.cleanJavascriptUrl(extractor.extractJavascriptUrl(videoId));
38-
cachedJavascriptCode = extractor.downloadJavascriptCode(playerJsUrl);
37+
public static String extractJavaScriptCode(String videoId) throws ParsingException {
38+
if (cachedJavaScriptCode == null) {
39+
final String playerJsUrl = YoutubeJavaScriptExtractor.cleanJavaScriptUrl(
40+
YoutubeJavaScriptExtractor.extractJavaScriptUrl(videoId));
41+
cachedJavaScriptCode = YoutubeJavaScriptExtractor.downloadJavaScriptCode(playerJsUrl);
3942
}
4043

41-
return cachedJavascriptCode;
44+
return cachedJavaScriptCode;
4245
}
4346

4447
/**
45-
* Same as {@link YoutubeJavascriptExtractor#extractJavascriptCode(String)} but with a constant value for videoId.
48+
* Same as {@link YoutubeJavaScriptExtractor#extractJavaScriptCode(String)} but with a constant value for videoId.
4649
* Possible because the videoId has no influence on the result.
47-
*
48-
* For tracking avoidance purposes it may make sense to pass in valid video ids.
50+
* <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.
4953
*/
5054
@Nonnull
51-
public static String extractJavascriptCode() throws ParsingException {
52-
return extractJavascriptCode("d4IGg5dqeO8");
55+
public static String extractJavaScriptCode() throws ParsingException {
56+
return extractJavaScriptCode("d4IGg5dqeO8");
5357
}
5458

55-
private String extractJavascriptUrl(String videoId) throws ParsingException {
59+
private static String extractJavaScriptUrl(String videoId) throws ParsingException {
5660
try {
5761
final String embedUrl = "https://www.youtube.com/embed/" + videoId;
5862
final String embedPageContent = NewPipe.getDownloader()
@@ -80,7 +84,7 @@ private String extractJavascriptUrl(String videoId) throws ParsingException {
8084
throw new ParsingException("Embedded info did not provide YouTube player js url");
8185
}
8286

83-
private String cleanJavascriptUrl(String playerJsUrl) {
87+
private static String cleanJavaScriptUrl(String playerJsUrl) {
8488
if (playerJsUrl.startsWith("//")) {
8589
return HTTPS + playerJsUrl;
8690
} else if (playerJsUrl.startsWith("/")) {
@@ -91,7 +95,7 @@ private String cleanJavascriptUrl(String playerJsUrl) {
9195
}
9296
}
9397

94-
private String downloadJavascriptCode(String playerJsUrl) throws ParsingException {
98+
private static String downloadJavaScriptCode(String playerJsUrl) throws ParsingException {
9599
try {
96100
return NewPipe.getDownloader().get(playerJsUrl, Localization.DEFAULT).responseBody();
97101
} catch (Exception e) {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.schabi.newpipe.extractor.services.youtube;
22

33
import org.schabi.newpipe.extractor.exceptions.ParsingException;
4-
import org.schabi.newpipe.extractor.utils.Javascript;
4+
import org.schabi.newpipe.extractor.utils.JavaScript;
55
import org.schabi.newpipe.extractor.utils.Parser;
66

77
import java.util.regex.Pattern;
@@ -35,14 +35,14 @@ public class YoutubeThrottlingDecrypter {
3535
* Otherwise use the no-arg constructor which uses a constant value.
3636
*/
3737
public YoutubeThrottlingDecrypter(String videoId) throws ParsingException {
38-
final String playerJsCode = YoutubeJavascriptExtractor.extractJavascriptCode(videoId);
38+
final String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode(videoId);
3939

4040
functionName = parseDecodeFunctionName(playerJsCode);
4141
function = parseDecodeFunction(playerJsCode, functionName);
4242
}
4343

4444
public YoutubeThrottlingDecrypter() throws ParsingException {
45-
final String playerJsCode = YoutubeJavascriptExtractor.extractJavascriptCode();
45+
final String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode();
4646

4747
functionName = parseDecodeFunctionName(playerJsCode);
4848
function = parseDecodeFunction(playerJsCode, functionName);
@@ -78,8 +78,7 @@ private String parseNParam(String url) throws Parser.RegexException {
7878
}
7979

8080
private String decryptNParam(String nParam) {
81-
Javascript javascript = new Javascript();
82-
return javascript.run(function, functionName, nParam);
81+
return JavaScript.run(function, functionName, nParam);
8382
}
8483

8584
private String replaceNParam(String url, String oldValue, String newValue) {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
import com.grack.nanojson.JsonObject;
55
import com.grack.nanojson.JsonParser;
66
import com.grack.nanojson.JsonParserException;
7-
import org.jsoup.Jsoup;
8-
import org.jsoup.nodes.Document;
9-
import org.jsoup.nodes.Element;
10-
import org.jsoup.select.Elements;
117
import org.mozilla.javascript.Context;
128
import org.mozilla.javascript.Function;
139
import org.mozilla.javascript.ScriptableObject;
@@ -24,7 +20,7 @@
2420
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
2521
import org.schabi.newpipe.extractor.localization.TimeAgoPatternsManager;
2622
import org.schabi.newpipe.extractor.services.youtube.ItagItem;
27-
import org.schabi.newpipe.extractor.services.youtube.YoutubeJavascriptExtractor;
23+
import org.schabi.newpipe.extractor.services.youtube.YoutubeJavaScriptExtractor;
2824
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
2925
import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter;
3026
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
@@ -524,7 +520,7 @@ public List<AudioStream> getAudioStreams() throws ExtractionException {
524520
public List<VideoStream> getVideoStreams() throws ExtractionException {
525521
assertPageFetched();
526522
final List<VideoStream> videoStreams = new ArrayList<>();
527-
YoutubeThrottlingDecrypter throttlingDecrypter = new YoutubeThrottlingDecrypter(getId());
523+
final YoutubeThrottlingDecrypter throttlingDecrypter = new YoutubeThrottlingDecrypter(getId());
528524

529525
try {
530526
for (final Map.Entry<String, ItagItem> entry : getItags(FORMATS, ItagItem.ItagType.VIDEO).entrySet()) {
@@ -817,7 +813,7 @@ private String getDeobfuscationFuncName(final String playerCode) throws Deobfusc
817813
private String loadDeobfuscationCode()
818814
throws DeobfuscateException {
819815
try {
820-
final String playerCode = YoutubeJavascriptExtractor.extractJavascriptCode(getId());
816+
final String playerCode = YoutubeJavaScriptExtractor.extractJavaScriptCode(getId());
821817
final String deobfuscationFunctionName = getDeobfuscationFuncName(playerCode);
822818

823819
final String functionPattern = "("

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Javascript.java renamed to extractor/src/main/java/org/schabi/newpipe/extractor/utils/JavaScript.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import org.mozilla.javascript.Function;
55
import org.mozilla.javascript.ScriptableObject;
66

7-
public class Javascript {
7+
public class JavaScript {
88

9-
public String run(String function, String functionName, String... parameters) {
9+
private JavaScript() {
10+
}
11+
12+
public static String run(String function, String functionName, String... parameters) {
1013
try {
1114
Context context = Context.enter();
1215
context.setOptimizationLevel(-1);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@
1212
import static org.hamcrest.CoreMatchers.containsString;
1313
import static org.hamcrest.MatcherAssert.assertThat;
1414

15-
public class YoutubeJavascriptExtractorTest {
15+
public class YoutubeJavaScriptExtractorTest {
1616

1717
@Before
1818
public void setup() throws IOException {
1919
NewPipe.init(DownloaderTestImpl.getInstance());
2020
}
2121

2222
@Test
23-
public void testExtractJavascript__success() throws ParsingException {
24-
String playerJsCode = YoutubeJavascriptExtractor.extractJavascriptCode("d4IGg5dqeO8");
23+
public void testExtractJavaScript__success() throws ParsingException {
24+
String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode("d4IGg5dqeO8");
2525
assertPlayerJsCode(playerJsCode);
2626

27-
playerJsCode = YoutubeJavascriptExtractor.extractJavascriptCode();
27+
playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode();
2828
assertPlayerJsCode(playerJsCode);
2929
}
3030

3131
@Test
32-
public void testExtractJavascript__invalidVideoId__success() throws ParsingException {
33-
String playerJsCode = YoutubeJavascriptExtractor.extractJavascriptCode("not_a_video_id");
32+
public void testExtractJavaScript__invalidVideoId__success() throws ParsingException {
33+
String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode("not_a_video_id");
3434
assertPlayerJsCode(playerJsCode);
3535

36-
playerJsCode = YoutubeJavascriptExtractor.extractJavascriptCode("11-chars123");
36+
playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode("11-chars123");
3737
assertPlayerJsCode(playerJsCode);
3838

3939
}

0 commit comments

Comments
 (0)