[GH-ISSUE #205] How to get the moods and genres located in https://music.youtube.com/moods_and_genres #159

Closed
opened 2026-02-27 22:08:21 +03:00 by kerem · 3 comments
Owner

Originally created by @YashMakan on GitHub (Jun 9, 2021).
Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/205

I am looking for a way to get the moods and genres. How can I do that?

Originally created by @YashMakan on GitHub (Jun 9, 2021). Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/205 I am looking for a way to get the moods and genres. How can I do that?
kerem 2026-02-27 22:08:21 +03:00
Author
Owner

@impliedchaos commented on GitHub (Jun 9, 2021):

Something like this works:

from ytmusicapi import YTMusic
from ytmusicapi.parsers.utils import (nav, NAVIGATION_BROWSE_ID, SECTION_LIST, SINGLE_COLUMN_TAB, TITLE_TEXT)

ytm = YTMusic('auth.json')

# Get moods and genre categories.
response = ytm._send_request("browse", {"browseId": "FEmusic_moods_and_genres"})
moods = {}
for sect in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST):
    for cat in nav(sect, ["gridRenderer", "items"]):
        title = nav(cat, ["musicNavigationButtonRenderer", "buttonText", "runs", 0, "text"]).strip()
        endpnt = nav(cat, ["musicNavigationButtonRenderer", "clickCommand", "browseEndpoint", "browseId"])
        params = nav(cat, ["musicNavigationButtonRenderer", "clickCommand", "browseEndpoint", "params"])
        moods[title] = {"name": title, "params": params, "endpoint": endpnt}

# Print the list of categories.
for m in moods.keys():
    print(m)

# Get playlists from "Focus"
mood = "Focus"
response = ytm._send_request("browse", {"browseId": moods[mood]["endpoint"], "params": moods[mood]["params"]})
for sect in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST):
    key = []
    if "gridRenderer" in sect:
        key = ["gridRenderer", "items"]
    elif "musicCarouselShelfRenderer" in sect:
        key = ["musicCarouselShelfRenderer", "contents"]
    elif "musicImmersiveCarouselShelfRenderer" in sect:
        key = ["musicImmersiveCarouselShelfRenderer", "contents"]
    if len(key):
        for item in nav(sect, key):
            title = nav(item, ["musicTwoRowItemRenderer"] + TITLE_TEXT).strip()
            brId = nav(item, ["musicTwoRowItemRenderer"] + NAVIGATION_BROWSE_ID)
            print("Playlist Title: " + title + " - Playlist ID: " + brId)

Probably better off using find_object_by_key instead of nav all the time, but it's just an example.

<!-- gh-comment-id:857750241 --> @impliedchaos commented on GitHub (Jun 9, 2021): Something like this works: ``` from ytmusicapi import YTMusic from ytmusicapi.parsers.utils import (nav, NAVIGATION_BROWSE_ID, SECTION_LIST, SINGLE_COLUMN_TAB, TITLE_TEXT) ytm = YTMusic('auth.json') # Get moods and genre categories. response = ytm._send_request("browse", {"browseId": "FEmusic_moods_and_genres"}) moods = {} for sect in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST): for cat in nav(sect, ["gridRenderer", "items"]): title = nav(cat, ["musicNavigationButtonRenderer", "buttonText", "runs", 0, "text"]).strip() endpnt = nav(cat, ["musicNavigationButtonRenderer", "clickCommand", "browseEndpoint", "browseId"]) params = nav(cat, ["musicNavigationButtonRenderer", "clickCommand", "browseEndpoint", "params"]) moods[title] = {"name": title, "params": params, "endpoint": endpnt} # Print the list of categories. for m in moods.keys(): print(m) # Get playlists from "Focus" mood = "Focus" response = ytm._send_request("browse", {"browseId": moods[mood]["endpoint"], "params": moods[mood]["params"]}) for sect in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST): key = [] if "gridRenderer" in sect: key = ["gridRenderer", "items"] elif "musicCarouselShelfRenderer" in sect: key = ["musicCarouselShelfRenderer", "contents"] elif "musicImmersiveCarouselShelfRenderer" in sect: key = ["musicImmersiveCarouselShelfRenderer", "contents"] if len(key): for item in nav(sect, key): title = nav(item, ["musicTwoRowItemRenderer"] + TITLE_TEXT).strip() brId = nav(item, ["musicTwoRowItemRenderer"] + NAVIGATION_BROWSE_ID) print("Playlist Title: " + title + " - Playlist ID: " + brId) ``` Probably better off using `find_object_by_key` instead of `nav` all the time, but it's just an example.
Author
Owner

