package org.schabi.newpipe.extractor.services.peertube.extractors; import com.grack.nanojson.JsonObject; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.ServiceList; import org.schabi.newpipe.extractor.comments.CommentsInfoItemExtractor; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper; import org.schabi.newpipe.extractor.stream.Description; import org.schabi.newpipe.extractor.utils.JsonUtils; import javax.annotation.Nullable; import java.util.Objects; public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor { private final JsonObject item; private final String url; private final String baseUrl; public PeertubeCommentsInfoItemExtractor(final JsonObject item, final PeertubeCommentsExtractor extractor) throws ParsingException { this.item = item; this.url = extractor.getUrl(); this.baseUrl = extractor.getBaseUrl(); } @Override public String getUrl() throws ParsingException { return url + "/" + getCommentId(); } @Override public String getThumbnailUrl() { String value; try { value = JsonUtils.getString(item, "account.avatar.path"); } catch (final Exception e) { value = "/client/assets/images/default-avatar.png"; } return baseUrl + value; } @Override public String getName() throws ParsingException { return JsonUtils.getString(item, "account.displayName"); } @Override public String getTextualUploadDate() throws ParsingException { return JsonUtils.getString(item, "createdAt"); } @Override public DateWrapper getUploadDate() throws ParsingException { final String textualUploadDate = getTextualUploadDate(); return new DateWrapper(PeertubeParsingHelper.parseDateFrom(textualUploadDate)); } @Override public Description getCommentText() throws ParsingException { final String htmlText = JsonUtils.getString(item, "text"); try { final Document doc = Jsoup.parse(htmlText); final var text = doc.body().text(); return new Description(text, Description.PLAIN_TEXT); } catch (final Exception e) { final var text = htmlText.replaceAll("(?s)<[^>]*>(\\s*<[^>]*>)*", ""); return new Description(text, Description.PLAIN_TEXT); } } @Override public String getCommentId() { return Objects.toString(item.getLong("id"), null); } @Override public String getUploaderAvatarUrl() { String value; try { value = JsonUtils.getString(item, "account.avatar.path"); } catch (final Exception e) { value = "/client/assets/images/default-avatar.png"; } return baseUrl + value; } @Override public String getUploaderName() throws ParsingException { return JsonUtils.getString(item, "account.name") + "@" + JsonUtils.getString(item, "account.host"); } @Override public String getUploaderUrl() throws ParsingException { final String name = JsonUtils.getString(item, "account.name"); final String host = JsonUtils.getString(item, "account.host"); return ServiceList.PeerTube.getChannelLHFactory() .fromId("accounts/" + name + "@" + host, baseUrl).getUrl(); } @Override @Nullable public Page getReplies() throws ParsingException { if (JsonUtils.getNumber(item, "totalReplies").intValue() == 0) { return null; } final String threadId = JsonUtils.getNumber(item, "threadId").toString(); return new Page(url + "/" + threadId, threadId); } @Override public int getReplyCount() throws ParsingException { return JsonUtils.getNumber(item, "totalReplies").intValue(); } }