1818import java .time .OffsetDateTime ;
1919import java .time .ZoneOffset ;
2020import java .time .format .DateTimeFormatter ;
21+ import java .util .regex .Pattern ;
2122
2223import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getTextFromObject ;
2324import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getThumbnailUrlFromInfoItem ;
4344 */
4445
4546public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
47+
48+ private static final Pattern ACCESSIBILITY_DATA_VIEW_COUNT_REGEX =
49+ Pattern .compile ("([\\ d,]+) views$" );
50+ private static final String NO_VIEWS_LOWERCASE = "no views" ;
51+
4652 private final JsonObject videoInfo ;
4753 private final TimeAgoParser timeAgoParser ;
4854 private StreamType cachedStreamType ;
@@ -289,30 +295,36 @@ public long getViewCount() throws ParsingException {
289295 if (!isNullOrEmpty (viewCount )) {
290296 try {
291297 // These approaches are language dependent
292- if (viewCount .toLowerCase ().contains ("no views" )) {
298+ if (viewCount .toLowerCase ().contains (NO_VIEWS_LOWERCASE )) {
293299 return 0 ;
294300 } else if (viewCount .toLowerCase ().contains ("recommended" )) {
295301 return -1 ;
296302 }
297303
298304 return Long .parseLong (Utils .removeNonDigitCharacters (viewCount ));
299305 } catch (final Exception ignored ) {
300- // Ignore all exceptions, as we can fallback to accessibility data
306+ // Ignore all exceptions, as we can fall back to accessibility data
301307 }
302308 }
303309
304310 // Try parsing the real view count from accessibility data, if that's not a running
305311 // livestream (the view count is returned and not the count of people watching currently
306312 // the livestream)
307313 if (getStreamType () != StreamType .LIVE_STREAM ) {
314+ final String videoInfoTitleAccessibilityData = videoInfo .getObject ("title" )
315+ .getObject ("accessibility" )
316+ .getObject ("accessibilityData" )
317+ .getString ("label" , "" );
318+
319+ if (videoInfoTitleAccessibilityData .toLowerCase ().endsWith (NO_VIEWS_LOWERCASE )) {
320+ return 0 ;
321+ }
322+
308323 try {
309324 return Long .parseLong (Utils .removeNonDigitCharacters (
310325 // This approach is language dependent
311- Parser .matchGroup1 ("([\\ d,]+) views$" ,
312- videoInfo .getObject ("title" )
313- .getObject ("accessibility" )
314- .getObject ("accessibilityData" )
315- .getString ("label" , "" ))));
326+ Parser .matchGroup1 (ACCESSIBILITY_DATA_VIEW_COUNT_REGEX ,
327+ videoInfoTitleAccessibilityData )));
316328 } catch (final Exception ignored ) {
317329 // Ignore all exceptions, as the view count can be hidden by creators, and so
318330 // cannot be found in this case
@@ -323,18 +335,30 @@ public long getViewCount() throws ParsingException {
323335 try {
324336 // Returned in playlists, in the form: view count separator upload date
325337 if (videoInfo .has ("videoInfo" )) {
326- return Utils . mixedNumberWordToLong ( videoInfo .getObject ("videoInfo" )
338+ final String videoInfoViewCountText = videoInfo .getObject ("videoInfo" )
327339 .getArray ("runs" )
328340 .getObject (0 )
329- .getString ("text" ));
341+ .getString ("text" , "" );
342+ if (videoInfoViewCountText .toLowerCase ().contains (NO_VIEWS_LOWERCASE )) {
343+ return 0 ;
344+ }
345+
346+ return Utils .mixedNumberWordToLong (videoInfoViewCountText );
330347 }
331348
332349 // Returned everywhere but in playlists, used by the website to show view counts
333350 if (videoInfo .has ("shortViewCountText" )) {
334- return Utils .mixedNumberWordToLong (videoInfo .getObject ("shortViewCountText" )
335- .getArray ("runs" )
336- .getObject (0 )
337- .getString ("text" ));
351+ final String shortVideoViewCountText =
352+ getTextFromObject (videoInfo .getObject ("shortViewCountText" ));
353+ if (isNullOrEmpty (shortVideoViewCountText )) {
354+ return -1 ;
355+ }
356+
357+ if (shortVideoViewCountText .toLowerCase ().contains (NO_VIEWS_LOWERCASE )) {
358+ return 0 ;
359+ }
360+
361+ return Utils .mixedNumberWordToLong (shortVideoViewCountText );
338362 }
339363 } catch (final Exception ignored ) {
340364 // Ignore all exceptions, as the view count can be hidden by creators, and so cannot be
0 commit comments