mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2025-04-27 23:40:36 +05:30
Faster iframe api based player extraction. (#694)
* Faster iframe api based player extraction. Uses the IFrame API to reduce the required download to less than 1/50 of the size. * Remove debug code. * Extract to two methods. * Add tests for player URL extraction. * Add assertThat for tests.
This commit is contained in:
parent
4b147863ec
commit
71b9fd0076
@ -39,8 +39,13 @@ public class YoutubeJavaScriptExtractor {
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
public static String extractJavaScriptCode(final String videoId) throws ParsingException {
|
public static String extractJavaScriptCode(final String videoId) throws ParsingException {
|
||||||
if (cachedJavaScriptCode == null) {
|
if (cachedJavaScriptCode == null) {
|
||||||
final String playerJsUrl = YoutubeJavaScriptExtractor.cleanJavaScriptUrl(
|
String url;
|
||||||
YoutubeJavaScriptExtractor.extractJavaScriptUrl(videoId));
|
try {
|
||||||
|
url = YoutubeJavaScriptExtractor.extractJavaScriptUrl();
|
||||||
|
} catch (final Exception i) {
|
||||||
|
url = YoutubeJavaScriptExtractor.extractJavaScriptUrl(videoId);
|
||||||
|
}
|
||||||
|
final String playerJsUrl = YoutubeJavaScriptExtractor.cleanJavaScriptUrl(url);
|
||||||
cachedJavaScriptCode = YoutubeJavaScriptExtractor.downloadJavaScriptCode(playerJsUrl);
|
cachedJavaScriptCode = YoutubeJavaScriptExtractor.downloadJavaScriptCode(playerJsUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,7 +73,22 @@ public class YoutubeJavaScriptExtractor {
|
|||||||
cachedJavaScriptCode = null;
|
cachedJavaScriptCode = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String extractJavaScriptUrl(final String videoId) throws ParsingException {
|
public static String extractJavaScriptUrl() throws ParsingException {
|
||||||
|
try {
|
||||||
|
final String iframeUrl = "https://www.youtube.com/iframe_api";
|
||||||
|
final String iframeContent = NewPipe.getDownloader()
|
||||||
|
.get(iframeUrl, Localization.DEFAULT).responseBody();
|
||||||
|
final String hashPattern = "player\\\\\\/([a-z0-9]{8})\\\\\\/";
|
||||||
|
final String hash = Parser.matchGroup1(hashPattern, iframeContent);
|
||||||
|
|
||||||
|
return String.format("https://www.youtube.com/s/player/%s/player_ias.vflset/en_US/base.js", hash);
|
||||||
|
|
||||||
|
} catch (final Exception i) { }
|
||||||
|
|
||||||
|
throw new ParsingException("Iframe API did not provide YouTube player js url");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String extractJavaScriptUrl(final String videoId) throws ParsingException {
|
||||||
try {
|
try {
|
||||||
final String embedUrl = "https://www.youtube.com/embed/" + videoId;
|
final String embedUrl = "https://www.youtube.com/embed/" + videoId;
|
||||||
final String embedPageContent = NewPipe.getDownloader()
|
final String embedPageContent = NewPipe.getDownloader()
|
||||||
@ -90,9 +110,8 @@ public class YoutubeJavaScriptExtractor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (final Exception i) {
|
} catch (final Exception i) { }
|
||||||
throw new ParsingException("Embedded info did not provide YouTube player js url");
|
|
||||||
}
|
|
||||||
throw new ParsingException("Embedded info did not provide YouTube player js url");
|
throw new ParsingException("Embedded info did not provide YouTube player js url");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,8 +8,7 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.allOf;
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
|
||||||
public class YoutubeJavaScriptExtractorTest {
|
public class YoutubeJavaScriptExtractorTest {
|
||||||
@ -19,6 +18,16 @@ public class YoutubeJavaScriptExtractorTest {
|
|||||||
NewPipe.init(DownloaderTestImpl.getInstance());
|
NewPipe.init(DownloaderTestImpl.getInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExtractJavaScriptUrlIframe() throws ParsingException {
|
||||||
|
assertThat(YoutubeJavaScriptExtractor.extractJavaScriptUrl(), endsWith("base.js"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExtractJavaScriptUrlEmbed() throws ParsingException {
|
||||||
|
assertThat(YoutubeJavaScriptExtractor.extractJavaScriptUrl("d4IGg5dqeO8"), endsWith("base.js"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExtractJavaScript__success() throws ParsingException {
|
public void testExtractJavaScript__success() throws ParsingException {
|
||||||
String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode("d4IGg5dqeO8");
|
String playerJsCode = YoutubeJavaScriptExtractor.extractJavaScriptCode("d4IGg5dqeO8");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user