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

112 lines
3.6 KiB
Java
Raw Normal View History

2018-09-25 23:50:29 +02:00
package org.schabi.newpipe.extractor.services.youtube.extractors;
import org.schabi.newpipe.extractor.comments.CommentsInfoItemExtractor;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
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 com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
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;
2018-09-26 00:51:58 +02:00
2018-09-25 23:50:29 +02:00
public YoutubeCommentsInfoItemExtractor(JsonObject json, String url) {
this.json = json;
this.url = url;
}
@Override
public String getUrl() throws ParsingException {
return url;
}
@Override
public String getThumbnailUrl() 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 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 {
2018-10-19 16:03:36 +02:00
return YoutubeCommentsExtractor.getYoutubeText(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
public String getPublishedTime() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
2018-10-19 16:03:36 +02:00
return YoutubeCommentsExtractor.getYoutubeText(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
}
@Override
public Integer getLikeCount() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
2018-10-19 16:03:36 +02:00
return JsonUtils.getNumber(json, "likeCount").intValue();
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 {
2019-02-28 19:40:29 +01:00
String commentText = YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "contentText"));
// 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 getAuthorThumbnail() 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 String getAuthorName() throws ParsingException {
2018-09-26 00:51:58 +02:00
try {
2018-10-19 16:03:36 +02:00
return YoutubeCommentsExtractor.getYoutubeText(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
public String getAuthorEndpoint() 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
}
}