2020import org .schabi .newpipe .extractor .search .InfoItemsSearchCollector ;
2121import org .schabi .newpipe .extractor .search .SearchExtractor ;
2222import org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper ;
23+ import org .schabi .newpipe .extractor .services .youtube .linkHandler .YoutubePlaylistLinkHandlerFactory ;
24+ import org .schabi .newpipe .extractor .services .youtube .linkHandler .YoutubeStreamLinkHandlerFactory ;
2325import org .schabi .newpipe .extractor .utils .JsonUtils ;
2426import org .schabi .newpipe .extractor .utils .Utils ;
2527
@@ -258,16 +260,29 @@ private void collectMusicStreamsFrom(final InfoItemsSearchCollector collector, f
258260 final TimeAgoParser timeAgoParser = getTimeAgoParser ();
259261
260262 for (Object item : videos ) {
261- final JsonObject info = ((JsonObject ) item ).getObject ("musicResponsiveListItemRenderer" , null );
263+ final JsonObject info = ((JsonObject ) item )
264+ .getObject ("musicResponsiveListItemRenderer" , null );
262265 if (info != null ) {
266+ final String displayPolicy = info .getString ("musicItemRendererDisplayPolicy" , EMPTY_STRING );
267+ if (displayPolicy .equals ("MUSIC_ITEM_RENDERER_DISPLAY_POLICY_GREY_OUT" )) {
268+ continue ; // no info about video URL available
269+ }
270+
271+ final JsonObject flexColumnRenderer = info
272+ .getArray ("flexColumns" )
273+ .getObject (1 )
274+ .getObject ("musicResponsiveListItemFlexColumnRenderer" );
275+ final JsonArray descriptionElements = flexColumnRenderer
276+ .getObject ("text" )
277+ .getArray ("runs" );
263278 final String searchType = getLinkHandler ().getContentFilters ().get (0 );
264279 if (searchType .equals (MUSIC_SONGS ) || searchType .equals (MUSIC_VIDEOS )) {
265280 collector .commit (new YoutubeStreamInfoItemExtractor (info , timeAgoParser ) {
266281 @ Override
267282 public String getUrl () throws ParsingException {
268- final String url = getUrlFromNavigationEndpoint ( info .getObject ("doubleTapCommand" ) );
269- if (!isNullOrEmpty (url )) {
270- return url ;
283+ final String id = info .getObject ("playlistItemData" ). getString ( "videoId" );
284+ if (!isNullOrEmpty (id )) {
285+ return "https://music.youtube.com/watch?v=" + id ;
271286 }
272287 throw new ParsingException ("Could not get url" );
273288 }
@@ -284,8 +299,9 @@ public String getName() throws ParsingException {
284299
285300 @ Override
286301 public long getDuration () throws ParsingException {
287- final String duration = getTextFromObject (info .getArray ("flexColumns" ).getObject (3 )
288- .getObject ("musicResponsiveListItemFlexColumnRenderer" ).getObject ("text" ));
302+ final String duration = descriptionElements
303+ .getObject (descriptionElements .size () - 1 )
304+ .getString ("text" );
289305 if (!isNullOrEmpty (duration )) {
290306 return YoutubeParsingHelper .parseDurationString (duration );
291307 }
@@ -294,8 +310,7 @@ public long getDuration() throws ParsingException {
294310
295311 @ Override
296312 public String getUploaderName () throws ParsingException {
297- final String name = getTextFromObject (info .getArray ("flexColumns" ).getObject (1 )
298- .getObject ("musicResponsiveListItemFlexColumnRenderer" ).getObject ("text" ));
313+ final String name = descriptionElements .getObject (0 ).getString ("text" );
299314 if (!isNullOrEmpty (name )) {
300315 return name ;
301316 }
@@ -346,8 +361,9 @@ public long getViewCount() throws ParsingException {
346361 if (searchType .equals (MUSIC_SONGS )) {
347362 return -1 ;
348363 }
349- final String viewCount = getTextFromObject (info .getArray ("flexColumns" ).getObject (2 )
350- .getObject ("musicResponsiveListItemFlexColumnRenderer" ).getObject ("text" ));
364+ final String viewCount = descriptionElements
365+ .getObject (descriptionElements .size () - 3 )
366+ .getString ("text" );
351367 if (!isNullOrEmpty (viewCount )) {
352368 return Utils .mixedNumberWordToLong (viewCount );
353369 }
@@ -451,9 +467,27 @@ public String getName() throws ParsingException {
451467
452468 @ Override
453469 public String getUrl () throws ParsingException {
454- final String url = getUrlFromNavigationEndpoint (info .getObject ("doubleTapCommand" ));
455- if (!isNullOrEmpty (url )) {
456- return url ;
470+ String playlistId = info .getObject ("menu" )
471+ .getObject ("menuRenderer" )
472+ .getArray ("items" )
473+ .getObject (4 )
474+ .getObject ("toggleMenuServiceItemRenderer" )
475+ .getObject ("toggledServiceEndpoint" )
476+ .getObject ("likeEndpoint" )
477+ .getObject ("target" )
478+ .getString ("playlistId" );
479+
480+ if (isNullOrEmpty (playlistId )) {
481+ playlistId = info .getObject ("overlay" )
482+ .getObject ("musicItemThumbnailOverlayRenderer" )
483+ .getObject ("content" )
484+ .getObject ("musicPlayButtonRenderer" )
485+ .getObject ("playNavigationEndpoint" )
486+ .getObject ("watchPlaylistEndpoint" )
487+ .getString ("playlistId" );
488+ }
489+ if (!isNullOrEmpty (playlistId )) {
490+ return "https://music.youtube.com/playlist?list=" + playlistId ;
457491 }
458492 throw new ParsingException ("Could not get url" );
459493 }
@@ -462,11 +496,9 @@ public String getUrl() throws ParsingException {
462496 public String getUploaderName () throws ParsingException {
463497 final String name ;
464498 if (searchType .equals (MUSIC_ALBUMS )) {
465- name = getTextFromObject (info .getArray ("flexColumns" ).getObject (2 )
466- .getObject ("musicResponsiveListItemFlexColumnRenderer" ).getObject ("text" ));
499+ name = descriptionElements .getObject (2 ).getString ("text" );
467500 } else {
468- name = getTextFromObject (info .getArray ("flexColumns" ).getObject (1 )
469- .getObject ("musicResponsiveListItemFlexColumnRenderer" ).getObject ("text" ));
501+ name = descriptionElements .getObject (0 ).getString ("text" );
470502 }
471503 if (!isNullOrEmpty (name )) {
472504 return name ;
@@ -479,8 +511,7 @@ public long getStreamCount() throws ParsingException {
479511 if (searchType .equals (MUSIC_ALBUMS )) {
480512 return ITEM_COUNT_UNKNOWN ;
481513 }
482- final String count = getTextFromObject (info .getArray ("flexColumns" ).getObject (2 )
483- .getObject ("musicResponsiveListItemFlexColumnRenderer" ).getObject ("text" ));
514+ final String count = descriptionElements .getObject (2 ).getString ("text" );
484515 if (!isNullOrEmpty (count )) {
485516 if (count .contains ("100+" )) {
486517 return ITEM_COUNT_MORE_THAN_100 ;
0 commit comments