[YouTube] Fix parsing of video reminders

This commit is contained in:
Mauricio Colli 2019-04-28 17:03:18 -03:00
parent d8280ce0da
commit cb1e327a6e
No known key found for this signature in database
GPG Key ID: F200BFD6F29DDD85
2 changed files with 51 additions and 16 deletions

View File

@ -1038,19 +1038,6 @@ public class YoutubeStreamExtractor extends StreamExtractor {
return ""; return "";
} }
@Override
public long getViewCount() throws ParsingException {
try {
if (getStreamType() == StreamType.LIVE_STREAM) return -1;
return Long.parseLong(Utils.removeNonDigitCharacters(
li.select("span.view-count").first().text()));
} catch (Exception e) {
//related videos sometimes have no view count
return 0;
}
}
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
Element img = li.select("img").first(); Element img = li.select("img").first();

View File

@ -10,7 +10,9 @@ import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.utils.Utils; import org.schabi.newpipe.extractor.utils.Utils;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date;
/* /*
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org> * Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
@ -150,6 +152,15 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
} }
try { try {
if (isVideoReminder()) {
final Calendar calendar = getDateFromReminder();
if (calendar != null) {
return cachedUploadDate = new SimpleDateFormat("yyyy-MM-dd HH:mm")
.format(calendar.getTime());
}
}
Element meta = item.select("div[class=\"yt-lockup-meta\"]").first(); Element meta = item.select("div[class=\"yt-lockup-meta\"]").first();
if (meta == null) return ""; if (meta == null) return "";
@ -168,6 +179,13 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
return null; return null;
} }
if (isVideoReminder()) {
final Calendar calendar = getDateFromReminder();
if (calendar != null) {
return calendar;
}
}
String textualUploadDate = getTextualUploadDate(); String textualUploadDate = getTextualUploadDate();
if (timeAgoParser != null && textualUploadDate != null && !textualUploadDate.isEmpty()) { if (timeAgoParser != null && textualUploadDate != null && !textualUploadDate.isEmpty()) {
return timeAgoParser.parse(textualUploadDate); return timeAgoParser.parse(textualUploadDate);
@ -180,8 +198,12 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
public long getViewCount() throws ParsingException { public long getViewCount() throws ParsingException {
String input; String input;
if (getStreamType().equals(StreamType.LIVE_STREAM)) { final Element spanViewCount = item.select("span.view-count").first();
Element meta = item.select("ul[class=\"yt-lockup-meta-info\"]").first(); if (spanViewCount != null) {
input = spanViewCount.text();
} else if (getStreamType().equals(StreamType.LIVE_STREAM)) {
Element meta = item.select("ul.yt-lockup-meta-info").first();
if (meta == null) return 0; if (meta == null) return 0;
final Elements li = meta.select("li"); final Elements li = meta.select("li");
@ -190,7 +212,7 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
input = li.first().text(); input = li.first().text();
} else { } else {
try { try {
Element meta = item.select("div[class=\"yt-lockup-meta\"]").first(); Element meta = item.select("div.yt-lockup-meta").first();
if (meta == null) return -1; if (meta == null) return -1;
// This case can happen if google releases a special video // This case can happen if google releases a special video
@ -238,6 +260,32 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
} }
} }
private boolean isVideoReminder() {
return !item.select("span.yt-uix-livereminder").isEmpty();
}
private Calendar getDateFromReminder() throws ParsingException {
final Element timeFuture = item.select("span.yt-badge.localized-date").first();
if (timeFuture == null) {
throw new ParsingException("Span timeFuture is null");
}
final String timestamp = timeFuture.attr("data-timestamp");
if (!timestamp.isEmpty()) {
try {
final Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(Long.parseLong(timestamp) * 1000L));
return calendar;
} catch (Exception e) {
throw new ParsingException("Could not parse = \"" + timestamp + "\"");
}
}
throw new ParsingException("Could not parse date from reminder element: \"" + timeFuture + "\"");
}
/** /**
* Generic method that checks if the element contains any clues that it's a livestream item * Generic method that checks if the element contains any clues that it's a livestream item
*/ */