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;
|
2020-02-22 20:19:41 +01:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
|
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;
|
|
|
|
|
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-22 20:19:41 +01:00
|
|
|
String name = null;
|
2017-03-01 18:47:52 +01:00
|
|
|
try {
|
2020-02-22 20:19:41 +01:00
|
|
|
name = videoInfo.getObject("title").getString("simpleText");
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
if (name == null) {
|
|
|
|
try {
|
|
|
|
name = videoInfo.getObject("title").getArray("runs").getObject(0).getString("text");
|
|
|
|
} catch (Exception ignored) {}
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
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 {
|
2017-03-01 18:47:52 +01:00
|
|
|
try {
|
2017-08-10 19:50:59 +02:00
|
|
|
if (getStreamType() == StreamType.LIVE_STREAM) return -1;
|
2020-02-22 20:19:41 +01:00
|
|
|
return YoutubeParsingHelper.parseDurationString(videoInfo.getObject("lengthText").getString("simpleText"));
|
2017-06-29 20:12:55 +02:00
|
|
|
} catch (Exception e) {
|
2020-02-22 20:19:41 +01:00
|
|
|
throw new ParsingException("Could not get duration", e);
|
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;
|
2017-03-01 18:47:52 +01:00
|
|
|
try {
|
2020-02-22 20:33:05 +01:00
|
|
|
name = videoInfo.getObject("longBylineText").getArray("runs")
|
2020-02-22 20:19:41 +01:00
|
|
|
.getObject(0).getString("text");
|
2020-02-22 20:33:05 +01:00
|
|
|
} catch (Exception ignored) {}
|
|
|
|
if (name == null) {
|
|
|
|
try {
|
|
|
|
name = videoInfo.getObject("ownerText").getArray("runs")
|
|
|
|
.getObject(0).getString("text");
|
|
|
|
} catch (Exception ignored) {}
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
2020-02-23 13:48:54 +01:00
|
|
|
if (name == null) {
|
|
|
|
try {
|
|
|
|
name = videoInfo.getObject("shortBylineText").getArray("runs")
|
|
|
|
.getObject(0).getString("text");
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
}
|
2020-02-22 20:33:05 +01:00
|
|
|
if (name != null && !name.isEmpty()) return name;
|
|
|
|
throw new ParsingException("Could not get uploader name");
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
2017-11-08 10:17:44 +01:00
|
|
|
@Override
|
|
|
|
public String getUploaderUrl() throws ParsingException {
|
|
|
|
try {
|
2020-02-22 20:33:05 +01:00
|
|
|
String id = null;
|
|
|
|
try {
|
|
|
|
id = videoInfo.getObject("longBylineText").getArray("runs")
|
|
|
|
.getObject(0).getObject("navigationEndpoint")
|
|
|
|
.getObject("browseEndpoint").getString("browseId");
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
if (id == null) {
|
|
|
|
try {
|
|
|
|
id = videoInfo.getObject("ownerText").getArray("runs")
|
|
|
|
.getObject(0).getObject("navigationEndpoint")
|
|
|
|
.getObject("browseEndpoint").getString("browseId");
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
}
|
2020-02-23 13:48:54 +01:00
|
|
|
if (id == null) {
|
|
|
|
try {
|
|
|
|
id = videoInfo.getObject("shortBylineText").getArray("runs")
|
|
|
|
.getObject(0).getObject("navigationEndpoint")
|
|
|
|
.getObject("browseEndpoint").getString("browseId");
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
}
|
2020-02-22 20:19:41 +01:00
|
|
|
if (id == null || id.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("is empty");
|
|
|
|
}
|
|
|
|
return YoutubeChannelLinkHandlerFactory.getInstance().getUrl(id);
|
2017-11-08 10:17:44 +01:00
|
|
|
} catch (Exception e) {
|
2020-02-22 20:19:41 +01:00
|
|
|
throw new ParsingException("Could not get uploader 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() {
|
|
|
|
// TODO: Get upload date in case of a videoRenderer (not available in case of a compactVideoRenderer)
|
|
|
|
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-22 20:19:41 +01:00
|
|
|
public DateWrapper getUploadDate() {
|
|
|
|
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-22 20:19:41 +01:00
|
|
|
String viewCount;
|
|
|
|
if (getStreamType() == StreamType.LIVE_STREAM) {
|
|
|
|
viewCount = videoInfo.getObject("viewCountText")
|
|
|
|
.getArray("runs").getObject(0).getString("text");
|
|
|
|
} else {
|
|
|
|
viewCount = videoInfo.getObject("viewCountText").getString("simpleText");
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
2020-02-22 20:19:41 +01:00
|
|
|
if (viewCount.equals("Recommended for you")) return -1;
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
return videoInfo.getObject("thumbnail").getArray("thumbnails")
|
|
|
|
.getObject(0).getString("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
|
|
|
}
|