2018-05-08 19:19:03 +00:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
2017-03-01 17:47:52 +00:00
|
|
|
|
2020-02-23 17:27:28 +00:00
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
|
2020-03-17 13:04:46 +00:00
|
|
|
import org.schabi.newpipe.extractor.ListExtractor;
|
2017-08-11 01:23:09 +00:00
|
|
|
import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor;
|
2017-03-01 17:47:52 +00:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2021-01-22 00:44:58 +00:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
|
2020-02-23 17:27:28 +00:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
|
2017-07-11 03:08:03 +00:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2017-03-01 17:47:52 +00:00
|
|
|
|
2020-04-10 08:51:05 +00:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.fixThumbnailUrl;
|
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
|
2020-02-24 11:48:16 +00:00
|
|
|
|
2017-06-29 18:12:55 +00:00
|
|
|
/*
|
2017-03-01 17:47:52 +00:00
|
|
|
* Created by Christian Schabesberger on 12.02.17.
|
|
|
|
*
|
|
|
|
* Copyright (C) Christian Schabesberger 2017 <chris.schabesberger@mailbox.org>
|
2017-08-11 01:23:09 +00:00
|
|
|
* YoutubeChannelInfoItemExtractor.java is part of NewPipe.
|
2017-03-01 17:47:52 +00: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-08-11 01:23:09 +00:00
|
|
|
public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor {
|
2022-03-18 14:09:06 +00:00
|
|
|
private final JsonObject channelInfoItem;
|
2022-11-22 01:17:10 +00:00
|
|
|
/**
|
|
|
|
* New layout:
|
|
|
|
* "subscriberCountText": Channel handle
|
|
|
|
* "videoCountText": Subscriber count
|
|
|
|
*/
|
|
|
|
private final boolean withHandle;
|
2017-03-01 17:47:52 +00:00
|
|
|
|
2022-03-18 14:09:06 +00:00
|
|
|
public YoutubeChannelInfoItemExtractor(final JsonObject channelInfoItem) {
|
2020-02-23 17:27:28 +00:00
|
|
|
this.channelInfoItem = channelInfoItem;
|
2022-11-22 01:17:10 +00:00
|
|
|
|
|
|
|
boolean wHandle = false;
|
|
|
|
try {
|
|
|
|
final String subscriberCountText = getTextFromObject(
|
|
|
|
channelInfoItem.getObject("subscriberCountText"));
|
|
|
|
if (subscriberCountText != null) {
|
|
|
|
wHandle = subscriberCountText.startsWith("@");
|
|
|
|
}
|
|
|
|
} catch (final ParsingException ignored) {
|
|
|
|
}
|
|
|
|
this.withHandle = wHandle;
|
2017-03-01 17:47:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:12:55 +00:00
|
|
|
@Override
|
2017-03-01 17:47:52 +00:00
|
|
|
public String getThumbnailUrl() throws ParsingException {
|
2020-02-23 17:27:28 +00:00
|
|
|
try {
|
2022-03-18 14:09:06 +00:00
|
|
|
final String url = channelInfoItem.getObject("thumbnail").getArray("thumbnails")
|
|
|
|
.getObject(0).getString("url");
|
2020-02-27 18:08:46 +00:00
|
|
|
|
2020-02-28 08:36:33 +00:00
|
|
|
return fixThumbnailUrl(url);
|
2022-03-18 14:09:06 +00:00
|
|
|
} catch (final Exception e) {
|
2020-02-23 17:27:28 +00:00
|
|
|
throw new ParsingException("Could not get thumbnail url", e);
|
2017-03-01 17:47:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 18:12:55 +00:00
|
|
|
@Override
|
2017-08-11 18:21:49 +00:00
|
|
|
public String getName() throws ParsingException {
|
2020-02-23 17:27:28 +00:00
|
|
|
try {
|
2020-02-27 16:39:23 +00:00
|
|
|
return getTextFromObject(channelInfoItem.getObject("title"));
|
2022-03-18 14:09:06 +00:00
|
|
|
} catch (final Exception e) {
|
2020-02-23 17:27:28 +00:00
|
|
|
throw new ParsingException("Could not get name", e);
|
|
|
|
}
|
2017-03-01 17:47:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:12:55 +00:00
|
|
|
@Override
|
2017-08-11 18:21:49 +00:00
|
|
|
public String getUrl() throws ParsingException {
|
2019-08-27 10:01:00 +00:00
|
|
|
try {
|
2022-03-18 14:09:06 +00:00
|
|
|
final String id = "channel/" + channelInfoItem.getString("channelId");
|
2020-02-23 17:27:28 +00:00
|
|
|
return YoutubeChannelLinkHandlerFactory.getInstance().getUrl(id);
|
2022-03-18 14:09:06 +00:00
|
|
|
} catch (final Exception e) {
|
2020-02-23 17:27:28 +00:00
|
|
|
throw new ParsingException("Could not get url", e);
|
2019-08-12 09:57:29 +00:00
|
|
|
}
|
2017-03-01 17:47:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:12:55 +00:00
|
|
|
@Override
|
2020-02-24 09:39:52 +00:00
|
|
|
public long getSubscriberCount() throws ParsingException {
|
|
|
|
try {
|
2020-04-16 14:08:14 +00:00
|
|
|
if (!channelInfoItem.has("subscriberCountText")) {
|
2020-03-21 06:15:23 +00:00
|
|
|
// Subscription count is not available for this channel item.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-22 01:17:10 +00:00
|
|
|
if (withHandle) {
|
2023-01-24 22:03:29 +00:00
|
|
|
if (channelInfoItem.has("videoCountText")) {
|
|
|
|
return Utils.mixedNumberWordToLong(getTextFromObject(
|
|
|
|
channelInfoItem.getObject("videoCountText")));
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-11-22 01:17:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 14:09:06 +00:00
|
|
|
return Utils.mixedNumberWordToLong(getTextFromObject(
|
|
|
|
channelInfoItem.getObject("subscriberCountText")));
|
|
|
|
} catch (final Exception e) {
|
2020-02-24 09:39:52 +00:00
|
|
|
throw new ParsingException("Could not get subscriber count", e);
|
|
|
|
}
|
2017-03-01 17:47:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:12:55 +00:00
|
|
|
@Override
|
2017-08-06 20:20:15 +00:00
|
|
|
public long getStreamCount() throws ParsingException {
|
2020-02-23 17:27:28 +00:00
|
|
|
try {
|
2022-11-22 01:17:10 +00:00
|
|
|
if (withHandle || !channelInfoItem.has("videoCountText")) {
|
2022-11-29 18:06:03 +00:00
|
|
|
// Video count is not available, either the channel has no public uploads
|
|
|
|
// or YouTube displays the channel handle instead.
|
2020-03-17 13:04:46 +00:00
|
|
|
return ListExtractor.ITEM_COUNT_UNKNOWN;
|
2020-03-21 06:16:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 14:08:14 +00:00
|
|
|
return Long.parseLong(Utils.removeNonDigitCharacters(getTextFromObject(
|
|
|
|
channelInfoItem.getObject("videoCountText"))));
|
2022-03-18 14:09:06 +00:00
|
|
|
} catch (final Exception e) {
|
2020-02-23 18:45:45 +00:00
|
|
|
throw new ParsingException("Could not get stream count", e);
|
2017-03-01 17:47:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 00:44:58 +00:00
|
|
|
@Override
|
|
|
|
public boolean isVerified() throws ParsingException {
|
|
|
|
return YoutubeParsingHelper.isVerified(channelInfoItem.getArray("ownerBadges"));
|
|
|
|
}
|
|
|
|
|
2017-06-29 18:12:55 +00:00
|
|
|
@Override
|
2017-03-01 17:47:52 +00:00
|
|
|
public String getDescription() throws ParsingException {
|
2020-02-23 17:27:28 +00:00
|
|
|
try {
|
2020-04-16 14:08:14 +00:00
|
|
|
if (!channelInfoItem.has("descriptionSnippet")) {
|
2020-03-21 06:15:51 +00:00
|
|
|
// Channel have no description.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:08:14 +00:00
|
|
|
return getTextFromObject(channelInfoItem.getObject("descriptionSnippet"));
|
2022-03-18 14:09:06 +00:00
|
|
|
} catch (final Exception e) {
|
2020-02-23 18:45:45 +00:00
|
|
|
throw new ParsingException("Could not get description", e);
|
2017-03-01 17:47:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|