Skip to content

Commit 05f6a5e

Browse files
committed
Separate live chat from comments extractor
1 parent 3169692 commit 05f6a5e

5 files changed

Lines changed: 74 additions & 22 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsExtractor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public boolean isLiveChat() throws ExtractionException {
3131
return false;
3232
}
3333

34+
/**
35+
* @apiNote Warning: This method is experimental and may get removed in a future release.
36+
* Configures this extractor to fetch live chat messages using the given continuation.
37+
*/
38+
public void setLiveChatContinuation(final String continuation) {
39+
// no-op by default
40+
}
41+
3442
/**
3543
* @return the total number of comments
3644
*/

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

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public YoutubeCommentsExtractor(
6565
public InfoItemsPage<CommentsInfoItem> getInitialPage()
6666
throws IOException, ExtractionException {
6767

68-
if (commentsDisabled && liveChatContinuation != null) {
68+
if (liveChatContinuation != null) {
6969
return fetchLiveChat(liveChatContinuation);
7070
}
7171

@@ -209,8 +209,7 @@ private Page getNextPage(final String continuation) throws ParsingException {
209209
public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
210210
throws IOException, ExtractionException {
211211

212-
if ("live_chat".equals(page.getUrl())
213-
|| (commentsDisabled && liveChatContinuation != null)) {
212+
if ("live_chat".equals(page.getUrl()) || liveChatContinuation != null) {
214213
isLiveStream = true;
215214
return fetchLiveChat(page.getId());
216215
}
@@ -376,8 +375,6 @@ public void onFetchPage(@Nonnull final Downloader downloader)
376375
final String initialToken = findInitialCommentsToken(nextResponse);
377376

378377
if (initialToken == null) {
379-
// Try to extract live chat continuation for live streams
380-
findLiveChatContinuation(nextResponse);
381378
return;
382379
}
383380

@@ -393,24 +390,11 @@ public void onFetchPage(@Nonnull final Downloader downloader)
393390
}
394391

395392
/**
396-
* Tries to extract a live chat continuation token from the next response.
397-
* This is used when regular comments are disabled on a live stream.
393+
* Configures this extractor to fetch live chat messages.
398394
*/
399-
private void findLiveChatContinuation(final JsonObject nextResponse) {
400-
try {
401-
final JsonObject liveChatRenderer = nextResponse
402-
.getObject("contents")
403-
.getObject("twoColumnWatchNextResults")
404-
.getObject("conversationBar")
405-
.getObject("liveChatRenderer");
406-
liveChatContinuation = liveChatRenderer
407-
.getArray("continuations")
408-
.getObject(0)
409-
.getObject("reloadContinuationData")
410-
.getString("continuation");
411-
} catch (final Exception e) {
412-
liveChatContinuation = null;
413-
}
395+
@Override
396+
public void setLiveChatContinuation(final String continuation) {
397+
this.liveChatContinuation = continuation;
414398
}
415399

416400
/**

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,26 @@ public void onFetchPage(@Nonnull final Downloader downloader)
879879
.done())
880880
.getBytes(StandardCharsets.UTF_8);
881881
nextResponse = getJsonPostResponse(NEXT, nextBody, localization);
882+
883+
// Check for live chat availability
884+
findLiveChatContinuation(nextResponse);
885+
}
886+
887+
private void findLiveChatContinuation(final JsonObject response) {
888+
try {
889+
final JsonObject liveChatRenderer = response
890+
.getObject("contents")
891+
.getObject("twoColumnWatchNextResults")
892+
.getObject("conversationBar")
893+
.getObject("liveChatRenderer");
894+
liveChatContinuation = liveChatRenderer
895+
.getArray("continuations")
896+
.getObject(0)
897+
.getObject("reloadContinuationData")
898+
.getString("continuation");
899+
} catch (final Exception e) {
900+
liveChatContinuation = null;
901+
}
882902
}
883903

884904
private static void checkPlayabilityStatus(@Nonnull final JsonObject playabilityStatus)

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ public StreamExtractor(final StreamingService service, final LinkHandler linkHan
5656
super(service, linkHandler);
5757
}
5858

59+
/**
60+
* @return {@code true} if this stream has a live chat available
61+
*/
62+
public boolean hasLiveChat() {
63+
return liveChatContinuation != null;
64+
}
65+
66+
/**
67+
* @return the live chat continuation token, or {@code null} if not available
68+
*/
69+
public String getLiveChatContinuation() {
70+
return liveChatContinuation;
71+
}
72+
73+
protected String liveChatContinuation = null;
74+
5975
/**
6076
* The original textual date provided by the service. Should be used as a fallback if
6177
* {@link #getUploadDate()} isn't provided by the service, or it fails for some reason.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,12 @@ private static void extractOptionalData(final StreamInfo streamInfo,
353353
} catch (final Exception e) {
354354
streamInfo.addError(e);
355355
}
356+
try {
357+
streamInfo.setLiveChat(extractor.hasLiveChat());
358+
streamInfo.setLiveChatContinuation(extractor.getLiveChatContinuation());
359+
} catch (final Exception e) {
360+
streamInfo.addError(e);
361+
}
356362

357363
streamInfo.setRelatedItems(ExtractorHelper.getRelatedItemsOrLogError(streamInfo,
358364
extractor));
@@ -406,6 +412,8 @@ private static void extractOptionalData(final StreamInfo streamInfo,
406412
private boolean shortFormContent = false;
407413
@Nonnull
408414
private ContentAvailability contentAvailability = ContentAvailability.AVAILABLE;
415+
private boolean liveChat = false;
416+
private String liveChatContinuation = null;
409417

410418
/**
411419
* Preview frames, e.g. for the storyboard / seekbar thumbnail preview
@@ -761,4 +769,20 @@ public ContentAvailability getContentAvailability() {
761769
public void setContentAvailability(@Nonnull final ContentAvailability availability) {
762770
this.contentAvailability = availability;
763771
}
772+
773+
public boolean hasLiveChat() {
774+
return liveChat;
775+
}
776+
777+
public void setLiveChat(final boolean liveChat) {
778+
this.liveChat = liveChat;
779+
}
780+
781+
public String getLiveChatContinuation() {
782+
return liveChatContinuation;
783+
}
784+
785+
public void setLiveChatContinuation(final String liveChatContinuation) {
786+
this.liveChatContinuation = liveChatContinuation;
787+
}
764788
}

0 commit comments

Comments
 (0)