Skip to content

Commit 89a77ae

Browse files
committed
[YouTube] Fix detection of ended livestreams and parse livestream upload date
1 parent 827f7bd commit 89a77ae

2 files changed

Lines changed: 24 additions & 17 deletions

File tree

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,27 @@ public String getName() throws ParsingException {
137137
return title;
138138
}
139139

140+
@Nullable
140141
@Override
141142
public String getTextualUploadDate() throws ParsingException {
142-
if (getStreamType().equals(StreamType.LIVE_STREAM)) {
143-
return null;
144-
}
145-
146-
JsonObject micro = playerResponse.getObject("microformat").getObject("playerMicroformatRenderer");
147-
if (micro.isString("uploadDate") && !micro.getString("uploadDate").isEmpty()) {
143+
final JsonObject micro =
144+
playerResponse.getObject("microformat").getObject("playerMicroformatRenderer");
145+
if (!micro.getString("uploadDate", EMPTY_STRING).isEmpty()) {
148146
return micro.getString("uploadDate");
149-
}
150-
if (micro.isString("publishDate") && !micro.getString("publishDate").isEmpty()) {
147+
} else if (!micro.getString("publishDate", EMPTY_STRING).isEmpty()) {
151148
return micro.getString("publishDate");
149+
} else {
150+
final JsonObject liveDetails = micro.getObject("liveBroadcastDetails");
151+
if (!liveDetails.getString("endTimestamp", EMPTY_STRING).isEmpty()) {
152+
// an ended live stream
153+
return liveDetails.getString("endTimestamp");
154+
} else if (!liveDetails.getString("startTimestamp", EMPTY_STRING).isEmpty()) {
155+
// a running live stream
156+
return liveDetails.getString("startTimestamp");
157+
} else if (getStreamType() == StreamType.LIVE_STREAM) {
158+
// this should never be reached, but a live stream without upload date is valid
159+
return null;
160+
}
152161
}
153162

154163
if (getTextFromObject(getVideoPrimaryInfoRenderer().getObject("dateText")).startsWith("Premiered")) {
@@ -176,6 +185,7 @@ public String getTextualUploadDate() throws ParsingException {
176185
return DateTimeFormatter.ISO_LOCAL_DATE.format(localDate);
177186
} catch (Exception ignored) {
178187
}
188+
179189
throw new ParsingException("Could not get upload date");
180190
}
181191

@@ -597,16 +607,13 @@ public List<SubtitlesStream> getSubtitles(final MediaFormat format) throws Parsi
597607
}
598608

599609
@Override
600-
public StreamType getStreamType() throws ParsingException {
610+
public StreamType getStreamType() {
601611
assertPageFetched();
602-
try {
603-
return playerResponse.getObject("videoDetails").getBoolean("isLiveContent")
604-
? StreamType.LIVE_STREAM : StreamType.VIDEO_STREAM;
605-
} catch (Exception e) {
606-
throw new ParsingException("Could not get stream type", e);
607-
}
612+
return playerResponse.getObject("streamingData").has(FORMATS)
613+
? StreamType.VIDEO_STREAM : StreamType.LIVE_STREAM;
608614
}
609615

616+
@Nullable
610617
private StreamInfoItemExtractor getNextStream() throws ExtractionException {
611618
try {
612619
final JsonObject firstWatchNextItem = initialData.getObject("contents")

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public static void setUp() throws Exception {
4545
@Override public long expectedLength() { return 0; }
4646
@Override public long expectedTimestamp() { return TIMESTAMP; }
4747
@Override public long expectedViewCountAtLeast() { return 0; }
48-
@Nullable @Override public String expectedUploadDate() { return null; }
49-
@Nullable @Override public String expectedTextualUploadDate() { return null; }
48+
@Nullable @Override public String expectedUploadDate() { return "2020-02-22 00:00:00.000"; }
49+
@Nullable @Override public String expectedTextualUploadDate() { return "2020-02-22"; }
5050
@Override public long expectedLikeCountAtLeast() { return 825000; }
5151
@Override public long expectedDislikeCountAtLeast() { return 15600; }
5252
@Override public boolean expectedHasSubtitles() { return false; }

0 commit comments

Comments
 (0)