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

213 lines
8.0 KiB
Java
Raw Normal View History

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;
import org.schabi.newpipe.extractor.localization.DateWrapper;
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory;
import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor;
import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.utils.Utils;
2017-03-01 18:47:52 +01:00
import javax.annotation.Nullable;
/*
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 {
private JsonObject videoInfo;
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) {
this.videoInfo = videoInfoItem;
2020-02-17 20:24:48 +01:00
this.timeAgoParser = timeAgoParser;
}
@Override
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;
}
@Override
public boolean isAd() {
2020-02-23 14:19:13 +01:00
return isPremium();
}
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 {
String videoId = videoInfo.getString("videoId");
return YoutubeStreamLinkHandlerFactory.getInstance().getUrl(videoId);
2017-03-01 18:47:52 +01:00
} catch (Exception e) {
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 {
String name = null;
2017-03-01 18:47:52 +01:00
try {
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
}
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 {
if (getStreamType() == StreamType.LIVE_STREAM) return -1;
return YoutubeParsingHelper.parseDurationString(videoInfo.getObject("lengthText").getString("simpleText"));
} catch (Exception e) {
throw new ParsingException("Could not get duration", e);
2017-03-01 18:47:52 +01:00
}
}
@Override
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")
.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) {}
}
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) {
throw new ParsingException("Could not get uploader url");
2017-11-08 10:17:44 +01:00
}
}
@Nullable
2017-03-01 18:47:52 +01:00
@Override
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
}
@Nullable
@Override
public DateWrapper getUploadDate() {
return null;
}
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;
}
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
}
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 {
// 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
}