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;
|
2020-02-23 13:48:54 +01:00
|
|
|
|
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;
|
2018-07-13 18:02:40 +02:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.linkHandler.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;
|
|
|
|
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-02-28 09:36:33 +01:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
|
2020-02-27 17:39:23 +01:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
|
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
|
|
|
|
|
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 {
|
|
|
|
|
2020-02-22 20:19:41 +01:00
|
|
|
private JsonObject videoInfo;
|
2019-10-02 07:02:01 +02:00
|
|
|
private final TimeAgoParser timeAgoParser;
|
|
|
|
|
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() {
|
|
|
|
try {
|
|
|
|
if (videoInfo.getArray("badges").getObject(0).getObject("metadataBadgeRenderer").getString("label").equals("LIVE NOW")) {
|
|
|
|
return StreamType.LIVE_STREAM;
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
return 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-02-22 20:19:41 +01:00
|
|
|
if (name != null && !name.isEmpty()) return name;
|
|
|
|
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-26 09:31:26 +01:00
|
|
|
if (getStreamType() == StreamType.LIVE_STREAM) return -1;
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-02-26 09:31:26 +01:00
|
|
|
String duration = null;
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2017-03-01 18:47:52 +01:00
|
|
|
try {
|
2020-02-27 17:39:23 +01:00
|
|
|
duration = getTextFromObject(videoInfo.getObject("lengthText"));
|
2020-02-26 09:31:26 +01:00
|
|
|
} catch (Exception ignored) {}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-02-26 09:31:26 +01:00
|
|
|
if (duration == null) {
|
|
|
|
try {
|
|
|
|
for (Object thumbnailOverlay : videoInfo.getArray("thumbnailOverlays")) {
|
|
|
|
if (((JsonObject) thumbnailOverlay).getObject("thumbnailOverlayTimeStatusRenderer") != null) {
|
2020-02-27 17:39:23 +01:00
|
|
|
duration = getTextFromObject(((JsonObject) thumbnailOverlay)
|
|
|
|
.getObject("thumbnailOverlayTimeStatusRenderer").getObject("text"));
|
2020-02-26 09:31:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) {}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
|
|
|
if (duration == null) 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-02-22 20:33:05 +01:00
|
|
|
String name = null;
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2017-03-01 18:47:52 +01:00
|
|
|
try {
|
2020-02-27 17:39:23 +01:00
|
|
|
name = getTextFromObject(videoInfo.getObject("longBylineText"));
|
2020-02-22 20:33:05 +01:00
|
|
|
} catch (Exception ignored) {}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-02-22 20:33:05 +01:00
|
|
|
if (name == null) {
|
|
|
|
try {
|
2020-02-27 17:39:23 +01:00
|
|
|
name = getTextFromObject(videoInfo.getObject("ownerText"));
|
2020-02-22 20:33:05 +01:00
|
|
|
} catch (Exception ignored) {}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
|
|
|
if (name == null) {
|
|
|
|
try {
|
|
|
|
name = getTextFromObject(videoInfo.getObject("shortBylineText"));
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
|
|
|
|
if (name == null) throw new ParsingException("Could not get uploader name");
|
|
|
|
}
|
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-02-29 17:18:50 +01:00
|
|
|
String url = null;
|
|
|
|
|
2017-11-08 10:17:44 +01:00
|
|
|
try {
|
2020-02-29 17:18:50 +01:00
|
|
|
url = getUrlFromNavigationEndpoint(videoInfo.getObject("longBylineText")
|
|
|
|
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
|
|
|
|
if (url == null) {
|
2020-02-22 20:33:05 +01:00
|
|
|
try {
|
2020-02-29 17:18:50 +01:00
|
|
|
url = getUrlFromNavigationEndpoint(videoInfo.getObject("ownerText")
|
2020-02-27 17:39:23 +01:00
|
|
|
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
|
2020-02-22 20:33:05 +01:00
|
|
|
} catch (Exception ignored) {}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
2020-02-27 17:39:23 +01:00
|
|
|
if (url == null) {
|
2020-02-23 13:48:54 +01:00
|
|
|
try {
|
2020-02-27 17:39:23 +01:00
|
|
|
url = getUrlFromNavigationEndpoint(videoInfo.getObject("shortBylineText")
|
|
|
|
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
|
2020-02-23 13:48:54 +01:00
|
|
|
} catch (Exception ignored) {}
|
2020-02-29 17:18:50 +01:00
|
|
|
|
|
|
|
if (url == null) 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
|
|
|
}
|
|
|
|
|
2019-11-03 19:45:25 +01:00
|
|
|
@Nullable
|
2017-03-01 18:47:52 +01:00
|
|
|
@Override
|
2020-02-22 20:19:41 +01:00
|
|
|
public String getTextualUploadDate() {
|
2020-02-25 10:38:54 +01:00
|
|
|
try {
|
2020-02-27 17:39:23 +01:00
|
|
|
return getTextFromObject(videoInfo.getObject("publishedTimeText"));
|
2020-02-25 10:38:54 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
// upload date is not always available, e.g. in playlists
|
|
|
|
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 {
|
|
|
|
String textualUploadDate = getTextualUploadDate();
|
|
|
|
if (timeAgoParser != null && textualUploadDate != null && !textualUploadDate.isEmpty()) {
|
|
|
|
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-02-23 14:19:13 +01:00
|
|
|
if (videoInfo.getObject("topStandaloneBadge") != null || isPremium()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-02-27 17:39:23 +01:00
|
|
|
String viewCount = getTextFromObject(videoInfo.getObject("viewCountText"));
|
2020-02-29 21:28:38 +01:00
|
|
|
|
2020-02-22 20:19:41 +01:00
|
|
|
return Long.parseLong(Utils.removeNonDigitCharacters(viewCount));
|
2020-02-29 21:28:38 +01:00
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return -1;
|
2020-02-22 20:19:41 +01:00
|
|
|
} 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 {
|
|
|
|
try {
|
2020-02-22 20:19:41 +01:00
|
|
|
// TODO: Don't simply get the first item, but look at all thumbnails and their resolution
|
2020-02-27 19:08:46 +01:00
|
|
|
String url = videoInfo.getObject("thumbnail").getArray("thumbnails")
|
2020-02-22 20:19:41 +01:00
|
|
|
.getObject(0).getString("url");
|
2020-02-27 19:08:46 +01:00
|
|
|
|
2020-02-28 09:36:33 +01:00
|
|
|
return fixThumbnailUrl(url);
|
2017-03-01 18:47:52 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get thumbnail url", e);
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 14:19:13 +01:00
|
|
|
|
|
|
|
private boolean isPremium() {
|
|
|
|
try {
|
|
|
|
JsonArray badges = videoInfo.getArray("badges");
|
|
|
|
for (Object badge : badges) {
|
|
|
|
if (((JsonObject) badge).getObject("metadataBadgeRenderer").getString("label").equals("Premium")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|