1212import 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 ) {
0 commit comments