|
| 1 | +package org.schabi.newpipe.extractor.services.youtube.extractors; |
| 2 | + |
| 3 | +import com.grack.nanojson.JsonArray; |
| 4 | +import com.grack.nanojson.JsonObject; |
| 5 | + |
| 6 | +import org.schabi.newpipe.extractor.Image; |
| 7 | +import org.schabi.newpipe.extractor.comments.CommentsInfoItemExtractor; |
| 8 | +import org.schabi.newpipe.extractor.exceptions.ParsingException; |
| 9 | +import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; |
| 10 | +import org.schabi.newpipe.extractor.stream.Description; |
| 11 | + |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import javax.annotation.Nonnull; |
| 16 | + |
| 17 | +/** |
| 18 | + * Extracts comment info from a YouTube live chat message. |
| 19 | + */ |
| 20 | +public class YoutubeLiveChatInfoItemExtractor implements CommentsInfoItemExtractor { |
| 21 | + |
| 22 | + private final JsonObject chatMessage; |
| 23 | + |
| 24 | + public YoutubeLiveChatInfoItemExtractor(final JsonObject chatMessage) { |
| 25 | + this.chatMessage = chatMessage; |
| 26 | + } |
| 27 | + |
| 28 | + @Nonnull |
| 29 | + @Override |
| 30 | + public Description getCommentText() throws ParsingException { |
| 31 | + return new Description(YoutubeParsingHelper.getTextFromObject( |
| 32 | + chatMessage.getObject("message")), Description.PLAIN_TEXT); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public String getCommentId() throws ParsingException { |
| 37 | + return chatMessage.getString("id", ""); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getUploaderName() throws ParsingException { |
| 42 | + return YoutubeParsingHelper.getTextFromObject( |
| 43 | + chatMessage.getObject("authorName")); |
| 44 | + } |
| 45 | + |
| 46 | + @Nonnull |
| 47 | + @Override |
| 48 | + public List<Image> getUploaderAvatars() throws ParsingException { |
| 49 | + try { |
| 50 | + final JsonArray thumbnails = chatMessage.getObject("authorPhoto") |
| 51 | + .getArray("thumbnails"); |
| 52 | + return YoutubeParsingHelper.getImagesFromThumbnailsArray(thumbnails); |
| 53 | + } catch (final Exception e) { |
| 54 | + return List.of(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getUploaderUrl() throws ParsingException { |
| 60 | + try { |
| 61 | + return YoutubeParsingHelper.getUrlFromNavigationEndpoint( |
| 62 | + chatMessage.getObject("authorEndpoint")); |
| 63 | + } catch (final Exception e) { |
| 64 | + return ""; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean isChannelOwner() throws ParsingException { |
| 70 | + final JsonArray badges = chatMessage.getArray("authorBadges"); |
| 71 | + for (int i = 0; i < badges.size(); i++) { |
| 72 | + final JsonObject badge = badges.getObject(i); |
| 73 | + if (badge.has("liveChatAuthorBadgeRenderer")) { |
| 74 | + final JsonObject renderer = badge.getObject( |
| 75 | + "liveChatAuthorBadgeRenderer"); |
| 76 | + if (renderer.has("icon")) { |
| 77 | + final String iconType = renderer.getObject("icon") |
| 78 | + .getString("iconType", ""); |
| 79 | + if ("owner".equals(iconType)) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + @Nonnull |
| 89 | + @Override |
| 90 | + public List<Image> getThumbnails() throws ParsingException { |
| 91 | + return Collections.emptyList(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String getName() throws ParsingException { |
| 96 | + return getUploaderName(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String getUrl() throws ParsingException { |
| 101 | + return ""; |
| 102 | + } |
| 103 | +} |
0 commit comments