[GH-ISSUE #758] YTMusic.search('query', 'songs') returns no view count #486

Closed
opened 2026-02-27 23:01:04 +03:00 by kerem · 3 comments
Owner

Originally created by @serg-bloim on GitHub (Apr 2, 2025).
Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/758

Describe the bug
The result of YTMusic.search('query') returns no 'views' count for songs, but does so for videos.

To Reproduce
This code snippet should explain the issue:

        ytm = YTMusic()
        song = ytm.search('Nothing else matters', 'songs')[0]
        video = ytm.search('Nothing else matters', 'videos')[0]
        
        assert 'views' in video # just fine
        assert 'views' in song # Fails

Additional context
It turns out there is a slight format difference in the output for video and song content:
The views count is being processed here: github.com/sigma67/ytmusicapi@f59a1b5dfd/ytmusicapi/parsers/search.py (L173)
It takes data from flexColumns prop of data var. Video records have 2 flexColumns and views count is in the second column. Whereas song records have 3 columns: most data is in the second one and the views(plays) are in the third column.

Here's a snippet that works for me locally.

        flex_items = [fci for i in [1, 2] if (fci := get_flex_column_item(data, i))]
        runs = [run 
                for flex_item in flex_items 
                for run in flex_item["text"]["runs"] + [{'text':'separator'}]]

JSON example for video:

data={
  "flexColumns": [
    {
      "musicResponsiveListItemFlexColumnRenderer": {
        "text": {
          "runs": [
            {
              "text": "Nothing Else Matters (Live)",
              "navigationEndpoint": {
                "clickTrackingParams": "CJsCENNoGAAiEwjz7I_7zLmMAxUMgOQGHRUBM62aAQMQ9CQ=",
                "watchEndpoint": {
                  "videoId": "tAGnKpE4NCI",
                  "watchEndpointMusicSupportedConfigs": {
                    "watchEndpointMusicConfig": {
                      "musicVideoType": "MUSIC_VIDEO_TYPE_OMV"
                    }
                  }
                }
              }
            }
          ]
        },
        "displayPriority": "MUSIC_RESPONSIVE_LIST_ITEM_COLUMN_DISPLAY_PRIORITY_HIGH"
      }
    },
    {
      "musicResponsiveListItemFlexColumnRenderer": {
        "text": {
          "runs": [
            {
              "text": "San Francisco Symphony",
              "navigationEndpoint": {
                "clickTrackingParams": "CJsCENNoGAAiEwjz7I_7zLmMAxUMgOQGHRUBM60=",
                "browseEndpoint": {
                  "browseId": "UCxleVYUZPyrp_QheZRJnzCQ",
                  "browseEndpointContextSupportedConfigs": {
                    "browseEndpointContextMusicConfig": {
                      "pageType": "MUSIC_PAGE_TYPE_ARTIST"
                    }
                  }
                }
              }
            },
            {
              "text": " & "
            },
            {
              "text": "Metallica",
              "navigationEndpoint": {
                "clickTrackingParams": "CJsCENNoGAAiEwjz7I_7zLmMAxUMgOQGHRUBM60=",
                "browseEndpoint": {
                  "browseId": "UCGexNm_Kw4rdQjLxmpb2EKw",
                  "browseEndpointContextSupportedConfigs": {
                    "browseEndpointContextMusicConfig": {
                      "pageType": "MUSIC_PAGE_TYPE_ARTIST"
                    }
                  }
                }
              }
            },
            {
              "text": " • "
            },
            {
              "text": "1.4B views"
            },
            {
              "text": " • "
            },
            {
              "text": "6:26"
            }
          ],
          "accessibility": {
            "accessibilityData": {
              "label": "San Francisco Symphony & Metallica • 1.4 billion views • 6 minutes, 26 seconds"
            }
          }
        },
        "displayPriority": "MUSIC_RESPONSIVE_LIST_ITEM_COLUMN_DISPLAY_PRIORITY_HIGH"
      }
    }
  ]
}

JSON example for song:

data={
  "flexColumns": [
    {
      "musicResponsiveListItemFlexColumnRenderer": {
        "text": {
          "runs": [
            {
              "text": "Nothing Else Matters",
              "navigationEndpoint": {
                "clickTrackingParams": "CMoCEOFnGAAiEwikzO6WyrmMAxXsgeQGHUrMMIyaAQMQ9CQ=",
                "watchEndpoint": {
                  "videoId": "pTYIf2pkxzQ",
                  "watchEndpointMusicSupportedConfigs": {
                    "watchEndpointMusicConfig": {
                      "musicVideoType": "MUSIC_VIDEO_TYPE_ATV"
                    }
                  }
                }
              }
            }
          ]
        },
        "displayPriority": "MUSIC_RESPONSIVE_LIST_ITEM_COLUMN_DISPLAY_PRIORITY_HIGH"
      }
    },
    {
      "musicResponsiveListItemFlexColumnRenderer": {
        "text": {
          "runs": [
            {
              "text": "Metallica",
              "navigationEndpoint": {
                "clickTrackingParams": "CMoCEOFnGAAiEwikzO6WyrmMAxXsgeQGHUrMMIw=",
                "browseEndpoint": {
                  "browseId": "UCGexNm_Kw4rdQjLxmpb2EKw",
                  "browseEndpointContextSupportedConfigs": {
                    "browseEndpointContextMusicConfig": {
                      "pageType": "MUSIC_PAGE_TYPE_ARTIST"
                    }
                  }
                }
              }
            },
            {
              "text": " • "
            },
            {
              "text": "Metallica",
              "navigationEndpoint": {
                "clickTrackingParams": "CMoCEOFnGAAiEwikzO6WyrmMAxXsgeQGHUrMMIw=",
                "browseEndpoint": {
                  "browseId": "MPREb_AmmVchA41UA",
                  "browseEndpointContextSupportedConfigs": {
                    "browseEndpointContextMusicConfig": {
                      "pageType": "MUSIC_PAGE_TYPE_ALBUM"
                    }
                  }
                }
              }
            },
            {
              "text": " • "
            },
            {
              "text": "6:29"
            }
          ],
          "accessibility": {
            "accessibilityData": {
              "label": "Metallica • Metallica • 6 minutes, 29 seconds"
            }
          }
        },
        "displayPriority": "MUSIC_RESPONSIVE_LIST_ITEM_COLUMN_DISPLAY_PRIORITY_HIGH"
      }
    },
    {
      "musicResponsiveListItemFlexColumnRenderer": {
        "text": {
          "runs": [
            {
              "text": "2B plays"
            }
          ],
          "accessibility": {
            "accessibilityData": {
              "label": "2 billion plays"
            }
          }
        },
        "displayPriority": "MUSIC_RESPONSIVE_LIST_ITEM_COLUMN_DISPLAY_PRIORITY_HIGH"
      }
    }
  ]
}
Originally created by @serg-bloim on GitHub (Apr 2, 2025). Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/758 **Describe the bug** The result of YTMusic.search('query') returns no 'views' count for songs, but does so for videos. **To Reproduce** This code snippet should explain the issue: ``` ytm = YTMusic() song = ytm.search('Nothing else matters', 'songs')[0] video = ytm.search('Nothing else matters', 'videos')[0] assert 'views' in video # just fine assert 'views' in song # Fails ``` **Additional context** It turns out there is a slight format difference in the output for video and song content: The views count is being processed here: https://github.com/sigma67/ytmusicapi/blob/f59a1b5dfd23629615397bd2409e610169b9d57a/ytmusicapi/parsers/search.py#L173 It takes data from `flexColumns` prop of data var. Video records have 2 flexColumns and views count is in the second column. Whereas song records have 3 columns: most data is in the second one and the views(plays) are in the third column. Here's a snippet that works for me locally. ``` flex_items = [fci for i in [1, 2] if (fci := get_flex_column_item(data, i))] runs = [run for flex_item in flex_items for run in flex_item["text"]["runs"] + [{'text':'separator'}]] ``` JSON example for video: ``` data={ "flexColumns": [ { "musicResponsiveListItemFlexColumnRenderer": { "text": { "runs": [ { "text": "Nothing Else Matters (Live)", "navigationEndpoint": { "clickTrackingParams": "CJsCENNoGAAiEwjz7I_7zLmMAxUMgOQGHRUBM62aAQMQ9CQ=", "watchEndpoint": { "videoId": "tAGnKpE4NCI", "watchEndpointMusicSupportedConfigs": { "watchEndpointMusicConfig": { "musicVideoType": "MUSIC_VIDEO_TYPE_OMV" } } } } } ] }, "displayPriority": "MUSIC_RESPONSIVE_LIST_ITEM_COLUMN_DISPLAY_PRIORITY_HIGH" } }, { "musicResponsiveListItemFlexColumnRenderer": { "text": { "runs": [ { "text": "San Francisco Symphony", "navigationEndpoint": { "clickTrackingParams": "CJsCENNoGAAiEwjz7I_7zLmMAxUMgOQGHRUBM60=", "browseEndpoint": { "browseId": "UCxleVYUZPyrp_QheZRJnzCQ", "browseEndpointContextSupportedConfigs": { "browseEndpointContextMusicConfig": { "pageType": "MUSIC_PAGE_TYPE_ARTIST" } } } } }, { "text": " & " }, { "text": "Metallica", "navigationEndpoint": { "clickTrackingParams": "CJsCENNoGAAiEwjz7I_7zLmMAxUMgOQGHRUBM60=", "browseEndpoint": { "browseId": "UCGexNm_Kw4rdQjLxmpb2EKw", "browseEndpointContextSupportedConfigs": { "browseEndpointContextMusicConfig": { "pageType": "MUSIC_PAGE_TYPE_ARTIST" } } } } }, { "text": " • " }, { "text": "1.4B views" }, { "text": " • " }, { "text": "6:26" } ], "accessibility": { "accessibilityData": { "label": "San Francisco Symphony & Metallica • 1.4 billion views • 6 minutes, 26 seconds" } } }, "displayPriority": "MUSIC_RESPONSIVE_LIST_ITEM_COLUMN_DISPLAY_PRIORITY_HIGH" } } ] } ``` JSON example for song: ``` data={ "flexColumns": [ { "musicResponsiveListItemFlexColumnRenderer": { "text": { "runs": [ { "text": "Nothing Else Matters", "navigationEndpoint": { "clickTrackingParams": "CMoCEOFnGAAiEwikzO6WyrmMAxXsgeQGHUrMMIyaAQMQ9CQ=", "watchEndpoint": { "videoId": "pTYIf2pkxzQ", "watchEndpointMusicSupportedConfigs": { "watchEndpointMusicConfig": { "musicVideoType": "MUSIC_VIDEO_TYPE_ATV" } } } } } ] }, "displayPriority": "MUSIC_RESPONSIVE_LIST_ITEM_COLUMN_DISPLAY_PRIORITY_HIGH" } }, { "musicResponsiveListItemFlexColumnRenderer": { "text": { "runs": [ { "text": "Metallica", "navigationEndpoint": { "clickTrackingParams": "CMoCEOFnGAAiEwikzO6WyrmMAxXsgeQGHUrMMIw=", "browseEndpoint": { "browseId": "UCGexNm_Kw4rdQjLxmpb2EKw", "browseEndpointContextSupportedConfigs": { "browseEndpointContextMusicConfig": { "pageType": "MUSIC_PAGE_TYPE_ARTIST" } } } } }, { "text": " • " }, { "text": "Metallica", "navigationEndpoint": { "clickTrackingParams": "CMoCEOFnGAAiEwikzO6WyrmMAxXsgeQGHUrMMIw=", "browseEndpoint": { "browseId": "MPREb_AmmVchA41UA", "browseEndpointContextSupportedConfigs": { "browseEndpointContextMusicConfig": { "pageType": "MUSIC_PAGE_TYPE_ALBUM" } } } } }, { "text": " • " }, { "text": "6:29" } ], "accessibility": { "accessibilityData": { "label": "Metallica • Metallica • 6 minutes, 29 seconds" } } }, "displayPriority": "MUSIC_RESPONSIVE_LIST_ITEM_COLUMN_DISPLAY_PRIORITY_HIGH" } }, { "musicResponsiveListItemFlexColumnRenderer": { "text": { "runs": [ { "text": "2B plays" } ], "accessibility": { "accessibilityData": { "label": "2 billion plays" } } }, "displayPriority": "MUSIC_RESPONSIVE_LIST_ITEM_COLUMN_DISPLAY_PRIORITY_HIGH" } } ] } ```
kerem 2026-02-27 23:01:04 +03:00
Author
Owner

@sigma67 commented on GitHub (Apr 2, 2025):

It takes data from flexColumns prop of data var. Video records have 2 flexColumns and views count is in the second column. Whereas song records have 3 columns: most data is in the second one and the views(plays) are in the third column.

Yeah because why should they even bother being a shred consistent about it 🤦‍♂️

PR welcome

<!-- gh-comment-id:2773366353 --> @sigma67 commented on GitHub (Apr 2, 2025): > It takes data from flexColumns prop of data var. Video records have 2 flexColumns and views count is in the second column. Whereas song records have 3 columns: most data is in the second one and the views(plays) are in the third column. Yeah because why should they even bother being a shred consistent about it 🤦‍♂️ PR welcome
Author
Owner

@sigma67 commented on GitHub (Apr 3, 2025):

The root cause here is that for whatever reason the plays data is provided in a separate flex column item.

<!-- gh-comment-id:2774907488 --> @sigma67 commented on GitHub (Apr 3, 2025): The root cause here is that for whatever reason the plays data is provided in a separate flex column item.
Author
Owner

@serg-bloim commented on GitHub (Apr 3, 2025):

Wow, I see you already addressed it. I wasn't confident filing a PR right away without a discussion.

Anyway, thanks for a quick fix!

<!-- gh-comment-id:2775158869 --> @serg-bloim commented on GitHub (Apr 3, 2025): Wow, I see you already addressed it. I wasn't confident filing a PR right away without a discussion. Anyway, thanks for a quick fix!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ytmusicapi#486
No description provided.