|
| 1 | +package org.schabi.newpipe.extractor.services.youtube; |
| 2 | + |
| 3 | +import org.schabi.newpipe.extractor.exceptions.ParsingException; |
| 4 | +import org.schabi.newpipe.extractor.utils.JavaScript; |
| 5 | +import org.schabi.newpipe.extractor.utils.Parser; |
| 6 | + |
| 7 | +import javax.annotation.Nonnull; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.regex.Pattern; |
| 11 | + |
| 12 | +/** |
| 13 | + * <p> |
| 14 | + * YouTube's media is protected with a cipher, |
| 15 | + * which modifies the "n" query parameter of it's video playback urls. |
| 16 | + * This class handles extracting that "n" query parameter, |
| 17 | + * applying the cipher on it and returning the resulting url which is not throttled. |
| 18 | + * </p> |
| 19 | + * |
| 20 | + * <p> |
| 21 | + * https://r5---sn-4g5ednsz.googlevideo.com/videoplayback?n=VVF2xyZLVRZZxHXZ&other=other |
| 22 | + * </p> |
| 23 | + * becomes |
| 24 | + * <p> |
| 25 | + * https://r5---sn-4g5ednsz.googlevideo.com/videoplayback?n=iHywZkMipkszqA&other=other |
| 26 | + * </p> |
| 27 | + * <br> |
| 28 | + * <p> |
| 29 | + * Decoding the "n" parameter is time intensive. For this reason, the results are cached. |
| 30 | + * The cache can be cleared using {@link #clearCache()} |
| 31 | + * </p> |
| 32 | + * |
| 33 | + */ |
| 34 | +public class YoutubeThrottlingDecrypter { |
| 35 | + |
| 36 | + private static final String N_PARAM_REGEX = "[&?]n=([^&]+)"; |
| 37 | + private static final Map<String, String> nParams = new HashMap<>(); |
| 38 | + |
| 39 | + private final String functionName; |
| 40 | + private final String function; |
| 41 | + |
| 42 | + /** |
| 43 | + * <p> |
| 44 | + * Use this if you care about the off chance that YouTube tracks with which videoId the cipher |
| 45 | + * is requested. |
| 46 | + * </p> |
| 47 | + * Otherwise use the no-arg constructor which uses a constant value. |
| 48 | + */ |
| 49 | + public YoutubeThrottlingDecrypter(final String videoId) throws ParsingException { |
| 50 | + final String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode(videoId); |
| 51 | + |
| 52 | + functionName = parseDecodeFunctionName(playerJsCode); |
| 53 | + function = parseDecodeFunction(playerJsCode, functionName); |
| 54 | + } |
| 55 | + |
| 56 | + public YoutubeThrottlingDecrypter() throws ParsingException { |
| 57 | + final String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode(); |
| 58 | + |
| 59 | + functionName = parseDecodeFunctionName(playerJsCode); |
| 60 | + function = parseDecodeFunction(playerJsCode, functionName); |
| 61 | + } |
| 62 | + |
| 63 | + private String parseDecodeFunctionName(final String playerJsCode) |
| 64 | + throws Parser.RegexException { |
| 65 | + Pattern pattern = Pattern.compile( |
| 66 | + "b=a\\.get\\(\"n\"\\)\\)&&\\(b=(\\w+)\\(b\\),a\\.set\\(\"n\",b\\)"); |
| 67 | + return Parser.matchGroup1(pattern, playerJsCode); |
| 68 | + } |
| 69 | + |
| 70 | + @Nonnull |
| 71 | + private String parseDecodeFunction(final String playerJsCode, final String functionName) |
| 72 | + throws Parser.RegexException { |
| 73 | + Pattern functionPattern = Pattern.compile(functionName + "=function(.*?;)\n", |
| 74 | + Pattern.DOTALL); |
| 75 | + return "function " + functionName + Parser.matchGroup1(functionPattern, playerJsCode); |
| 76 | + } |
| 77 | + |
| 78 | + public String apply(final String url) throws Parser.RegexException { |
| 79 | + if (containsNParam(url)) { |
| 80 | + String oldNParam = parseNParam(url); |
| 81 | + String newNParam = decryptNParam(oldNParam); |
| 82 | + return replaceNParam(url, oldNParam, newNParam); |
| 83 | + } else { |
| 84 | + return url; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private boolean containsNParam(final String url) { |
| 89 | + return Parser.isMatch(N_PARAM_REGEX, url); |
| 90 | + } |
| 91 | + |
| 92 | + private String parseNParam(final String url) throws Parser.RegexException { |
| 93 | + Pattern nValuePattern = Pattern.compile(N_PARAM_REGEX); |
| 94 | + return Parser.matchGroup1(nValuePattern, url); |
| 95 | + } |
| 96 | + |
| 97 | + private String decryptNParam(final String nParam) { |
| 98 | + if (nParams.containsKey(nParam)) { |
| 99 | + return nParams.get(nParam); |
| 100 | + } |
| 101 | + final String decryptedNParam = JavaScript.run(function, functionName, nParam); |
| 102 | + nParams.put(nParam, decryptedNParam); |
| 103 | + return decryptedNParam; |
| 104 | + } |
| 105 | + |
| 106 | + @Nonnull |
| 107 | + private String replaceNParam(@Nonnull final String url, |
| 108 | + final String oldValue, |
| 109 | + final String newValue) { |
| 110 | + return url.replace(oldValue, newValue); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return the number of the cached "n" query parameters. |
| 115 | + */ |
| 116 | + public static int getCacheSize() { |
| 117 | + return nParams.size(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Clears all stored "n" query parameters. |
| 122 | + */ |
| 123 | + public static void clearCache() { |
| 124 | + nParams.clear(); |
| 125 | + } |
| 126 | +} |
0 commit comments