From f037f67a157efbc747aede1c8f1a4a27bec8eaaa Mon Sep 17 00:00:00 2001 From: Coffeemakr Date: Thu, 30 Nov 2017 10:49:27 +0100 Subject: [PATCH] Add checks if page is fetched --- .../youtube/YoutubeStreamExtractor.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java index 33c94972a..61465fbd3 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java @@ -105,6 +105,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Nonnull @Override public String getName() throws ParsingException { + assertPageFetched(); String name = getStringFromMetaData("title"); if(name == null) { // Fallback to HTML method @@ -123,6 +124,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Nonnull @Override public String getUploadDate() throws ParsingException { + assertPageFetched(); try { return doc.select("meta[itemprop=datePublished]").attr(CONTENT); } catch (Exception e) {//todo: add fallback method @@ -133,6 +135,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Nonnull @Override public String getThumbnailUrl() throws ParsingException { + assertPageFetched(); // Try to get high resolution thumbnail first, if it fails, use low res from the player instead try { return doc.select("link[itemprop=\"thumbnailUrl\"]").first().attr("abs:href"); @@ -156,6 +159,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Nonnull @Override public String getDescription() throws ParsingException { + assertPageFetched(); try { return doc.select("p[id=\"eow-description\"]").first().html(); } catch (Exception e) {//todo: add fallback method <-- there is no ... as long as i know @@ -165,6 +169,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public int getAgeLimit() throws ParsingException { + assertPageFetched(); if (!isAgeRestricted) { return NO_AGE_LIMIT; } @@ -178,6 +183,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public long getLength() throws ParsingException { + assertPageFetched(); if(playerArgs != null) { try { long returnValue = Long.parseLong(playerArgs.get("length_seconds") + ""); @@ -216,6 +222,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public long getViewCount() throws ParsingException { + assertPageFetched(); try { return Long.parseLong(doc.select("meta[itemprop=interactionCount]").attr(CONTENT)); } catch (Exception e) {//todo: find fallback method @@ -225,6 +232,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public long getLikeCount() throws ParsingException { + assertPageFetched(); String likesString = ""; try { Element button = doc.select("button.like-button-renderer-like-button").first(); @@ -244,6 +252,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public long getDislikeCount() throws ParsingException { + assertPageFetched(); String dislikesString = ""; try { Element button = doc.select("button.like-button-renderer-dislike-button").first(); @@ -264,6 +273,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Nonnull @Override public String getUploaderUrl() throws ParsingException { + assertPageFetched(); try { return doc.select("div[class=\"yt-user-info\"]").first().children() .select("a").first().attr("abs:href"); @@ -275,6 +285,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Nullable private String getStringFromMetaData(String field) { + assertPageFetched(); String value = null; if(playerArgs != null) { // This can not fail @@ -290,6 +301,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Nonnull @Override public String getUploaderName() throws ParsingException { + assertPageFetched(); String name = getStringFromMetaData("author"); if(name == null) { @@ -309,6 +321,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Nonnull @Override public String getUploaderAvatarUrl() throws ParsingException { + assertPageFetched(); try { return doc.select("a[class*=\"yt-user-photo\"]").first() .select("img").first() @@ -320,6 +333,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public String getDashMpdUrl() throws ParsingException { + assertPageFetched(); try { String dashManifestUrl; if (videoInfoPage.containsKey("dashmpd")) { @@ -346,6 +360,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public List getAudioStreams() throws IOException, ExtractionException { + assertPageFetched(); List audioStreams = new ArrayList<>(); try { for (Map.Entry entry : getItags(ADAPTIVE_FMTS, ItagItem.ItagType.AUDIO).entrySet()) { @@ -365,6 +380,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public List getVideoStreams() throws IOException, ExtractionException { + assertPageFetched(); List videoStreams = new ArrayList<>(); try { for (Map.Entry entry : getItags(URL_ENCODED_FMT_STREAM_MAP, ItagItem.ItagType.VIDEO).entrySet()) { @@ -384,6 +400,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public List getVideoOnlyStreams() throws IOException, ExtractionException { + assertPageFetched(); List videoOnlyStreams = new ArrayList<>(); try { for (Map.Entry entry : getItags(ADAPTIVE_FMTS, ItagItem.ItagType.VIDEO_ONLY).entrySet()) { @@ -410,6 +427,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override @Nullable public List getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException { + assertPageFetched(); if(isAgeRestricted) { // If the video is age restricted getPlayerConfig will fail return null; @@ -459,6 +477,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public StreamInfoItem getNextVideo() throws IOException, ExtractionException { + assertPageFetched(); try { StreamInfoItemCollector collector = new StreamInfoItemCollector(getServiceId()); collector.commit(extractVideoPreviewInfo(doc.select("div[class=\"watch-sidebar-section\"]") @@ -472,6 +491,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public StreamInfoItemCollector getRelatedVideos() throws IOException, ExtractionException { + assertPageFetched(); try { StreamInfoItemCollector collector = new StreamInfoItemCollector(getServiceId()); Element ul = doc.select("ul[id=\"watch-related\"]").first();