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

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