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;
|
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
2020-02-23 11:23:33 +01:00
|
|
|
|
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;
|
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;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
|
2019-10-29 06:00:29 +01:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
|
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;
|
2017-07-11 05:08:03 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2017-04-21 23:31:40 +02:00
|
|
|
|
2017-03-01 18:47:52 +01:00
|
|
|
import java.io.IOException;
|
2020-02-24 15:48:23 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2020-02-23 11:23:33 +01:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2020-02-27 17:39:23 +01:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
|
2020-02-17 11:02:43 +01:00
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
|
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
|
|
|
|
|
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 {
|
2019-08-12 11:57:29 +02:00
|
|
|
/*package-private*/ static final String CHANNEL_URL_BASE = "https://www.youtube.com/channel/";
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2020-02-22 23:51:02 +01:00
|
|
|
private JsonObject initialData;
|
2017-09-26 22:46:21 +02:00
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
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 {
|
2020-02-27 18:54:27 +01:00
|
|
|
final String url = super.getUrl() + "/videos?pbj=1&view=0&flow=grid";
|
2020-02-26 09:31:26 +01:00
|
|
|
|
|
|
|
JsonArray ajaxJson;
|
|
|
|
|
|
|
|
Map<String, List<String>> headers = new HashMap<>();
|
|
|
|
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
|
|
|
|
headers.put("X-YouTube-Client-Version",
|
2020-02-26 15:22:59 +01:00
|
|
|
Collections.singletonList(YoutubeParsingHelper.getClientVersion()));
|
2020-02-26 09:31:26 +01:00
|
|
|
final String response = getDownloader().get(url, headers, getExtractorLocalization()).responseBody();
|
|
|
|
if (response.length() < 50) { // ensure to have a valid response
|
|
|
|
throw new ParsingException("Could not parse json data for next streams");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
ajaxJson = JsonParser.array().from(response);
|
|
|
|
} catch (JsonParserException e) {
|
|
|
|
throw new ParsingException("Could not parse json data for next streams", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
initialData = ajaxJson.getObject(1).getObject("response");
|
2020-02-17 09:55:06 +01:00
|
|
|
}
|
|
|
|
|
2018-02-26 15:55:27 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getNextPageUrl() throws ExtractionException {
|
2020-02-27 09:21:25 +01:00
|
|
|
if (getVideoTab() == null) return "";
|
2020-02-26 09:31:26 +01:00
|
|
|
return getNextPageUrlFrom(getVideoTab().getObject("content").getObject("sectionListRenderer")
|
|
|
|
.getArray("contents").getObject(0).getObject("itemSectionRenderer")
|
|
|
|
.getArray("contents").getObject(0).getObject("gridRenderer").getArray("continuations"));
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
2017-03-01 18:47:52 +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 {
|
2019-08-12 11:57:29 +02:00
|
|
|
return CHANNEL_URL_BASE + getId();
|
2017-08-16 04:56:10 +02:00
|
|
|
} catch (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 {
|
2017-06-29 20:12:55 +02:00
|
|
|
try {
|
2020-02-25 16:32:08 +01:00
|
|
|
return initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getString("channelId");
|
2017-06-29 20:12:55 +02:00
|
|
|
} catch (Exception e) {
|
2017-08-16 04:56:10 +02:00
|
|
|
throw new ParsingException("Could not get channel id", e);
|
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 {
|
2020-02-25 16:32:08 +01:00
|
|
|
return initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getString("title");
|
2017-06-29 20:12:55 +02:00
|
|
|
} catch (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 {
|
2020-02-27 17:39:23 +01:00
|
|
|
String url = initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getObject("avatar")
|
2020-02-25 16:32:08 +01:00
|
|
|
.getArray("thumbnails").getObject(0).getString("url");
|
2020-02-27 17:39:23 +01:00
|
|
|
|
|
|
|
if (url.startsWith(HTTP)) {
|
|
|
|
url = Utils.replaceHttpWithHttps(url);
|
|
|
|
} else if (!url.startsWith(HTTPS)) {
|
|
|
|
url = HTTPS + url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
2017-06-29 20:12:55 +02:00
|
|
|
} 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-02-24 13:01:13 +01:00
|
|
|
String url = null;
|
|
|
|
try {
|
2020-02-25 16:32:08 +01:00
|
|
|
url = initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getObject("banner")
|
|
|
|
.getArray("thumbnails").getObject(0).getString("url");
|
2020-02-24 13:01:13 +01:00
|
|
|
} catch (Exception ignored) {}
|
|
|
|
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-17 11:02:43 +01:00
|
|
|
if (url.startsWith(HTTP)) {
|
|
|
|
url = Utils.replaceHttpWithHttps(url);
|
|
|
|
} else if (!url.startsWith(HTTPS)) {
|
|
|
|
url = HTTPS + url;
|
|
|
|
}
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2020-02-17 11:02:43 +01:00
|
|
|
return url;
|
2017-06-29 20:12:55 +02:00
|
|
|
} catch (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());
|
2017-08-11 03:23:09 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get feed url", e);
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 20:12:55 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getSubscriberCount() throws ParsingException {
|
2020-02-22 23:51:02 +01:00
|
|
|
final JsonObject subscriberInfo = initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getObject("subscriberCountText");
|
2020-02-17 17:30:22 +01:00
|
|
|
if (subscriberInfo != null) {
|
2018-09-04 03:37:31 +02:00
|
|
|
try {
|
2020-02-27 17:39:23 +01:00
|
|
|
return Utils.mixedNumberWordToLong(getTextFromObject(subscriberInfo));
|
2018-09-04 03:37:31 +02:00
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
throw new ParsingException("Could not get subscriber count", e);
|
|
|
|
}
|
2017-08-06 22:20:15 +02:00
|
|
|
} else {
|
2020-02-27 16:26:22 +01:00
|
|
|
// If there's no subscribe button, the channel has the subscriber count disabled
|
|
|
|
if (initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getObject("subscribeButton") == null) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
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 {
|
2020-02-22 23:51:02 +01:00
|
|
|
return initialData.getObject("metadata").getObject("channelMetadataRenderer").getString("description");
|
2017-06-29 20:12:55 +02:00
|
|
|
} catch (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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 02:03:30 +01:00
|
|
|
@Nonnull
|
2017-06-29 20:12:55 +02:00
|
|
|
@Override
|
2018-03-11 21:54:41 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
|
2018-02-24 22:20:50 +01:00
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2020-02-24 15:48:23 +01:00
|
|
|
|
2020-02-27 09:21:25 +01:00
|
|
|
if (getVideoTab() != null) {
|
|
|
|
JsonArray videos = getVideoTab().getObject("content").getObject("sectionListRenderer").getArray("contents")
|
|
|
|
.getObject(0).getObject("itemSectionRenderer").getArray("contents").getObject(0)
|
|
|
|
.getObject("gridRenderer").getArray("items");
|
|
|
|
collectStreamsFrom(collector, videos);
|
|
|
|
}
|
2020-02-24 15:48:23 +01:00
|
|
|
|
2018-03-11 21:54:41 +01:00
|
|
|
return new InfoItemsPage<>(collector, getNextPageUrl());
|
2017-08-06 22:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-11 21:54:41 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
|
2018-03-01 01:02:43 +01:00
|
|
|
if (pageUrl == null || pageUrl.isEmpty()) {
|
|
|
|
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
|
|
|
|
}
|
2017-06-29 20:12:55 +02:00
|
|
|
|
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();
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2018-03-01 01:02:43 +01:00
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2020-02-24 15:48:23 +01:00
|
|
|
JsonArray ajaxJson;
|
2020-02-24 19:03:54 +01:00
|
|
|
|
|
|
|
Map<String, List<String>> headers = new HashMap<>();
|
|
|
|
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
|
2020-02-26 09:31:26 +01:00
|
|
|
headers.put("X-YouTube-Client-Version",
|
2020-02-26 15:22:59 +01:00
|
|
|
Collections.singletonList(YoutubeParsingHelper.getClientVersion()));
|
2020-02-26 09:31:26 +01:00
|
|
|
final String response = getDownloader().get(pageUrl, headers, getExtractorLocalization()).responseBody();
|
|
|
|
if (response.length() < 50) { // ensure to have a valid response
|
|
|
|
throw new ParsingException("Could not parse json data for next streams");
|
|
|
|
}
|
|
|
|
|
2018-03-01 01:02:43 +01:00
|
|
|
try {
|
2020-02-24 15:48:23 +01:00
|
|
|
ajaxJson = JsonParser.array().from(response);
|
2020-02-26 09:31:26 +01:00
|
|
|
} catch (JsonParserException e) {
|
|
|
|
throw new ParsingException("Could not parse json data for next streams", e);
|
2018-02-26 15:55:27 +01:00
|
|
|
}
|
2018-03-01 01:02:43 +01:00
|
|
|
|
2020-02-24 15:48:23 +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
|
|
|
|
2020-02-24 15:48:23 +01:00
|
|
|
return new InfoItemsPage<>(collector, getNextPageUrlFrom(sectionListContinuation.getArray("continuations")));
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
2020-02-24 15:48:23 +01:00
|
|
|
|
|
|
|
private String getNextPageUrlFrom(JsonArray continuations) {
|
2020-02-27 09:21:25 +01:00
|
|
|
if (continuations == null) return "";
|
2020-02-24 16:45:36 +01:00
|
|
|
|
2020-02-24 15:48:23 +01:00
|
|
|
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;
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
2020-02-24 15:48:23 +01:00
|
|
|
private void collectStreamsFrom(StreamInfoItemsCollector collector, 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();
|
|
|
|
|
2020-02-23 11:23:33 +01:00
|
|
|
for (Object video : videos) {
|
2020-02-26 09:31:26 +01:00
|
|
|
if (((JsonObject) video).getObject("gridVideoRenderer") != null) {
|
|
|
|
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() {
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 15:48:23 +01:00
|
|
|
|
|
|
|
private JsonObject getVideoTab() throws ParsingException {
|
|
|
|
JsonArray tabs = initialData.getObject("contents").getObject("twoColumnBrowseResultsRenderer")
|
|
|
|
.getArray("tabs");
|
|
|
|
JsonObject videoTab = null;
|
|
|
|
|
|
|
|
for (Object tab : tabs) {
|
|
|
|
if (((JsonObject) tab).getObject("tabRenderer") != null) {
|
|
|
|
if (((JsonObject) tab).getObject("tabRenderer").getString("title").equals("Videos")) {
|
|
|
|
videoTab = ((JsonObject) tab).getObject("tabRenderer");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoTab == null) {
|
|
|
|
throw new ParsingException("Could not find Videos tab");
|
|
|
|
}
|
|
|
|
|
2020-02-27 09:21:25 +01:00
|
|
|
try {
|
2020-02-27 17:39:23 +01:00
|
|
|
if (getTextFromObject(videoTab.getObject("content").getObject("sectionListRenderer")
|
|
|
|
.getArray("contents").getObject(0).getObject("itemSectionRenderer")
|
|
|
|
.getArray("contents").getObject(0).getObject("messageRenderer")
|
|
|
|
.getObject("text")).equals("This channel has no videos."))
|
2020-02-27 09:21:25 +01:00
|
|
|
return null;
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
|
2020-02-24 15:48:23 +01:00
|
|
|
return videoTab;
|
|
|
|
}
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|