44import org .schabi .newpipe .extractor .utils .JavaScript ;
55import org .schabi .newpipe .extractor .utils .Parser ;
66
7+ import javax .annotation .Nonnull ;
78import java .util .HashMap ;
89import java .util .Map ;
910import 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}
0 commit comments