Add checks if page is fetched

This commit is contained in:
Coffeemakr 2017-11-30 10:49:27 +01:00
parent 23231732ca
commit f037f67a15
No known key found for this signature in database
GPG key ID: 3F35676D8FF6E743

View file

@ -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<AudioStream> getAudioStreams() throws IOException, ExtractionException {
assertPageFetched();
List<AudioStream> audioStreams = new ArrayList<>();
try {
for (Map.Entry<String, ItagItem> entry : getItags(ADAPTIVE_FMTS, ItagItem.ItagType.AUDIO).entrySet()) {
@ -365,6 +380,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
@Override
public List<VideoStream> getVideoStreams() throws IOException, ExtractionException {
assertPageFetched();
List<VideoStream> videoStreams = new ArrayList<>();
try {
for (Map.Entry<String, ItagItem> entry : getItags(URL_ENCODED_FMT_STREAM_MAP, ItagItem.ItagType.VIDEO).entrySet()) {
@ -384,6 +400,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
@Override
public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionException {
assertPageFetched();
List<VideoStream> videoOnlyStreams = new ArrayList<>();
try {
for (Map.Entry<String, ItagItem> entry : getItags(ADAPTIVE_FMTS, ItagItem.ItagType.VIDEO_ONLY).entrySet()) {
@ -410,6 +427,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
@Override
@Nullable
public List<Subtitles> 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();