Skip to content

Commit a683c8d

Browse files
committed
Delete duplicated code to load youtube javascript file
1 parent a02ee2e commit a683c8d

1 file changed

Lines changed: 4 additions & 47 deletions

File tree

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

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
2525
import org.schabi.newpipe.extractor.localization.TimeAgoPatternsManager;
2626
import org.schabi.newpipe.extractor.services.youtube.ItagItem;
27+
import org.schabi.newpipe.extractor.services.youtube.YoutubeJavascriptExtractor;
2728
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
2829
import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter;
2930
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
@@ -799,45 +800,6 @@ private void fetchVideoInfoPage() throws ParsingException, ReCaptchaException, I
799800
}
800801
}
801802

802-
private String extractPlayerJsUrl() throws ParsingException {
803-
try {
804-
final String embedUrl = "https://www.youtube.com/embed/" + getId();
805-
final String embedPageContent = NewPipe.getDownloader()
806-
.get(embedUrl, getExtractorLocalization()).responseBody();
807-
808-
try {
809-
final String assetsPattern = "\"assets\":.+?\"js\":\\s*(\"[^\"]+\")";
810-
return Parser.matchGroup1(assetsPattern, embedPageContent)
811-
.replace("\\", "").replace("\"", "");
812-
} catch (final Parser.RegexException ex) {
813-
// playerJsUrl is still available in the file, just somewhere else TODO
814-
// it is ok not to find it, see how that's handled in getDeobfuscationCode()
815-
final Document doc = Jsoup.parse(embedPageContent);
816-
final Elements elems = doc.select("script").attr("name", "player_ias/base");
817-
for (final Element elem : elems) {
818-
if (elem.attr("src").contains("base.js")) {
819-
return elem.attr("src");
820-
}
821-
}
822-
}
823-
824-
} catch (final Exception i) {
825-
throw new ParsingException("Embedded info did not provide YouTube player js url");
826-
}
827-
throw new ParsingException("Embedded info did not provide YouTube player js url");
828-
}
829-
830-
private String cleanPlayerJsUrl(String playerJsUrl) {
831-
if (playerJsUrl.startsWith("//")) {
832-
return HTTPS + playerJsUrl;
833-
} else if (playerJsUrl.startsWith("/")) {
834-
// sometimes https://www.youtube.com part has to be added manually
835-
return HTTPS + "//www.youtube.com" + playerJsUrl;
836-
} else {
837-
return playerJsUrl;
838-
}
839-
}
840-
841803
private String getDeobfuscationFuncName(final String playerCode) throws DeobfuscateException {
842804
Parser.RegexException exception = null;
843805
for (final String regex : REGEXES) {
@@ -852,11 +814,10 @@ private String getDeobfuscationFuncName(final String playerCode) throws Deobfusc
852814
throw new DeobfuscateException("Could not find deobfuscate function with any of the given patterns.", exception);
853815
}
854816

855-
private String loadDeobfuscationCode(@Nonnull final String playerJsUrl)
817+
private String loadDeobfuscationCode()
856818
throws DeobfuscateException {
857819
try {
858-
final String playerCode = NewPipe.getDownloader()
859-
.get(playerJsUrl, getExtractorLocalization()).responseBody();
820+
final String playerCode = YoutubeJavascriptExtractor.extractJavascriptCode(getId());
860821
final String deobfuscationFunctionName = getDeobfuscationFuncName(playerCode);
861822

862823
final String functionPattern = "("
@@ -875,8 +836,6 @@ private String loadDeobfuscationCode(@Nonnull final String playerJsUrl)
875836
"function " + DEOBFUSCATION_FUNC_NAME + "(a){return " + deobfuscationFunctionName + "(a);}";
876837

877838
return helperObject + deobfuscateFunction + callerFunction;
878-
} catch (final IOException ioe) {
879-
throw new DeobfuscateException("Could not load deobfuscate function", ioe);
880839
} catch (final Exception e) {
881840
throw new DeobfuscateException("Could not parse deobfuscate function ", e);
882841
}
@@ -885,9 +844,7 @@ private String loadDeobfuscationCode(@Nonnull final String playerJsUrl)
885844
@Nonnull
886845
private String getDeobfuscationCode() throws ParsingException {
887846
if (cachedDeobfuscationCode == null) {
888-
String playerJsUrl = cleanPlayerJsUrl(extractPlayerJsUrl());
889-
890-
cachedDeobfuscationCode = loadDeobfuscationCode(playerJsUrl);
847+
cachedDeobfuscationCode = loadDeobfuscationCode();
891848
}
892849
return cachedDeobfuscationCode;
893850
}

0 commit comments

Comments
 (0)