Skip to content

Commit

Permalink
Merge pull request #528 from Stypox/ytmusic-0
Browse files Browse the repository at this point in the history
[YouTube Music] Fix extracting search item view/subscriber count when = 0
  • Loading branch information
TobiGr authored Jan 29, 2021
2 parents d728c4f + a64dfd7 commit bfa6399
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector;
import org.schabi.newpipe.extractor.search.SearchExtractor;
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.Parser;
import org.schabi.newpipe.extractor.utils.Utils;

import java.io.IOException;
Expand All @@ -33,7 +32,10 @@

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_ARTISTS;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS;
Expand Down Expand Up @@ -365,7 +367,12 @@ public long getViewCount() throws ParsingException {
.getObject(descriptionElements.size() - 3)
.getString("text");
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");
}
Expand Down Expand Up @@ -421,10 +428,15 @@ public String getUrl() throws ParsingException {

@Override
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"));
if (!isNullOrEmpty(viewCount)) {
return Utils.mixedNumberWordToLong(viewCount);
if (!isNullOrEmpty(subscriberCount)) {
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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public static String matchGroup(Pattern pat, String input, int group) throws Reg
} else {
// only pass input to exception message when it is not too long
if (input.length() > 1024) {
throw new RegexException("failed to find pattern \"" + pat.pattern());
throw new RegexException("failed to find pattern \"" + pat.pattern() + "\"");
} 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 + "\"");
}
}
}
Expand Down

0 comments on commit bfa6399

Please sign in to comment.