Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the moods and genres located in https://music.youtube.com/moods_and_genres #205

Closed
YashMakan opened this issue Jun 9, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@YashMakan
Copy link

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

@impliedchaos
Copy link
Contributor

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.

@sigma67
Copy link
Owner

sigma67 commented Jun 9, 2021

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

@YashMakan
Copy link
Author

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...

@sigma67 sigma67 added the enhancement New feature or request label Jun 10, 2021
@sigma67 sigma67 closed this as completed in 5491fda Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants