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;
|
2021-05-21 19:51:56 +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;
|
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;
|
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) {
|
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
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 19:40:51 +02:00
|
|
|
/**
|
|
|
|
* @implNote The method is parsing internally a localized string.<br/>
|
|
|
|
* This will fail for other languages than English.
|
|
|
|
* However as long as the Extractor only uses "en-GB"
|
|
|
|
* (as seen in {@link org.schabi.newpipe.extractor.services.youtube.YoutubeService#SUPPORTED_LANGUAGES})
|
|
|
|
* everything will work fine.<br/>
|
|
|
|
* Consider using {@link #getTextualVoteCount()}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int getLikeCount() throws ParsingException {
|
|
|
|
// This may return a language dependent version, e.g. in German: 3,3 Mio
|
|
|
|
String voteCount = getTextualVoteCount();
|
|
|
|
try {
|
|
|
|
if (Utils.isBlank(voteCount)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) Utils.mixedNumberWordToLong(voteCount);
|
|
|
|
} catch (Exception e) {
|
2021-05-21 19:53:39 +02:00
|
|
|
throw new ParsingException("Unexpected error while converting vote count to like count", e);
|
2021-05-21 19:40:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
@Override
|
2021-05-20 20:10:14 +02:00
|
|
|
public String getTextualVoteCount() throws ParsingException {
|
2021-05-20 21:09:45 +02:00
|
|
|
/*
|
|
|
|
* Example results as of 2021-05-20:
|
2021-05-20 20:10:14 +02:00
|
|
|
* Language = English
|
|
|
|
* 3.3M
|
|
|
|
* 48K
|
|
|
|
* 1.4K
|
|
|
|
* 270K
|
|
|
|
* 19
|
|
|
|
* 6
|
|
|
|
*
|
|
|
|
* Language = German
|
|
|
|
* 3,3 Mio
|
|
|
|
* 48.189
|
|
|
|
* 1419
|
|
|
|
* 270.984
|
|
|
|
* 19
|
|
|
|
* 6
|
|
|
|
*/
|
2018-09-26 00:51:58 +02:00
|
|
|
try {
|
2021-05-20 22:25:14 +02:00
|
|
|
// If a comment has no likes voteCount is not set
|
2021-05-21 19:51:56 +02:00
|
|
|
if (!json.has("voteCount")) {
|
2021-05-20 21:23:31 +02:00
|
|
|
return EMPTY_STRING;
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:10:14 +02:00
|
|
|
final JsonObject voteCountObj = JsonUtils.getObject(json, "voteCount");
|
2021-05-21 19:51:56 +02:00
|
|
|
if (voteCountObj.isEmpty()) {
|
2021-05-20 20:10:14 +02:00
|
|
|
return EMPTY_STRING;
|
|
|
|
}
|
|
|
|
return getTextFromObject(voteCountObj);
|
2018-09-26 00:51:58 +02:00
|
|
|
} catch (Exception e) {
|
2021-05-20 20:10:14 +02:00
|
|
|
throw new ParsingException("Could not get vote count", e);
|
2018-09-26 00:51:58 +02:00
|
|
|
}
|
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
|
2021-02-07 22:42:21 +01:00
|
|
|
return EMPTY_STRING;
|
2020-08-05 18:25:35 +02:00
|
|
|
}
|
|
|
|
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
|
2021-01-23 13:06:07 +01:00
|
|
|
public boolean isHeartedByUploader() throws ParsingException {
|
2021-01-11 18:38:54 +01:00
|
|
|
return json.has("creatorHeart");
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:59:22 +01:00
|
|
|
@Override
|
2021-01-23 13:06:07 +01:00
|
|
|
public boolean isPinned() {
|
2021-01-22 23:59:22 +01:00
|
|
|
return json.has("pinnedCommentBadge");
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:44:58 +01:00
|
|
|
public boolean isUploaderVerified() throws ParsingException {
|
|
|
|
// impossible to get this information from the mobile layout
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
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
|
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) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|