2018-09-25 23:50:29 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
import com.grack.nanojson.JsonArray;
|
|
|
|
import com.grack.nanojson.JsonObject;
|
2020-04-15 14:09:46 +02:00
|
|
|
|
2018-09-25 23:50:29 +02:00
|
|
|
import org.schabi.newpipe.extractor.comments.CommentsInfoItemExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2019-11-03 19:45:25 +01:00
|
|
|
import org.schabi.newpipe.extractor.localization.DateWrapper;
|
2020-02-08 23:58:46 +01:00
|
|
|
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
|
|
|
|
2019-11-03 19:45:25 +01: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;
|
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;
|
2019-04-28 22:03:16 +02:00
|
|
|
private final TimeAgoParser timeAgoParser;
|
2018-09-26 00:51:58 +02:00
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
public YoutubeCommentsInfoItemExtractor(JsonObject json, String url, TimeAgoParser timeAgoParser) {
|
2018-09-25 23:50:29 +02:00
|
|
|
this.json = json;
|
|
|
|
this.url = url;
|
2019-04-28 22:03:16 +02:00
|
|
|
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 {
|
2020-08-05 18:25:35 +02:00
|
|
|
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) {
|
2019-03-22 19:06:35 +01:00
|
|
|
return "";
|
2018-09-26 00:51:58 +02:00
|
|
|
}
|
2018-09-25 23:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-21 19:49:17 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-03 19:45:25 +01:00
|
|
|
@Nullable
|
2018-09-25 23:50:29 +02:00
|
|
|
@Override
|
2020-04-21 19:49:17 +02:00
|
|
|
public DateWrapper getUploadDate() throws ParsingException {
|
|
|
|
String textualPublishedTime = getTextualUploadDate();
|
2019-04-28 22:03:16 +02:00
|
|
|
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 {
|
2020-08-05 18:25:35 +02:00
|
|
|
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
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
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);
|
2018-09-28 01:24:57 +02:00
|
|
|
} 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
|
2020-04-21 19:49:17 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-11 18:38:54 +01:00
|
|
|
@Override
|
|
|
|
public boolean getHeartedByUploader() throws ParsingException {
|
|
|
|
return json.has("creatorHeart");
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:59:22 +01:00
|
|
|
@Override
|
|
|
|
public boolean getPinned() {
|
|
|
|
return json.has("pinnedCommentBadge");
|
|
|
|
}
|
|
|
|
|
2018-09-25 23:50:29 +02:00
|
|
|
@Override
|
2020-04-21 19:49:17 +02:00
|
|
|
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) {
|
2019-03-22 19:06:35 +01:00
|
|
|
return "";
|
2018-09-26 00:51:58 +02:00
|
|
|
}
|
2018-09-25 23:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-21 19:49:17 +02:00
|
|
|
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) {
|
2019-03-22 19:06:35 +01:00
|
|
|
return "";
|
2018-09-26 00:51:58 +02:00
|
|
|
}
|
2018-09-25 23:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|