@sigma67 commented on GitHub (Jun 9, 2021):

I've thought about it but didn't find the time to implement it. Very open toward a PR for this

<!-- gh-comment-id:857841629 --> @sigma67 commented on GitHub (Jun 9, 2021): I've thought about it but didn't find the time to implement it. Very open toward a PR for this
Author
Owner

@YashMakan commented on GitHub (Jun 9, 2021):

Something like this works:

from ytmusicapi import YTMusic
from ytmusicapi.parsers.utils import (nav, NAVIGATION_BROWSE_ID, SECTION_LIST, SINGLE_COLUMN_TAB, TITLE_TEXT)

ytm = YTMusic('auth.json')

# Get moods and genre categories.
response = ytm._send_request("browse", {"browseId": "FEmusic_moods_and_genres"})
moods = {}
for sect in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST):
    for cat in nav(sect, ["gridRenderer", "items"]):
        title = nav(cat, ["musicNavigationButtonRenderer", "buttonText", "runs", 0, "text"]).strip()
        endpnt = nav(cat, ["musicNavigationButtonRenderer", "clickCommand", "browseEndpoint", "browseId"])
        params = nav(cat, ["musicNavigationButtonRenderer", "clickCommand", "browseEndpoint", "params"])
        moods[title] = {"name": title, "params": params, "endpoint": endpnt}

# Print the list of categories.
for m in moods.keys():
    print(m)

# Get playlists from "Focus"
mood = "Focus"
response = ytm._send_request("browse", {"browseId": moods[mood]["endpoint"], "params": moods[mood]["params"]})
for sect in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST):
    key = []
    if "gridRenderer" in sect:
        key = ["gridRenderer", "items"]
    elif "musicCarouselShelfRenderer" in sect:
        key = ["musicCarouselShelfRenderer", "contents"]
    elif "musicImmersiveCarouselShelfRenderer" in sect:
        key = ["musicImmersiveCarouselShelfRenderer", "contents"]
    if len(key):
        for item in nav(sect, key):
            title = nav(item, ["musicTwoRowItemRenderer"] + TITLE_TEXT).strip()
            brId = nav(item, ["musicTwoRowItemRenderer"] + NAVIGATION_BROWSE_ID)
            print("Playlist Title: " + title + " - Playlist ID: " + brId)

Probably better off using find_object_by_key instead of nav all the time, but it's just an example.

Thanks, it is working...

<!-- gh-comment-id:857953001 --> @YashMakan commented on GitHub (Jun 9, 2021): > Something like this works: > > ``` > from ytmusicapi import YTMusic > from ytmusicapi.parsers.utils import (nav, NAVIGATION_BROWSE_ID, SECTION_LIST, SINGLE_COLUMN_TAB, TITLE_TEXT) > > ytm = YTMusic('auth.json') > > # Get moods and genre categories. > response = ytm._send_request("browse", {"browseId": "FEmusic_moods_and_genres"}) > moods = {} > for sect in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST): > for cat in nav(sect, ["gridRenderer", "items"]): > title = nav(cat, ["musicNavigationButtonRenderer", "buttonText", "runs", 0, "text"]).strip() > endpnt = nav(cat, ["musicNavigationButtonRenderer", "clickCommand", "browseEndpoint", "browseId"]) > params = nav(cat, ["musicNavigationButtonRenderer", "clickCommand", "browseEndpoint", "params"]) > moods[title] = {"name": title, "params": params, "endpoint": endpnt} > > # Print the list of categories. > for m in moods.keys(): > print(m) > > # Get playlists from "Focus" > mood = "Focus" > response = ytm._send_request("browse", {"browseId": moods[mood]["endpoint"], "params": moods[mood]["params"]}) > for sect in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST): > key = [] > if "gridRenderer" in sect: > key = ["gridRenderer", "items"] > elif "musicCarouselShelfRenderer" in sect: > key = ["musicCarouselShelfRenderer", "contents"] > elif "musicImmersiveCarouselShelfRenderer" in sect: > key = ["musicImmersiveCarouselShelfRenderer", "contents"] > if len(key): > for item in nav(sect, key): > title = nav(item, ["musicTwoRowItemRenderer"] + TITLE_TEXT).strip() > brId = nav(item, ["musicTwoRowItemRenderer"] + NAVIGATION_BROWSE_ID) > print("Playlist Title: " + title + " - Playlist ID: " + brId) > ``` > > Probably better off using `find_object_by_key` instead of `nav` all the time, but it's just an example. Thanks, it is working...
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#159
No description provided.