Skip to content

Commit cb1e327

Browse files
committed
[YouTube] Fix parsing of video reminders
1 parent d8280ce commit cb1e327

2 files changed

Lines changed: 51 additions & 16 deletions

File tree

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,19 +1038,6 @@ public String getTextualUploadDate() throws ParsingException {
10381038
return "";
10391039
}
10401040

1041-
@Override
1042-
public long getViewCount() throws ParsingException {
1043-
try {
1044-
if (getStreamType() == StreamType.LIVE_STREAM) return -1;
1045-
1046-
return Long.parseLong(Utils.removeNonDigitCharacters(
1047-
li.select("span.view-count").first().text()));
1048-
} catch (Exception e) {
1049-
//related videos sometimes have no view count
1050-
return 0;
1051-
}
1052-
}
1053-
10541041
@Override
10551042
public String getThumbnailUrl() throws ParsingException {
10561043
Element img = li.select("img").first();

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import org.schabi.newpipe.extractor.utils.Utils;
1111

1212
import javax.annotation.Nullable;
13+
import java.text.SimpleDateFormat;
1314
import java.util.Calendar;
15+
import java.util.Date;
1416

1517
/*
1618
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
@@ -150,6 +152,15 @@ public String getTextualUploadDate() throws ParsingException {
150152
}
151153

152154
try {
155+
if (isVideoReminder()) {
156+
final Calendar calendar = getDateFromReminder();
157+
if (calendar != null) {
158+
return cachedUploadDate = new SimpleDateFormat("yyyy-MM-dd HH:mm")
159+
.format(calendar.getTime());
160+
}
161+
}
162+
163+
153164
Element meta = item.select("div[class=\"yt-lockup-meta\"]").first();
154165
if (meta == null) return "";
155166

@@ -168,6 +179,13 @@ public Calendar getUploadDate() throws ParsingException {
168179
return null;
169180
}
170181

182+
if (isVideoReminder()) {
183+
final Calendar calendar = getDateFromReminder();
184+
if (calendar != null) {
185+
return calendar;
186+
}
187+
}
188+
171189
String textualUploadDate = getTextualUploadDate();
172190
if (timeAgoParser != null && textualUploadDate != null && !textualUploadDate.isEmpty()) {
173191
return timeAgoParser.parse(textualUploadDate);
@@ -180,8 +198,12 @@ public Calendar getUploadDate() throws ParsingException {
180198
public long getViewCount() throws ParsingException {
181199
String input;
182200

183-
if (getStreamType().equals(StreamType.LIVE_STREAM)) {
184-
Element meta = item.select("ul[class=\"yt-lockup-meta-info\"]").first();
201+
final Element spanViewCount = item.select("span.view-count").first();
202+
if (spanViewCount != null) {
203+
input = spanViewCount.text();
204+
205+
} else if (getStreamType().equals(StreamType.LIVE_STREAM)) {
206+
Element meta = item.select("ul.yt-lockup-meta-info").first();
185207
if (meta == null) return 0;
186208

187209
final Elements li = meta.select("li");
@@ -190,7 +212,7 @@ public long getViewCount() throws ParsingException {
190212
input = li.first().text();
191213
} else {
192214
try {
193-
Element meta = item.select("div[class=\"yt-lockup-meta\"]").first();
215+
Element meta = item.select("div.yt-lockup-meta").first();
194216
if (meta == null) return -1;
195217

196218
// This case can happen if google releases a special video
@@ -238,6 +260,32 @@ public String getThumbnailUrl() throws ParsingException {
238260
}
239261
}
240262

263+
264+
private boolean isVideoReminder() {
265+
return !item.select("span.yt-uix-livereminder").isEmpty();
266+
}
267+
268+
private Calendar getDateFromReminder() throws ParsingException {
269+
final Element timeFuture = item.select("span.yt-badge.localized-date").first();
270+
271+
if (timeFuture == null) {
272+
throw new ParsingException("Span timeFuture is null");
273+
}
274+
275+
final String timestamp = timeFuture.attr("data-timestamp");
276+
if (!timestamp.isEmpty()) {
277+
try {
278+
final Calendar calendar = Calendar.getInstance();
279+
calendar.setTime(new Date(Long.parseLong(timestamp) * 1000L));
280+
return calendar;
281+
} catch (Exception e) {
282+
throw new ParsingException("Could not parse = \"" + timestamp + "\"");
283+
}
284+
}
285+
286+
throw new ParsingException("Could not parse date from reminder element: \"" + timeFuture + "\"");
287+
}
288+
241289
/**
242290
* Generic method that checks if the element contains any clues that it's a livestream item
243291
*/

0 commit comments

Comments
 (0)