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

334 lines
13 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 11:23:33 +01:00
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import org.schabi.newpipe.extractor.StreamingService;
2017-08-11 03:23:09 +02:00
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException;
2017-03-01 18:47:52 +01:00
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
2020-02-29 16:26:28 +01:00
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
2018-03-01 01:02:43 +01:00
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
2018-02-24 22:20:50 +01:00
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.Utils;
2017-04-21 23:31:40 +02:00
2020-02-23 11:23:33 +01:00
import javax.annotation.Nonnull;
import java.io.IOException;
2020-02-23 11:23:33 +01:00
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*;
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
2020-04-15 18:49:58 +02:00
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2020-02-17 11:02:43 +01:00
/*
2017-03-01 18:47:52 +01:00
* Created by Christian Schabesberger on 25.07.16.
*
2018-07-01 16:21:40 +02:00
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
2017-08-11 03:23:09 +02:00
* YoutubeChannelExtractor.java is part of NewPipe.
2017-03-01 18:47:52 +01:00
*
* 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/>.
*/
@SuppressWarnings("WeakerAccess")
2017-08-11 03:23:09 +02:00
public class YoutubeChannelExtractor extends ChannelExtractor {
private JsonObject initialData;
2020-02-28 17:05:31 +01:00
private JsonObject videoTab;
2017-09-26 22:46:21 +02:00
/**
* Some channels have response redirects and the only way to reliably get the id is by saving it.
* <p>
* "Movies & Shows":
* <pre>
* UCuJcl0Ju-gPDoksRjK1ya-w
* UChBfWrfBXL9wS6tQtgjt_OQ UClgRkhTL3_hImCAmdLfDE4g
* UCok7UTQQEP1Rsctxiv3gwSQ
* </pre>
*/
private String redirectedChannelId;
public YoutubeChannelExtractor(StreamingService service, ListLinkHandler linkHandler) {
super(service, linkHandler);
2017-08-06 22:20:15 +02:00
}
@Override
2017-11-28 13:37:01 +01:00
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
String url = super.getUrl() + "/videos?pbj=1&view=0&flow=grid";
JsonArray ajaxJson = null;
int level = 0;
while (level < 3) {
final JsonArray jsonResponse = getJsonResponse(url, getExtractorLocalization());
2020-04-16 16:08:14 +02:00
final JsonObject endpoint = jsonResponse.getObject(1).getObject("response")
.getArray("onResponseReceivedActions").getObject(0).getObject("navigateAction")
.getObject("endpoint");
2020-04-16 16:08:14 +02:00
final String webPageType = endpoint.getObject("commandMetadata").getObject("webCommandMetadata")
.getString("webPageType", EMPTY_STRING);
2020-04-16 16:08:14 +02:00
final String browseId = endpoint.getObject("browseEndpoint").getString("browseId", EMPTY_STRING);
if (webPageType.equalsIgnoreCase("WEB_PAGE_TYPE_BROWSE") && !browseId.isEmpty()) {
if (!browseId.startsWith("UC")) {
throw new ExtractionException("Redirected id is not pointing to a channel");
}
url = "https://www.youtube.com/channel/" + browseId + "/videos?pbj=1&view=0&flow=grid";
redirectedChannelId = browseId;
level++;
} else {
ajaxJson = jsonResponse;
break;
}
}
if (ajaxJson == null) {
throw new ExtractionException("Could not fetch initial JSON data");
}
2020-02-26 09:31:26 +01:00
initialData = ajaxJson.getObject(1).getObject("response");
YoutubeParsingHelper.defaultAlertsCheck(initialData);
2020-02-17 09:55:06 +01:00
}
@Nonnull
@Override
public String getUrl() throws ParsingException {
try {
2020-02-29 16:26:28 +01:00
return YoutubeChannelLinkHandlerFactory.getInstance().getUrl("channel/" + getId());
} catch (ParsingException e) {
return super.getUrl();
}
}
@Nonnull
@Override
2017-08-11 03:23:09 +02:00
public String getId() throws ParsingException {
2020-04-16 16:08:14 +02:00
final String channelId = initialData.getObject("header").getObject("c4TabbedHeaderRenderer")
.getString("channelId", EMPTY_STRING);
if (!channelId.isEmpty()) {
return channelId;
2020-04-15 18:49:58 +02:00
} else if (!isNullOrEmpty(redirectedChannelId)) {
return redirectedChannelId;
} else {
throw new ParsingException("Could not get channel id");
2017-03-01 18:47:52 +01:00
}
}
@Nonnull
2017-03-01 18:47:52 +01:00
@Override
2017-08-11 03:23:09 +02:00
public String getName() throws ParsingException {
2017-03-01 18:47:52 +01:00
try {
return initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getString("title");
} catch (Exception e) {
throw new ParsingException("Could not get channel name", e);
2017-03-01 18:47:52 +01:00
}
}
@Override
public String getAvatarUrl() throws ParsingException {
try {
2020-02-27 17:39:23 +01:00
String url = initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getObject("avatar")
.getArray("thumbnails").getObject(0).getString("url");
2020-02-27 17:39:23 +01:00
return fixThumbnailUrl(url);
} catch (Exception e) {
2017-03-01 18:47:52 +01:00
throw new ParsingException("Could not get avatar", e);
}
}
@Override
public String getBannerUrl() throws ParsingException {
try {
2020-04-16 16:08:14 +02:00
String url = initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getObject("banner")
.getArray("thumbnails").getObject(0).getString("url");
2020-04-16 16:08:14 +02:00
2020-02-24 13:01:13 +01:00
if (url == null || url.contains("s.ytimg.com") || url.contains("default_banner")) {
2020-02-17 11:02:43 +01:00
return null;
}
2020-02-27 19:08:46 +01:00
return fixThumbnailUrl(url);
} catch (Exception e) {
throw new ParsingException("Could not get banner", e);
2017-03-01 18:47:52 +01:00
}
}
2017-08-11 03:23:09 +02:00
@Override
public String getFeedUrl() throws ParsingException {
try {
return YoutubeParsingHelper.getFeedUrlFrom(getId());
2017-08-11 03:23:09 +02:00
} catch (Exception e) {
throw new ParsingException("Could not get feed url", e);
}
}
@Override
public long getSubscriberCount() throws ParsingException {
2020-04-16 16:08:14 +02:00
final JsonObject c4TabbedHeaderRenderer = initialData.getObject("header").getObject("c4TabbedHeaderRenderer");
if (c4TabbedHeaderRenderer.has("subscriberCountText")) {
try {
2020-04-16 16:08:14 +02:00
return Utils.mixedNumberWordToLong(getTextFromObject(c4TabbedHeaderRenderer.getObject("subscriberCountText")));
} catch (NumberFormatException e) {
throw new ParsingException("Could not get subscriber count", e);
}
2017-08-06 22:20:15 +02:00
} else {
// If there's no subscribe button, the channel has the subscriber count disabled
2020-04-16 16:08:14 +02:00
if (c4TabbedHeaderRenderer.has("subscribeButton")) {
return 0;
2020-04-16 16:08:14 +02:00
} else {
return -1;
}
2017-03-01 18:47:52 +01:00
}
}
@Override
public String getDescription() throws ParsingException {
try {
return initialData.getObject("metadata").getObject("channelMetadataRenderer").getString("description");
} catch (Exception e) {
2017-08-11 03:23:09 +02:00
throw new ParsingException("Could not get channel description", e);
}
}
@Override
public String getParentChannelName() throws ParsingException {
return "";
}
@Override
public String getParentChannelUrl() throws ParsingException {
return "";
}
@Override
public String getParentChannelAvatarUrl() throws ParsingException {
return "";
}
2017-11-25 02:03:30 +01:00
@Nonnull
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
String nextPageUrl = null;
if (getVideoTab() != null) {
final JsonObject gridRenderer = getVideoTab().getObject("content").getObject("sectionListRenderer")
.getArray("contents").getObject(0).getObject("itemSectionRenderer")
.getArray("contents").getObject(0).getObject("gridRenderer");
collectStreamsFrom(collector, gridRenderer.getArray("items"));
nextPageUrl = getNextPageUrlFrom(gridRenderer.getArray("continuations"));
}
return new InfoItemsPage<>(collector, nextPageUrl);
2017-08-06 22:20:15 +02:00
}
@Override
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
if (isNullOrEmpty(pageUrl)) {
2018-03-01 01:02:43 +01:00
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
}
2018-03-01 01:02:43 +01:00
// Unfortunately, we have to fetch the page even if we are only getting next streams,
// as they don't deliver enough information on their own (the channel name, for example).
2020-02-24 15:51:08 +01:00
fetchPage();
2018-03-01 01:02:43 +01:00
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
2020-02-29 16:55:07 +01:00
final JsonArray ajaxJson = getJsonResponse(pageUrl, getExtractorLocalization());
2018-03-01 01:02:43 +01:00
JsonObject sectionListContinuation = ajaxJson.getObject(1).getObject("response")
2020-02-26 09:31:26 +01:00
.getObject("continuationContents").getObject("gridContinuation");
2018-03-01 01:02:43 +01:00
2020-02-26 09:31:26 +01:00
collectStreamsFrom(collector, sectionListContinuation.getArray("items"));
2018-02-26 15:55:27 +01:00
return new InfoItemsPage<>(collector, getNextPageUrlFrom(sectionListContinuation.getArray("continuations")));
}
private String getNextPageUrlFrom(JsonArray continuations) {
if (isNullOrEmpty(continuations)) {
2020-04-16 16:08:14 +02:00
return "";
}
JsonObject nextContinuationData = continuations.getObject(0).getObject("nextContinuationData");
String continuation = nextContinuationData.getString("continuation");
String clickTrackingParams = nextContinuationData.getString("clickTrackingParams");
return "https://www.youtube.com/browse_ajax?ctoken=" + continuation + "&continuation=" + continuation
+ "&itct=" + clickTrackingParams;
}
private void collectStreamsFrom(StreamInfoItemsCollector collector, JsonArray videos) throws ParsingException {
collector.reset();
final String uploaderName = getName();
final String uploaderUrl = getUrl();
final TimeAgoParser timeAgoParser = getTimeAgoParser();
2020-02-23 11:23:33 +01:00
for (Object video : videos) {
2020-04-16 16:08:14 +02:00
if (((JsonObject) video).has("gridVideoRenderer")) {
2020-02-26 09:31:26 +01:00
collector.commit(new YoutubeStreamInfoItemExtractor(
((JsonObject) video).getObject("gridVideoRenderer"), timeAgoParser) {
2017-03-01 18:47:52 +01:00
@Override
2020-02-23 11:23:33 +01:00
public String getUploaderName() {
return uploaderName;
2017-03-01 18:47:52 +01:00
}
2017-11-25 02:38:46 +01:00
@Override
2020-02-23 11:23:33 +01:00
public String getUploaderUrl() {
2017-11-25 02:38:46 +01:00
return uploaderUrl;
}
2017-03-01 18:47:52 +01:00
});
}
}
}
private JsonObject getVideoTab() throws ParsingException {
2020-02-28 17:05:31 +01:00
if (this.videoTab != null) return this.videoTab;
JsonArray tabs = initialData.getObject("contents").getObject("twoColumnBrowseResultsRenderer")
.getArray("tabs");
JsonObject videoTab = null;
for (Object tab : tabs) {
2020-04-16 16:08:14 +02:00
if (((JsonObject) tab).has("tabRenderer")) {
if (((JsonObject) tab).getObject("tabRenderer").getString("title", EMPTY_STRING).equals("Videos")) {
videoTab = ((JsonObject) tab).getObject("tabRenderer");
break;
}
}
}
if (videoTab == null) {
throw new ContentNotSupportedException("This channel has no Videos tab");
}
final String messageRendererText = getTextFromObject(videoTab.getObject("content")
.getObject("sectionListRenderer").getArray("contents").getObject(0)
.getObject("itemSectionRenderer").getArray("contents").getObject(0)
.getObject("messageRenderer").getObject("text"));
if (messageRendererText != null
&& messageRendererText.equals("This channel has no videos.")) {
2020-04-16 16:08:14 +02:00
return null;
2020-04-20 14:27:33 +02:00
}
2020-02-28 17:05:31 +01:00
this.videoTab = videoTab;
return videoTab;
}
2017-03-01 18:47:52 +01:00
}