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;
|
2017-08-16 04:40:03 +02:00
|
|
|
import com.grack.nanojson.JsonObject;
|
2021-03-04 21:38:23 +01:00
|
|
|
import com.grack.nanojson.JsonWriter;
|
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
import org.schabi.newpipe.extractor.Page;
|
2018-09-04 03:37:31 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2017-08-11 03:23:09 +02:00
|
|
|
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
2021-03-04 21:38:23 +01:00
|
|
|
import org.schabi.newpipe.extractor.downloader.Response;
|
2021-04-08 17:23:31 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
|
2020-04-01 22:31:53 +02:00
|
|
|
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;
|
2018-09-04 03:37:31 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
2021-04-30 19:06:56 +02:00
|
|
|
import org.schabi.newpipe.extractor.localization.Localization;
|
2019-04-28 22:03:16 +02: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-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;
|
2021-03-04 21:38:23 +01:00
|
|
|
import org.schabi.newpipe.extractor.utils.JsonUtils;
|
2017-07-11 05:08:03 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2017-04-21 23:31:40 +02:00
|
|
|
|
2020-10-25 20:29:47 +01:00
|
|
|
import java.io.IOException;
|
2021-05-30 17:23:51 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2020-05-11 15:25:18 +02:00
|
|
|
|
2021-04-02 21:42:22 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2021-05-30 17:23:51 +02:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*;
|
2021-02-07 22:42:21 +01:00
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
|
2021-03-04 21:38:23 +01:00
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
|
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-06-29 20:12:55 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
2017-06-29 20:12:55 +02:00
|
|
|
@SuppressWarnings("WeakerAccess")
|
2017-08-11 03:23:09 +02:00
|
|
|
public class YoutubeChannelExtractor extends ChannelExtractor {
|
2020-02-22 23:51:02 +01:00
|
|
|
private JsonObject initialData;
|
2020-02-28 17:05:31 +01:00
|
|
|
private JsonObject videoTab;
|
2017-09-26 22:46:21 +02:00
|
|
|
|
2020-03-14 06:34:29 +01:00
|
|
|
/**
|
|
|
|
* Some channels have response redirects and the only way to reliably get the id is by saving it.
|
2020-05-05 14:49:21 +02:00
|
|
|
* <p>
|
2020-03-14 06:34:29 +01:00
|
|
|
* "Movies & Shows":
|
|
|
|
* <pre>
|
|
|
|
* UCuJcl0Ju-gPDoksRjK1ya-w ┐
|
|
|
|
* UChBfWrfBXL9wS6tQtgjt_OQ ├ UClgRkhTL3_hImCAmdLfDE4g
|
|
|
|
* UCok7UTQQEP1Rsctxiv3gwSQ ┘
|
|
|
|
* </pre>
|
|
|
|
*/
|
|
|
|
private String redirectedChannelId;
|
|
|
|
|
2021-04-15 18:58:59 +02:00
|
|
|
public YoutubeChannelExtractor(final StreamingService service,
|
|
|
|
final ListLinkHandler linkHandler) {
|
2019-04-28 22:03:16 +02:00
|
|
|
super(service, linkHandler);
|
2017-08-06 22:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-15 18:58:59 +02:00
|
|
|
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException,
|
|
|
|
ExtractionException {
|
2021-04-08 17:23:31 +02:00
|
|
|
final String channel_path = super.getId();
|
|
|
|
final String[] channelInfo = channel_path.split("/");
|
|
|
|
String id = "";
|
2021-04-14 15:01:07 +02:00
|
|
|
// If the url is an URL which is not a /channel URL, we need to use the
|
|
|
|
// navigation/resolve_url endpoint of the youtubei API to get the channel id. Otherwise, we
|
|
|
|
// couldn't get information about the channel associated with this URL, if there is one.
|
|
|
|
if (!channelInfo[0].equals("channel")) {
|
2021-04-30 19:06:56 +02:00
|
|
|
final byte[] body = JsonWriter.string(prepareJsonBuilder(getExtractorLocalization(),
|
|
|
|
getExtractorContentCountry())
|
2021-04-08 17:23:31 +02:00
|
|
|
.value("url", "https://www.youtube.com/" + channel_path)
|
|
|
|
.done())
|
|
|
|
.getBytes(UTF_8);
|
|
|
|
|
|
|
|
final JsonObject jsonResponse = getJsonPostResponse("navigation/resolve_url",
|
|
|
|
body, getExtractorLocalization());
|
|
|
|
|
|
|
|
if (jsonResponse.has("error")) {
|
|
|
|
if (jsonResponse.getInt("code") == 404) {
|
|
|
|
throw new ContentNotAvailableException("No channel associated with this user"
|
|
|
|
+ "exists");
|
|
|
|
} else {
|
|
|
|
throw new ContentNotAvailableException("Got error:\""
|
|
|
|
+ jsonResponse.getString("status") + "\""
|
|
|
|
+ jsonResponse.getString("message"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final JsonObject endpoint = jsonResponse.getObject("endpoint");
|
|
|
|
|
|
|
|
final String webPageType = endpoint.getObject("commandMetadata")
|
|
|
|
.getObject("webCommandMetadata")
|
|
|
|
.getString("webPageType", EMPTY_STRING);
|
|
|
|
|
|
|
|
final JsonObject browseEndpoint = endpoint.getObject("browseEndpoint");
|
|
|
|
final String browseId = browseEndpoint.getString("browseId", EMPTY_STRING);
|
|
|
|
|
|
|
|
if (webPageType.equalsIgnoreCase("WEB_PAGE_TYPE_BROWSE")
|
|
|
|
|| webPageType.equalsIgnoreCase("WEB_PAGE_TYPE_CHANNEL")
|
|
|
|
&& !browseId.isEmpty()) {
|
|
|
|
if (!browseId.startsWith("UC")) {
|
|
|
|
throw new ExtractionException("Redirected id is not pointing to a channel");
|
|
|
|
}
|
|
|
|
|
|
|
|
id = browseId;
|
|
|
|
redirectedChannelId = browseId;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
id = channelInfo[1];
|
|
|
|
}
|
|
|
|
JsonObject ajaxJson = null;
|
2020-03-14 06:29:43 +01:00
|
|
|
|
|
|
|
int level = 0;
|
|
|
|
while (level < 3) {
|
2021-04-30 19:06:56 +02:00
|
|
|
final byte[] body = JsonWriter.string(prepareJsonBuilder(getExtractorLocalization(),
|
|
|
|
getExtractorContentCountry())
|
2021-04-08 17:23:31 +02:00
|
|
|
.value("browseId", id)
|
2021-05-29 14:43:50 +02:00
|
|
|
.value("params", "EgZ2aWRlb3M%3D") // Equal to videos
|
2021-04-08 17:23:31 +02:00
|
|
|
.done())
|
|
|
|
.getBytes(UTF_8);
|
|
|
|
|
|
|
|
final JsonObject jsonResponse = getJsonPostResponse("browse", body,
|
|
|
|
getExtractorLocalization());
|
|
|
|
|
|
|
|
if (!isNullOrEmpty(jsonResponse.getObject("error"))) {
|
|
|
|
final int errorCode = jsonResponse.getObject("error").getInt("code");
|
|
|
|
if (errorCode == 400) {
|
|
|
|
throw new ContentNotAvailableException("This channel doesn't exists");
|
|
|
|
} else {
|
|
|
|
throw new ContentNotAvailableException("Got error:\""
|
|
|
|
+ jsonResponse.getString("status") + "\""
|
|
|
|
+ jsonResponse.getString("message"));
|
|
|
|
}
|
|
|
|
}
|
2020-03-14 06:29:43 +01:00
|
|
|
|
2021-04-08 17:23:31 +02:00
|
|
|
final JsonObject endpoint = jsonResponse.getArray("onResponseReceivedActions")
|
|
|
|
.getObject(0)
|
|
|
|
.getObject("navigateAction")
|
2020-04-16 16:08:14 +02:00
|
|
|
.getObject("endpoint");
|
2020-03-14 06:29:43 +01:00
|
|
|
|
2021-04-08 17:23:31 +02:00
|
|
|
final String webPageType = endpoint.getObject("commandMetadata")
|
|
|
|
.getObject("webCommandMetadata")
|
2020-03-14 06:29:43 +01:00
|
|
|
.getString("webPageType", EMPTY_STRING);
|
|
|
|
|
2021-04-08 17:23:31 +02:00
|
|
|
final String browseId = endpoint.getObject("browseEndpoint").getString("browseId",
|
|
|
|
EMPTY_STRING);
|
2020-03-14 06:29:43 +01:00
|
|
|
|
2020-10-25 20:29:47 +01:00
|
|
|
if (webPageType.equalsIgnoreCase("WEB_PAGE_TYPE_BROWSE")
|
2021-04-08 17:23:31 +02:00
|
|
|
|| webPageType.equalsIgnoreCase("WEB_PAGE_TYPE_CHANNEL")
|
|
|
|
&& !browseId.isEmpty()) {
|
2020-03-14 06:29:43 +01:00
|
|
|
if (!browseId.startsWith("UC")) {
|
|
|
|
throw new ExtractionException("Redirected id is not pointing to a channel");
|
|
|
|
}
|
|
|
|
|
2021-04-08 17:23:31 +02:00
|
|
|
id = browseId;
|
2020-03-14 06:34:29 +01:00
|
|
|
redirectedChannelId = browseId;
|
2020-03-14 06:29:43 +01:00
|
|
|
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
|
|
|
|
2021-04-08 17:23:31 +02:00
|
|
|
initialData = ajaxJson;
|
2020-03-01 01:52:25 +01:00
|
|
|
YoutubeParsingHelper.defaultAlertsCheck(initialData);
|
2020-02-17 09:55:06 +01:00
|
|
|
}
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2017-08-16 04:56:10 +02:00
|
|
|
@Override
|
2018-05-06 14:08:50 +02:00
|
|
|
public String getUrl() throws ParsingException {
|
2017-08-16 04:56:10 +02:00
|
|
|
try {
|
2020-02-29 16:26:28 +01:00
|
|
|
return YoutubeChannelLinkHandlerFactory.getInstance().getUrl("channel/" + getId());
|
2021-04-08 17:23:31 +02:00
|
|
|
} catch (final ParsingException e) {
|
2018-05-06 14:08:50 +02:00
|
|
|
return super.getUrl();
|
2017-08-16 04:56:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2017-06-29 20:12:55 +02:00
|
|
|
@Override
|
2017-08-11 03:23:09 +02:00
|
|
|
public String getId() throws ParsingException {
|
2021-04-08 17:23:31 +02:00
|
|
|
final String channelId = initialData.getObject("header")
|
|
|
|
.getObject("c4TabbedHeaderRenderer")
|
2020-04-16 16:08:14 +02:00
|
|
|
.getString("channelId", EMPTY_STRING);
|
2020-03-14 06:34:29 +01:00
|
|
|
|
|
|
|
if (!channelId.isEmpty()) {
|
|
|
|
return channelId;
|
2020-04-15 18:49:58 +02:00
|
|
|
} else if (!isNullOrEmpty(redirectedChannelId)) {
|
2020-03-14 06:34:29 +01:00
|
|
|
return redirectedChannelId;
|
|
|
|
} else {
|
|
|
|
throw new ParsingException("Could not get channel id");
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 01:10:04 +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 {
|
2021-04-08 17:23:31 +02:00
|
|
|
return initialData.getObject("header").getObject("c4TabbedHeaderRenderer")
|
|
|
|
.getString("title");
|
2021-04-08 16:17:59 +02:00
|
|
|
} catch (final Exception e) {
|
2017-09-09 21:42:42 +02:00
|
|
|
throw new ParsingException("Could not get channel name", e);
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getAvatarUrl() throws ParsingException {
|
|
|
|
try {
|
2021-04-08 17:23:31 +02:00
|
|
|
String url = initialData.getObject("header")
|
|
|
|
.getObject("c4TabbedHeaderRenderer").getObject("avatar").getArray("thumbnails")
|
|
|
|
.getObject(0).getString("url");
|
2020-02-27 17:39:23 +01:00
|
|
|
|
2020-02-28 09:36:33 +01:00
|
|
|
return fixThumbnailUrl(url);
|
2021-04-08 16:17:59 +02:00
|
|
|
} catch (final 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 {
|
2021-04-08 17:23:31 +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
|
|
|
|
2020-02-28 09:36:33 +01:00
|
|
|
return fixThumbnailUrl(url);
|
2021-04-08 16:17:59 +02:00
|
|
|
} catch (final Exception e) {
|
2020-02-25 16:32:08 +01:00
|
|
|
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 {
|
2019-12-16 08:35:44 +01:00
|
|
|
return YoutubeParsingHelper.getFeedUrlFrom(getId());
|
2021-04-08 16:17:59 +02:00
|
|
|
} catch (final Exception e) {
|
2017-08-11 03:23:09 +02:00
|
|
|
throw new ParsingException("Could not get feed url", e);
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 20:12:55 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getSubscriberCount() throws ParsingException {
|
2021-04-08 17:23:31 +02:00
|
|
|
final JsonObject c4TabbedHeaderRenderer = initialData.getObject("header")
|
|
|
|
.getObject("c4TabbedHeaderRenderer");
|
2020-04-16 16:08:14 +02:00
|
|
|
if (c4TabbedHeaderRenderer.has("subscriberCountText")) {
|
2018-09-04 03:37:31 +02:00
|
|
|
try {
|
2021-04-08 17:23:31 +02:00
|
|
|
return Utils.mixedNumberWordToLong(getTextFromObject(c4TabbedHeaderRenderer
|
|
|
|
.getObject("subscriberCountText")));
|
|
|
|
} catch (final NumberFormatException e) {
|
2018-09-04 03:37:31 +02:00
|
|
|
throw new ParsingException("Could not get subscriber count", e);
|
|
|
|
}
|
2017-08-06 22:20:15 +02:00
|
|
|
} else {
|
2020-10-25 20:29:47 +01:00
|
|
|
return ITEM_COUNT_UNKNOWN;
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 19:00:36 +02:00
|
|
|
@Override
|
|
|
|
public String getDescription() throws ParsingException {
|
|
|
|
try {
|
2021-04-08 17:23:31 +02:00
|
|
|
return initialData.getObject("metadata").getObject("channelMetadataRenderer")
|
|
|
|
.getString("description");
|
2021-04-08 16:17:59 +02:00
|
|
|
} catch (final Exception e) {
|
2017-08-11 03:23:09 +02:00
|
|
|
throw new ParsingException("Could not get channel description", e);
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:33:44 +02:00
|
|
|
@Override
|
2020-05-08 16:02:59 +02:00
|
|
|
public String getParentChannelName() throws ParsingException {
|
2020-04-13 22:33:44 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-08 16:02:59 +02:00
|
|
|
public String getParentChannelUrl() throws ParsingException {
|
2020-04-13 22:33:44 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-08 16:02:59 +02:00
|
|
|
public String getParentChannelAvatarUrl() throws ParsingException {
|
2020-04-13 22:33:44 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:44:58 +01:00
|
|
|
@Override
|
|
|
|
public boolean isVerified() throws ParsingException {
|
2021-04-08 17:23:31 +02:00
|
|
|
final JsonArray badges = initialData.getObject("header")
|
|
|
|
.getObject("c4TabbedHeaderRenderer")
|
2021-01-22 01:44:58 +01:00
|
|
|
.getArray("badges");
|
|
|
|
|
|
|
|
return YoutubeParsingHelper.isVerified(badges);
|
|
|
|
}
|
|
|
|
|
2017-11-25 02:03:30 +01:00
|
|
|
@Nonnull
|
2017-06-29 20:12:55 +02:00
|
|
|
@Override
|
2021-03-04 21:38:23 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
|
2020-04-10 10:25:53 +02:00
|
|
|
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
Page nextPage = null;
|
2020-02-24 15:48:23 +01:00
|
|
|
|
2020-02-27 09:21:25 +01:00
|
|
|
if (getVideoTab() != null) {
|
2021-04-08 17:23:31 +02:00
|
|
|
final JsonObject gridRenderer = getVideoTab().getObject("content")
|
|
|
|
.getObject("sectionListRenderer")
|
2020-04-10 10:25:53 +02:00
|
|
|
.getArray("contents").getObject(0).getObject("itemSectionRenderer")
|
|
|
|
.getArray("contents").getObject(0).getObject("gridRenderer");
|
|
|
|
|
2021-04-08 17:23:31 +02:00
|
|
|
final JsonObject continuation = collectStreamsFrom(collector, gridRenderer
|
|
|
|
.getArray("items"));
|
2020-04-10 10:25:53 +02:00
|
|
|
|
2021-02-12 14:39:09 +01:00
|
|
|
nextPage = getNextPageFrom(continuation);
|
2020-02-27 09:21:25 +01:00
|
|
|
}
|
2020-02-24 15:48:23 +01:00
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
return new InfoItemsPage<>(collector, nextPage);
|
2017-08-06 22:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-15 18:58:59 +02:00
|
|
|
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException,
|
|
|
|
ExtractionException {
|
2020-05-11 15:25:18 +02:00
|
|
|
if (page == null || isNullOrEmpty(page.getUrl())) {
|
|
|
|
throw new IllegalArgumentException("Page doesn't contain an URL");
|
|
|
|
}
|
|
|
|
|
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).
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2021-05-30 17:23:51 +02:00
|
|
|
if (!isPageFetched()) fetchPage();
|
|
|
|
|
|
|
|
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
|
|
|
final Map<String, List<String>> headers = new HashMap<>();
|
|
|
|
addClientInfoHeaders(headers);
|
|
|
|
|
2021-04-02 21:42:22 +02:00
|
|
|
final Response response = getDownloader().post(page.getUrl(), null, page.getBody(),
|
|
|
|
getExtractorLocalization());
|
2021-03-04 21:38:23 +01:00
|
|
|
|
|
|
|
final JsonObject ajaxJson = JsonUtils.toJsonObject(getValidJsonResponseBody(response));
|
2018-03-01 01:02:43 +01:00
|
|
|
|
2021-03-04 21:38:23 +01:00
|
|
|
JsonObject sectionListContinuation = ajaxJson.getArray("onResponseReceivedActions")
|
|
|
|
.getObject(0)
|
|
|
|
.getObject("appendContinuationItemsAction");
|
2018-03-01 01:02:43 +01:00
|
|
|
|
2021-04-08 17:23:31 +02:00
|
|
|
final JsonObject continuation = collectStreamsFrom(collector, sectionListContinuation
|
|
|
|
.getArray("continuationItems"));
|
2018-02-26 15:55:27 +01:00
|
|
|
|
2021-02-12 14:39:09 +01:00
|
|
|
return new InfoItemsPage<>(collector, getNextPageFrom(continuation));
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 18:58:59 +02:00
|
|
|
private Page getNextPageFrom(final JsonObject continuations) throws IOException,
|
|
|
|
ExtractionException {
|
2020-05-11 11:40:24 +02:00
|
|
|
if (isNullOrEmpty(continuations)) {
|
2020-04-15 14:09:46 +02:00
|
|
|
return null;
|
2020-04-16 16:08:14 +02:00
|
|
|
}
|
2020-02-24 16:45:36 +01:00
|
|
|
|
2021-02-12 14:39:09 +01:00
|
|
|
final JsonObject continuationEndpoint = continuations.getObject("continuationEndpoint");
|
2021-04-08 17:23:31 +02:00
|
|
|
final String continuation = continuationEndpoint.getObject("continuationCommand")
|
|
|
|
.getString("token");
|
2021-04-02 21:42:22 +02:00
|
|
|
|
2021-04-30 19:06:56 +02:00
|
|
|
final byte[] body = JsonWriter.string(prepareJsonBuilder(getExtractorLocalization(),
|
|
|
|
getExtractorContentCountry())
|
2021-04-02 21:42:22 +02:00
|
|
|
.value("continuation", continuation)
|
|
|
|
.done())
|
|
|
|
.getBytes(UTF_8);
|
|
|
|
|
2021-05-30 17:23:51 +02:00
|
|
|
return new Page(YOUTUBEI_V1_URL + "browse?key=" + getKey(), body);
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 14:39:09 +01:00
|
|
|
/**
|
|
|
|
* Collect streams from an array of items
|
|
|
|
*
|
|
|
|
* @param collector the collector where videos will be commited
|
|
|
|
* @param videos the array to get videos from
|
|
|
|
* @return the continuation object
|
|
|
|
* @throws ParsingException if an error happened while extracting
|
|
|
|
*/
|
2021-04-08 17:23:31 +02:00
|
|
|
private JsonObject collectStreamsFrom(final StreamInfoItemsCollector collector,
|
|
|
|
final JsonArray videos) throws ParsingException {
|
2017-11-11 12:17:14 +01:00
|
|
|
collector.reset();
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2017-09-11 15:19:16 +02:00
|
|
|
final String uploaderName = getName();
|
2018-05-06 14:08:50 +02:00
|
|
|
final String uploaderUrl = getUrl();
|
2019-04-28 22:03:16 +02:00
|
|
|
final TimeAgoParser timeAgoParser = getTimeAgoParser();
|
|
|
|
|
2021-02-12 14:39:09 +01:00
|
|
|
JsonObject continuation = null;
|
|
|
|
|
2021-04-08 16:17:59 +02:00
|
|
|
for (final Object object : videos) {
|
2021-02-12 14:39:09 +01:00
|
|
|
final JsonObject video = (JsonObject) object;
|
|
|
|
if (video.has("gridVideoRenderer")) {
|
2020-02-26 09:31:26 +01:00
|
|
|
collector.commit(new YoutubeStreamInfoItemExtractor(
|
2021-02-12 14:39:09 +01:00
|
|
|
video.getObject("gridVideoRenderer"), timeAgoParser) {
|
2017-03-01 18:47:52 +01:00
|
|
|
@Override
|
2020-02-23 11:23:33 +01:00
|
|
|
public String getUploaderName() {
|
2017-09-11 15:19:16 +02:00
|
|
|
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
|
|
|
});
|
2021-02-12 14:39:09 +01:00
|
|
|
} else if (video.has("continuationItemRenderer")) {
|
|
|
|
continuation = video.getObject("continuationItemRenderer");
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-12 14:39:09 +01:00
|
|
|
|
|
|
|
return continuation;
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
2020-02-24 15:48:23 +01:00
|
|
|
|
|
|
|
private JsonObject getVideoTab() throws ParsingException {
|
2020-02-28 17:05:31 +01:00
|
|
|
if (this.videoTab != null) return this.videoTab;
|
|
|
|
|
2021-04-08 17:23:31 +02:00
|
|
|
JsonArray tabs = initialData.getObject("contents")
|
|
|
|
.getObject("twoColumnBrowseResultsRenderer")
|
2020-02-24 15:48:23 +01:00
|
|
|
.getArray("tabs");
|
|
|
|
JsonObject videoTab = null;
|
|
|
|
|
2021-04-08 16:17:59 +02:00
|
|
|
for (final Object tab : tabs) {
|
2020-04-16 16:08:14 +02:00
|
|
|
if (((JsonObject) tab).has("tabRenderer")) {
|
2021-04-08 17:23:31 +02:00
|
|
|
if (((JsonObject) tab).getObject("tabRenderer").getString("title",
|
|
|
|
EMPTY_STRING).equals("Videos")) {
|
2020-02-24 15:48:23 +01:00
|
|
|
videoTab = ((JsonObject) tab).getObject("tabRenderer");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoTab == null) {
|
2020-04-01 22:31:53 +02:00
|
|
|
throw new ContentNotSupportedException("This channel has no Videos tab");
|
2020-02-24 15:48:23 +01:00
|
|
|
}
|
|
|
|
|
2020-05-01 13:55:15 +02:00
|
|
|
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-27 09:21:25 +01:00
|
|
|
|
2020-02-28 17:05:31 +01:00
|
|
|
this.videoTab = videoTab;
|
2020-02-24 15:48:23 +01:00
|
|
|
return videoTab;
|
|
|
|
}
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|