[YouTube Music] Fix extracting search item view/subscriber count when = 0

This commit is contained in:
Stypox 2021-01-27 14:46:06 +01:00
parent c00ee75d74
commit a64dfd7343
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 21 additions and 9 deletions

View File

@ -20,9 +20,8 @@ import org.schabi.newpipe.extractor.localization.TimeAgoParser;
import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector; import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector;
import org.schabi.newpipe.extractor.search.SearchExtractor; import org.schabi.newpipe.extractor.search.SearchExtractor;
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubePlaylistLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Parser;
import org.schabi.newpipe.extractor.utils.Utils; import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
@ -33,7 +32,10 @@ import java.util.Map;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.fixThumbnailUrl;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ALBUMS; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ALBUMS;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ARTISTS; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ARTISTS;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS;
@ -365,7 +367,12 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
.getObject(descriptionElements.size() - 3) .getObject(descriptionElements.size() - 3)
.getString("text"); .getString("text");
if (!isNullOrEmpty(viewCount)) { if (!isNullOrEmpty(viewCount)) {
return Utils.mixedNumberWordToLong(viewCount); try {
return Utils.mixedNumberWordToLong(viewCount);
} catch (final Parser.RegexException e) {
// probably viewCount == "No views" or similar
return 0;
}
} }
throw new ParsingException("Could not get view count"); throw new ParsingException("Could not get view count");
} }
@ -421,10 +428,15 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
@Override @Override
public long getSubscriberCount() throws ParsingException { public long getSubscriberCount() throws ParsingException {
final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2) final String subscriberCount = getTextFromObject(info.getArray("flexColumns").getObject(2)
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
if (!isNullOrEmpty(viewCount)) { if (!isNullOrEmpty(subscriberCount)) {
return Utils.mixedNumberWordToLong(viewCount); try {
return Utils.mixedNumberWordToLong(subscriberCount);
} catch (final Parser.RegexException ignored) {
// probably subscriberCount == "No subscribers" or similar
return 0;
}
} }
throw new ParsingException("Could not get subscriber count"); throw new ParsingException("Could not get subscriber count");
} }

View File

@ -69,9 +69,9 @@ public class Parser {
} else { } else {
// only pass input to exception message when it is not too long // only pass input to exception message when it is not too long
if (input.length() > 1024) { if (input.length() > 1024) {
throw new RegexException("failed to find pattern \"" + pat.pattern()); throw new RegexException("failed to find pattern \"" + pat.pattern() + "\"");
} else { } else {
throw new RegexException("failed to find pattern \"" + pat.pattern() + " inside of " + input + "\""); throw new RegexException("failed to find pattern \"" + pat.pattern() + "\" inside of \"" + input + "\"");
} }
} }
} }