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

147 lines
4.8 KiB
Java
Raw Normal View History

2018-09-25 23:50:29 +02:00
package org.schabi.newpipe.extractor.services.youtube.extractors;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
2018-09-25 23:50:29 +02:00
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.localization.TimeAgoParser;
2018-09-25 23:50:29 +02:00
import org.schabi.newpipe.extractor.utils.JsonUtils;
2019-02-28 19:40:29 +01:00
import org.schabi.newpipe.extractor.utils.Utils;
2018-09-25 23:50:29 +02:00
import javax.annotation.Nullable;
2018-09-25 23:50:29 +02:00
2020-04-15 14:09:46 +02:00
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
2021-02-07 22:42:21 +01:00
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
2020-04-15 18:49:58 +02:00
2018-09-26 00:51:58 +02:00
public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
2018-09-25 23:50:29 +02:00
private final JsonObject json;
private final String url;
private final TimeAgoParser timeAgoParser;
2018-09-26 00:51:58 +02:00
public YoutubeCommentsInfoItemExtractor(JsonObject json, String url, TimeAgoParser timeAgoParser) {
2018-09-25 23:50:29 +02:00
this.json = json;
this.url = url;
this.timeAgoParser = timeAgoParser;
2018-09-25 23:50:29 +02:00
}
@Override
public String getUrl() throws ParsingException {
return url;
}
@Override
public String getThumbnailUrl() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
final JsonArray arr = JsonUtils.getArray(json, "authorThumbnail.thumbnails");
2018-10-19 16:03:36 +02:00
return JsonUtils.getString(arr.getObject(2), "url");
2018-09-26 00:51:58 +02:00
} catch (Exception e) {
throw new ParsingException("Could not get thumbnail url", e);
}
2018-09-25 23:50:29 +02:00
}
@Override
public String getName() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
2020-04-15 14:09:46 +02:00
return getTextFromObject(JsonUtils.getObject(json, "authorText"));
2018-09-26 00:51:58 +02:00
} catch (Exception e) {
2021-02-07 22:42:21 +01:00
return EMPTY_STRING;
2018-09-26 00:51:58 +02:00
}
2018-09-25 23:50:29 +02:00
}
@Override
public String getTextualUploadDate() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
2020-04-15 14:09:46 +02:00
return getTextFromObject(JsonUtils.getObject(json, "publishedTimeText"));
2018-09-26 00:51:58 +02:00
} catch (Exception e) {
throw new ParsingException("Could not get publishedTimeText", e);
}
2018-09-25 23:50:29 +02:00
}
@Nullable
2018-09-25 23:50:29 +02:00
@Override
public DateWrapper getUploadDate() throws ParsingException {
String textualPublishedTime = getTextualUploadDate();
if (timeAgoParser != null && textualPublishedTime != null && !textualPublishedTime.isEmpty()) {
return timeAgoParser.parse(textualPublishedTime);
} else {
return null;
}
}
@Override
public int getLikeCount() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
2020-06-13 20:25:38 +02:00
return json.getInt("likeCount");
2018-09-26 00:51:58 +02:00
} catch (Exception e) {
throw new ParsingException("Could not get like count", e);
}
2018-09-25 23:50:29 +02:00
}
@Override
public String getCommentText() throws ParsingException {
try {
final JsonObject contentText = JsonUtils.getObject(json, "contentText");
if (contentText.isEmpty()) {
// completely empty comments as described in
// https://github.com/TeamNewPipe/NewPipeExtractor/issues/380#issuecomment-668808584
2021-02-07 22:42:21 +01:00
return EMPTY_STRING;
}
final String commentText = getTextFromObject(contentText);
2019-02-28 19:40:29 +01:00
// youtube adds U+FEFF in some comments. eg. https://www.youtube.com/watch?v=Nj4F63E59io<feff>
return Utils.removeUTF8BOM(commentText);
} catch (Exception e) {
throw new ParsingException("Could not get comment text", e);
2018-09-25 23:50:29 +02:00
}
}
@Override
public String getCommentId() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
2018-10-19 16:03:36 +02:00
return JsonUtils.getString(json, "commentId");
2018-09-26 00:51:58 +02:00
} catch (Exception e) {
throw new ParsingException("Could not get comment id", e);
}
2018-09-25 23:50:29 +02:00
}
@Override
public String getUploaderAvatarUrl() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
2018-10-19 16:03:36 +02:00
JsonArray arr = JsonUtils.getArray(json, "authorThumbnail.thumbnails");
return JsonUtils.getString(arr.getObject(2), "url");
2018-09-26 00:51:58 +02:00
} catch (Exception e) {
throw new ParsingException("Could not get author thumbnail", e);
}
2018-09-25 23:50:29 +02:00
}
@Override
public boolean isHeartedByUploader() throws ParsingException {
return json.has("creatorHeart");
}
2021-01-22 23:59:22 +01:00
@Override
public boolean isPinned() {
2021-01-22 23:59:22 +01:00
return json.has("pinnedCommentBadge");
}
2018-09-25 23:50:29 +02:00
@Override
public String getUploaderName() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
2020-04-15 14:09:46 +02:00
return getTextFromObject(JsonUtils.getObject(json, "authorText"));
2018-09-26 00:51:58 +02:00
} catch (Exception e) {
2021-02-07 22:42:21 +01:00
return EMPTY_STRING;
2018-09-26 00:51:58 +02:00
}
2018-09-25 23:50:29 +02:00
}
@Override
public String getUploaderUrl() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
2019-02-23 13:50:12 +01:00
return "https://youtube.com/channel/" + JsonUtils.getString(json, "authorEndpoint.browseEndpoint.browseId");
2018-09-26 00:51:58 +02:00
} catch (Exception e) {
2021-02-07 22:42:21 +01:00
return EMPTY_STRING;
2018-09-26 00:51:58 +02:00
}
2018-09-25 23:50:29 +02:00
}
}