2018-05-08 21:19:03 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2020-02-23 14:19:13 +01:00
|
|
|
import com.grack.nanojson.JsonArray;
|
2020-02-17 20:24:48 +01:00
|
|
|
import com.grack.nanojson.JsonObject;
|
2017-03-01 18:47:52 +01:00
|
|
|
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;
|
2020-04-10 10:51:05 +02:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
|
2020-02-22 20:19:41 +01:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory;
|
2017-06-29 20:12:55 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor;
|
2017-07-11 05:08:03 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamType;
|
2021-09-01 20:40:00 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.JsonUtils;
|
2017-07-11 05:08:03 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2019-10-02 07:02:01 +02:00
|
|
|
import javax.annotation.Nullable;
|
2020-10-18 05:48:14 +02:00
|
|
|
import java.time.Instant;
|
|
|
|
import java.time.OffsetDateTime;
|
|
|
|
import java.time.ZoneOffset;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
2021-02-07 22:42:21 +01:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*;
|
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
|
2020-04-15 18:49:58 +02:00
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
2020-02-27 17:39:23 +01:00
|
|
|
|
2017-06-29 20:12:55 +02:00
|
|
|
/*
|
2017-03-01 18:47:52 +01:00
|
|
|
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
|
|
|
|
* YoutubeStreamInfoItemExtractor.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
|
2021-09-01 20:40:00 +02:00
|
|
|
private final JsonObject videoInfo;
|
2019-10-02 07:02:01 +02:00
|
|
|
private final TimeAgoParser timeAgoParser;
|
2020-03-07 20:46:00 +01:00
|
|
|
private StreamType cachedStreamType;
|
2019-10-02 07:02:01 +02:00
|
|
|
|
2020-02-17 20:24:48 +01:00
|
|
|
/**
|
|
|
|
* Creates an extractor of StreamInfoItems from a YouTube page.
|
|
|
|
*
|
|
|
|
* @param videoInfoItem The JSON page element
|
|
|
|
* @param timeAgoParser A parser of the textual dates or {@code null}.
|
|
|
|
*/
|
|
|
|
public YoutubeStreamInfoItemExtractor(JsonObject videoInfoItem, @Nullable TimeAgoParser timeAgoParser) {
|
2020-02-22 20:19:41 +01:00
|
|
|
this.videoInfo = videoInfoItem;
|
2020-02-17 20:24:48 +01:00
|
|
|
this.timeAgoParser = timeAgoParser;
|
|
|
|
}
|
|
|
|
|
2017-08-10 19:50:59 +02:00
|
|
|
@Override
|
2020-02-22 20:19:41 +01:00
|
|
|
public StreamType getStreamType() {
|
2020-03-07 20:46:00 +01:00
|
|
|
if (cachedStreamType != null) {
|
|
|
|
return cachedStreamType;
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:08:14 +02:00
|
|
|
final JsonArray badges = videoInfo.getArray("badges");
|
2021-06-23 11:15:40 +02:00
|
|
|
for (final Object badge : badges) {
|
2021-06-23 11:25:59 +02:00
|
|
|
final JsonObject badgeRenderer = ((JsonObject) badge).getObject("metadataBadgeRenderer");
|
|
|
|
if (badgeRenderer.getString("style", EMPTY_STRING).equals("BADGE_STYLE_TYPE_LIVE_NOW") ||
|
|
|
|
badgeRenderer.getString("label", EMPTY_STRING).equals("LIVE NOW")) {
|
2020-03-07 20:46:00 +01:00
|
|
|
return cachedStreamType = StreamType.LIVE_STREAM;
|
2020-03-07 20:42:00 +01:00
|
|
|
}
|
2020-04-16 16:08:14 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 11:15:40 +02:00
|
|
|
for (final Object overlay : videoInfo.getArray("thumbnailOverlays")) {
|
|
|
|
final String style = ((JsonObject) overlay)
|
|
|
|
.getObject("thumbnailOverlayTimeStatusRenderer").getString("style", EMPTY_STRING);
|
|
|
|
if (style.equalsIgnoreCase("LIVE")) {
|
|
|
|
return cachedStreamType = StreamType.LIVE_STREAM;
|
|
|
|
}
|
2020-04-16 16:08:14 +02:00
|
|
|
}
|
2020-03-07 20:42:00 +01:00
|
|
|
|
2020-03-07 20:46:00 +01:00
|
|
|
return cachedStreamType = StreamType.VIDEO_STREAM;
|
2017-08-10 19:50:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-02-24 12:55:51 +01:00
|
|
|
public boolean isAd() throws ParsingException {
|
|
|
|
return isPremium() || getName().equals("[Private video]") || getName().equals("[Deleted video]");
|
2017-08-10 19:50:59 +02:00
|
|
|
}
|
|
|
|
|
2017-03-01 18:47:52 +01:00
|
|
|
@Override
|
2017-08-11 20:21:49 +02:00
|
|
|
public String getUrl() throws ParsingException {
|
2017-03-01 18:47:52 +01:00
|
|
|
try {
|
2020-02-22 20:19:41 +01:00
|
|
|
String videoId = videoInfo.getString("videoId");
|
|
|
|
return YoutubeStreamLinkHandlerFactory.getInstance().getUrl(videoId);
|
2017-03-01 18:47:52 +01:00
|
|
|
} catch (Exception e) {
|
2020-02-22 20:19:41 +01:00
|
|
|
throw new ParsingException("Could not get url", e);
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-11 20:21:49 +02:00
|
|
|
public String getName() throws ParsingException {
|
2020-02-27 17:39:23 +01:00
|
|
|
String name = getTextFromObject(videoInfo.getObject("title"));
|
2020-04-15 18:49:58 +02:00
|
|
|
if (!isNullOrEmpty(name)) return name;
|
2020-02-22 20:19:41 +01:00
|
|
|
throw new ParsingException("Could not get name");
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-11 20:21:49 +02:00
|
|
|
public long getDuration() throws ParsingException {
|
2020-02-29 22:19:34 +01:00
|
|
|
if (getStreamType() == StreamType.LIVE_STREAM || isPremiere()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-04-16 16:08:14 +02:00
|
|
|
String duration = getTextFromObject(videoInfo.getObject("lengthText"));
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-05-11 11:40:24 +02:00
|
|
|
if (isNullOrEmpty(duration)) {
|
2020-04-16 16:08:14 +02:00
|
|
|
for (Object thumbnailOverlay : videoInfo.getArray("thumbnailOverlays")) {
|
|
|
|
if (((JsonObject) thumbnailOverlay).has("thumbnailOverlayTimeStatusRenderer")) {
|
|
|
|
duration = getTextFromObject(((JsonObject) thumbnailOverlay)
|
|
|
|
.getObject("thumbnailOverlayTimeStatusRenderer").getObject("text"));
|
2020-02-26 09:31:26 +01:00
|
|
|
}
|
2020-04-16 16:08:14 +02:00
|
|
|
}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-05-11 11:40:24 +02:00
|
|
|
if (isNullOrEmpty(duration)) throw new ParsingException("Could not get duration");
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
|
|
|
return YoutubeParsingHelper.parseDurationString(duration);
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-10 04:50:29 +02:00
|
|
|
public String getUploaderName() throws ParsingException {
|
2020-04-16 16:08:14 +02:00
|
|
|
String name = getTextFromObject(videoInfo.getObject("longBylineText"));
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-05-11 11:40:24 +02:00
|
|
|
if (isNullOrEmpty(name)) {
|
2020-04-16 16:08:14 +02:00
|
|
|
name = getTextFromObject(videoInfo.getObject("ownerText"));
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-05-11 11:40:24 +02:00
|
|
|
if (isNullOrEmpty(name)) {
|
2020-04-16 16:08:14 +02:00
|
|
|
name = getTextFromObject(videoInfo.getObject("shortBylineText"));
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-05-11 11:40:24 +02:00
|
|
|
if (isNullOrEmpty(name)) throw new ParsingException("Could not get uploader name");
|
2020-02-29 17:18:50 +01:00
|
|
|
}
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
|
|
|
return name;
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
2017-11-08 10:17:44 +01:00
|
|
|
@Override
|
|
|
|
public String getUploaderUrl() throws ParsingException {
|
2020-04-16 16:08:14 +02:00
|
|
|
String url = getUrlFromNavigationEndpoint(videoInfo.getObject("longBylineText")
|
|
|
|
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-05-11 11:40:24 +02:00
|
|
|
if (isNullOrEmpty(url)) {
|
2020-04-16 16:08:14 +02:00
|
|
|
url = getUrlFromNavigationEndpoint(videoInfo.getObject("ownerText")
|
2020-02-29 17:18:50 +01:00
|
|
|
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
|
|
|
|
|
2020-05-11 11:40:24 +02:00
|
|
|
if (isNullOrEmpty(url)) {
|
2020-04-16 16:08:14 +02:00
|
|
|
url = getUrlFromNavigationEndpoint(videoInfo.getObject("shortBylineText")
|
2020-02-27 17:39:23 +01:00
|
|
|
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-05-11 11:40:24 +02:00
|
|
|
if (isNullOrEmpty(url)) throw new ParsingException("Could not get uploader url");
|
2020-02-23 13:48:54 +01:00
|
|
|
}
|
2017-11-08 10:17:44 +01:00
|
|
|
}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
|
|
|
return url;
|
2017-11-08 10:17:44 +01:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:40:00 +02:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public String getUploaderAvatarUrl() throws ParsingException {
|
|
|
|
|
2021-09-03 19:51:01 +02:00
|
|
|
if (videoInfo.has("channelThumbnailSupportedRenderers")) {
|
2021-09-01 21:05:56 +02:00
|
|
|
return JsonUtils.getArray(videoInfo, "channelThumbnailSupportedRenderers.channelThumbnailWithLinkRenderer.thumbnail.thumbnails")
|
|
|
|
.getObject(0).getString("url");
|
2021-09-01 20:40:00 +02:00
|
|
|
}
|
|
|
|
|
2021-09-03 19:51:01 +02:00
|
|
|
if (videoInfo.has("channelThumbnail")) {
|
|
|
|
return JsonUtils.getArray(videoInfo, "channelThumbnail.thumbnails")
|
|
|
|
.getObject(0).getString("url");
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:40:00 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:44:58 +01:00
|
|
|
@Override
|
|
|
|
public boolean isUploaderVerified() throws ParsingException {
|
|
|
|
return YoutubeParsingHelper.isVerified(videoInfo.getArray("ownerBadges"));
|
|
|
|
}
|
|
|
|
|
2019-11-03 19:45:25 +01:00
|
|
|
@Nullable
|
2017-03-01 18:47:52 +01:00
|
|
|
@Override
|
2020-02-29 22:19:34 +01:00
|
|
|
public String getTextualUploadDate() throws ParsingException {
|
|
|
|
if (getStreamType().equals(StreamType.LIVE_STREAM)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPremiere()) {
|
2020-10-18 05:48:14 +02:00
|
|
|
return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(getDateFromPremiere());
|
2020-02-29 22:19:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 16:08:14 +02:00
|
|
|
final String publishedTimeText = getTextFromObject(videoInfo.getObject("publishedTimeText"));
|
2020-05-01 13:55:15 +02:00
|
|
|
if (publishedTimeText != null && !publishedTimeText.isEmpty()) return publishedTimeText;
|
2020-04-16 16:08:14 +02:00
|
|
|
|
|
|
|
return null;
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:45:25 +01:00
|
|
|
@Nullable
|
2019-10-02 07:02:01 +02:00
|
|
|
@Override
|
2020-02-25 10:38:54 +01:00
|
|
|
public DateWrapper getUploadDate() throws ParsingException {
|
2020-02-29 22:19:34 +01:00
|
|
|
if (getStreamType().equals(StreamType.LIVE_STREAM)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPremiere()) {
|
|
|
|
return new DateWrapper(getDateFromPremiere());
|
|
|
|
}
|
|
|
|
|
|
|
|
final String textualUploadDate = getTextualUploadDate();
|
2020-04-15 18:49:58 +02:00
|
|
|
if (timeAgoParser != null && !isNullOrEmpty(textualUploadDate)) {
|
2020-02-25 10:38:54 +01:00
|
|
|
try {
|
|
|
|
return timeAgoParser.parse(textualUploadDate);
|
|
|
|
} catch (ParsingException e) {
|
|
|
|
throw new ParsingException("Could not get upload date", e);
|
|
|
|
}
|
|
|
|
}
|
2020-02-22 20:19:41 +01:00
|
|
|
return null;
|
2019-10-02 07:02:01 +02:00
|
|
|
}
|
|
|
|
|
2017-03-01 18:47:52 +01:00
|
|
|
@Override
|
|
|
|
public long getViewCount() throws ParsingException {
|
|
|
|
try {
|
2020-04-16 16:08:14 +02:00
|
|
|
if (videoInfo.has("topStandaloneBadge") || isPremium()) {
|
2020-02-23 14:19:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2020-02-29 22:01:43 +01:00
|
|
|
|
2020-04-16 16:08:14 +02:00
|
|
|
if (!videoInfo.has("viewCountText")) {
|
2020-02-29 22:01:43 +01:00
|
|
|
// This object is null when a video has its views hidden.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:08:14 +02:00
|
|
|
final String viewCount = getTextFromObject(videoInfo.getObject("viewCountText"));
|
2020-02-29 21:28:38 +01:00
|
|
|
|
2020-02-29 22:02:00 +01:00
|
|
|
if (viewCount.toLowerCase().contains("no views")) {
|
|
|
|
return 0;
|
|
|
|
} else if (viewCount.toLowerCase().contains("recommended")) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-02-22 20:19:41 +01:00
|
|
|
return Long.parseLong(Utils.removeNonDigitCharacters(viewCount));
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get view count", e);
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getThumbnailUrl() throws ParsingException {
|
2022-02-02 20:23:11 +01:00
|
|
|
return getThumbnailUrlFromInfoItem(videoInfo);
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
2020-02-23 14:19:13 +01:00
|
|
|
|
|
|
|
private boolean isPremium() {
|
2020-04-16 16:08:14 +02:00
|
|
|
JsonArray badges = videoInfo.getArray("badges");
|
|
|
|
for (Object badge : badges) {
|
|
|
|
if (((JsonObject) badge).getObject("metadataBadgeRenderer").getString("label", EMPTY_STRING).equals("Premium")) {
|
|
|
|
return true;
|
2020-02-23 14:19:13 +01:00
|
|
|
}
|
2020-02-29 22:19:34 +01:00
|
|
|
}
|
2020-02-23 14:19:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-29 22:19:34 +01:00
|
|
|
|
|
|
|
private boolean isPremiere() {
|
|
|
|
return videoInfo.has("upcomingEventData");
|
|
|
|
}
|
|
|
|
|
2020-10-18 05:48:14 +02:00
|
|
|
private OffsetDateTime getDateFromPremiere() throws ParsingException {
|
2020-02-29 22:19:34 +01:00
|
|
|
final JsonObject upcomingEventData = videoInfo.getObject("upcomingEventData");
|
|
|
|
final String startTime = upcomingEventData.getString("startTime");
|
|
|
|
|
|
|
|
try {
|
2020-10-18 05:48:14 +02:00
|
|
|
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(startTime)),
|
|
|
|
ZoneOffset.UTC);
|
2020-02-29 22:19:34 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not parse date from premiere: \"" + startTime + "\"");
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 17:19:44 +02:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public String getShortDescription() throws ParsingException {
|
|
|
|
|
2021-10-04 02:28:07 +02:00
|
|
|
if (videoInfo.has("detailedMetadataSnippets")) {
|
2021-09-22 17:19:44 +02:00
|
|
|
return getTextFromObject(videoInfo.getArray("detailedMetadataSnippets").getObject(0).getObject("snippetText"));
|
2021-10-04 02:28:07 +02:00
|
|
|
}
|
2021-09-22 17:19:44 +02:00
|
|
|
|
2021-10-04 02:28:07 +02:00
|
|
|
if (videoInfo.has("descriptionSnippet")) {
|
|
|
|
return getTextFromObject(videoInfo.getObject("descriptionSnippet"));
|
|
|
|
}
|
2021-09-22 17:19:44 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|