Skip to content

Commit b6b3950

Browse files
litetexStypox
andcommitted
Small live stream improvements and cleanup
Thanks to Stypox review * Improved live stream related code * Fixed Javadoc Co-Authored-By: Stypox <stypox@pm.me>
1 parent 64e4baf commit b6b3950

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ public String getName() throws ParsingException {
136136

137137
@Override
138138
public long getDuration() throws ParsingException {
139-
// Duration can only be extracted for normal videos, not live streams
140-
if (getStreamType() != StreamType.VIDEO_STREAM) {
139+
// Duration can not be extract from live streams, only from normal videos
140+
if (isLive()) {
141141
return -1;
142142
}
143143

@@ -220,6 +220,12 @@ public String getTextualUploadDate() throws ParsingException {
220220
return cachedTextualUploadDate.orElse(null);
221221
}
222222

223+
// Live streams have no upload date
224+
if (isLive()) {
225+
cachedTextualUploadDate = Optional.empty();
226+
return null;
227+
}
228+
223229
// This might be null e.g. for live streams
224230
this.cachedTextualUploadDate = metadataPart(1, 1)
225231
.map(this::getTextContentFromMetadataPart);
@@ -249,7 +255,11 @@ public long getViewCount() throws ParsingException {
249255
if (optTextContent.isPresent()) {
250256
return getViewCountFromViewCountText(optTextContent.get());
251257
}
252-
return -1;
258+
return !isLive()
259+
? -1
260+
// Live streams don't have the metadata row present if there are 0 viewers
261+
// https://github.com/TeamNewPipe/NewPipeExtractor/pull/1320#discussion_r2205837528
262+
: 0;
253263
}
254264

255265
private long getViewCountFromViewCountText(@Nonnull final String viewCountText)
@@ -322,6 +332,10 @@ private String getTextContentFromMetadataPart(final JsonObject metadataPart) {
322332
return metadataPart.getObject("text").getString("content");
323333
}
324334

335+
private boolean isLive() throws ParsingException {
336+
return getStreamType() != StreamType.VIDEO_STREAM;
337+
}
338+
325339
abstract static class ChannelImageViewModel {
326340
protected JsonObject viewModel;
327341

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public interface StreamInfoItemExtractor extends InfoItemExtractor {
5858
/**
5959
* Parses the number of views
6060
*
61-
* @return the number of views or -1 for live streams
61+
* @return the number of views or -1 if not available
6262
* @throws ParsingException if there is an error in the extraction
6363
*/
6464
long getViewCount() throws ParsingException;

0 commit comments

Comments
 (0)