1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
|
import re
import warnings
from typing import Any, Optional, Union, cast, overload
from ytmusicapi.continuations import (
get_continuations,
get_reloadable_continuation_params,
)
from ytmusicapi.helpers import YTM_DOMAIN, sum_total_duration
from ytmusicapi.models.lyrics import LyricLine, Lyrics, TimedLyrics
from ytmusicapi.parsers.albums import parse_album_header_2024
from ytmusicapi.parsers.browsing import (
parse_album,
parse_content_list,
parse_mixed_content,
parse_playlist,
parse_video,
)
from ytmusicapi.parsers.library import parse_albums
from ytmusicapi.parsers.playlists import parse_playlist_items
from ..exceptions import YTMusicError, YTMusicUserError
from ..navigation import *
from ._protocol import MixinProtocol
from ._utils import get_datestamp
class BrowsingMixin(MixinProtocol):
def get_home(self, limit=3) -> list[dict]:
"""
Get the home page.
The home page is structured as titled rows, returning 3 rows of music suggestions at a time.
Content varies and may contain artist, album, song or playlist suggestions, sometimes mixed within the same row
:param limit: Number of rows to return
:return: List of dictionaries keyed with 'title' text and 'contents' list
Example list::
[
{
"title": "Your morning music",
"contents": [
{ //album result
"title": "Sentiment",
"browseId": "MPREb_QtqXtd2xZMR",
"thumbnails": [...]
},
{ //playlist result
"title": "r/EDM top submissions 01/28/2022",
"playlistId": "PLz7-xrYmULdSLRZGk-6GKUtaBZcgQNwel",
"thumbnails": [...],
"description": "redditEDM • 161 songs",
"count": "161",
"author": [
{
"name": "redditEDM",
"id": "UCaTrZ9tPiIGHrkCe5bxOGwA"
}
]
}
]
},
{
"title": "Your favorites",
"contents": [
{ //artist result
"title": "Chill Satellite",
"browseId": "UCrPLFBWdOroD57bkqPbZJog",
"subscribers": "374",
"thumbnails": [...]
}
{ //album result
"title": "Dragon",
"year": "Two Steps From Hell",
"browseId": "MPREb_M9aDqLRbSeg",
"thumbnails": [...]
}
]
},
{
"title": "Quick picks",
"contents": [
{ //song quick pick
"title": "Gravity",
"videoId": "EludZd6lfts",
"artists": [{
"name": "yetep",
"id": "UCSW0r7dClqCoCvQeqXiZBlg"
}],
"thumbnails": [...],
"album": {
"name": "Gravity",
"id": "MPREb_D6bICFcuuRY"
}
},
{ //video quick pick
"title": "Gryffin & Illenium (feat. Daya) - Feel Good (L3V3LS Remix)",
"videoId": "bR5l0hJDnX8",
"artists": [
{
"name": "L3V3LS",
"id": "UCCVNihbOdkOWw_-ajIYhAbQ"
}
],
"thumbnails": [...],
"views": "10M"
}
]
}
]
"""
endpoint = "browse"
body = {"browseId": "FEmusic_home"}
response = self._send_request(endpoint, body)
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST)
home = []
home.extend(parse_mixed_content(results))
section_list = nav(response, [*SINGLE_COLUMN_TAB, "sectionListRenderer"])
if "continuations" in section_list:
request_func = lambda additionalParams: self._send_request(endpoint, body, additionalParams)
parse_func = lambda contents: parse_mixed_content(contents)
home.extend(
get_continuations(
section_list, "sectionListContinuation", limit - len(home), request_func, parse_func
)
)
return home
def get_artist(self, channelId: str) -> dict:
"""
Get information about an artist and their top releases (songs,
albums, singles, videos, and related artists). The top lists
contain pointers for getting the full list of releases.
Possible content types for get_artist are:
- songs
- albums
- singles
- shows
- videos
- episodes
- podcasts
- related
Each of these content keys in the response contains
``results`` and possibly ``browseId`` and ``params``.
- For songs/videos, pass the browseId to :py:func:`get_playlist`.
- For albums/singles/shows, pass browseId and params to :py:func:`get_artist_albums`.
:param channelId: channel id of the artist
:return: Dictionary with requested information.
.. warning::
The returned channelId is not the same as the one passed to the function.
It should be used only with :py:func:`subscribe_artists`.
Example::
{
"description": "Oasis were ...",
"views": "3,693,390,359 views",
"name": "Oasis",
"channelId": "UCUDVBtnOQi4c7E8jebpjc9Q",
"shuffleId": "RDAOkjHYJjL1a3xspEyVkhHAsg",
"radioId": "RDEMkjHYJjL1a3xspEyVkhHAsg",
"subscribers": "3.86M",
"subscribed": false,
"thumbnails": [...],
"songs": {
"browseId": "VLPLMpM3Z0118S42R1npOhcjoakLIv1aqnS1",
"results": [
{
"videoId": "ZrOKjDZOtkA",
"title": "Wonderwall (Remastered)",
"thumbnails": [...],
"artist": "Oasis",
"album": "(What's The Story) Morning Glory? (Remastered)"
}
]
},
"albums": {
"results": [
{
"title": "Familiar To Millions",
"thumbnails": [...],
"year": "2018",
"browseId": "MPREb_AYetWMZunqA"
}
],
"browseId": "UCmMUZbaYdNH0bEd1PAlAqsA",
"params": "6gPTAUNwc0JDbndLYlFBQV..."
},
"singles": {
"results": [
{
"title": "Stand By Me (Mustique Demo)",
"thumbnails": [...],
"year": "2016",
"browseId": "MPREb_7MPKLhibN5G"
}
],
"browseId": "UCmMUZbaYdNH0bEd1PAlAqsA",
"params": "6gPTAUNwc0JDbndLYlFBQV..."
},
"videos": {
"results": [
{
"title": "Wonderwall",
"thumbnails": [...],
"views": "358M",
"videoId": "bx1Bh8ZvH84",
"playlistId": "PLMpM3Z0118S5xuNckw1HUcj1D021AnMEB"
}
],
"browseId": "VLPLMpM3Z0118S5xuNckw1HUcj1D021AnMEB"
},
"related": {
"results": [
{
"browseId": "UCt2KxZpY5D__kapeQ8cauQw",
"subscribers": "450K",
"title": "The Verve"
},
{
"browseId": "UCwK2Grm574W1u-sBzLikldQ",
"subscribers": "341K",
"title": "Liam Gallagher"
},
...
]
}
}
"""
if channelId.startswith("MPLA"):
channelId = channelId[4:]
body = {"browseId": channelId}
endpoint = "browse"
response = self._send_request(endpoint, body)
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST)
artist: dict[str, Any] = {"description": None, "views": None}
header = response["header"]["musicImmersiveHeaderRenderer"]
artist["name"] = nav(header, TITLE_TEXT)
descriptionShelf = find_object_by_key(results, DESCRIPTION_SHELF[0], is_key=True)
if descriptionShelf:
artist["description"] = nav(descriptionShelf, DESCRIPTION)
artist["views"] = (
None
if "subheader" not in descriptionShelf
else descriptionShelf["subheader"]["runs"][0]["text"]
)
subscription_button = header["subscriptionButton"]["subscribeButtonRenderer"]
artist["channelId"] = subscription_button["channelId"]
artist["shuffleId"] = nav(header, ["playButton", "buttonRenderer", *NAVIGATION_PLAYLIST_ID], True)
artist["radioId"] = nav(header, ["startRadioButton", "buttonRenderer", *NAVIGATION_PLAYLIST_ID], True)
artist["subscribers"] = nav(subscription_button, ["subscriberCountText", "runs", 0, "text"], True)
artist["subscribed"] = subscription_button["subscribed"]
artist["thumbnails"] = nav(header, THUMBNAILS, True)
artist["songs"] = {"browseId": None}
if "musicShelfRenderer" in results[0]: # API sometimes does not return songs
musicShelf = nav(results[0], MUSIC_SHELF)
if "navigationEndpoint" in nav(musicShelf, TITLE):
artist["songs"]["browseId"] = nav(musicShelf, TITLE + NAVIGATION_BROWSE_ID)
artist["songs"]["results"] = parse_playlist_items(musicShelf["contents"])
artist.update(self.parser.parse_channel_contents(results))
return artist
ArtistOrderType = Literal["Recency", "Popularity", "Alphabetical order"]
def get_artist_albums(
self, channelId: str, params: str, limit: Optional[int] = 100, order: Optional[ArtistOrderType] = None
) -> list[dict]:
"""
Get the full list of an artist's albums, singles or shows
:param channelId: browseId of the artist as returned by :py:func:`get_artist`
:param params: params obtained by :py:func:`get_artist`
:param limit: Number of albums to return. ``None`` retrieves them all. Default: 100
:param order: Order of albums to return. Allowed values: ``Recency``, ``Popularity``, `Alphabetical order`. Default: Default order.
:return: List of albums in the format of :py:func:`get_library_albums`,
except artists key is missing.
"""
body = {"browseId": channelId, "params": params}
endpoint = "browse"
response = self._send_request(endpoint, body)
request_func = lambda additionalParams: self._send_request(endpoint, body, additionalParams)
parse_func = lambda contents: parse_albums(contents)
if order:
# pick the correct continuation from response depending on the order chosen
sort_options = nav(
response,
SINGLE_COLUMN_TAB
+ SECTION
+ HEADER_SIDE
+ [
"endItems",
0,
"musicSortFilterButtonRenderer",
"menu",
"musicMultiSelectMenuRenderer",
"options",
],
)
continuation = next(
(
nav(
option,
[
*MULTI_SELECT,
"selectedCommand",
"commandExecutorCommand",
"commands",
-1,
"browseSectionListReloadEndpoint",
],
)
for option in sort_options
if nav(option, MULTI_SELECT + TITLE_TEXT).lower() == order.lower()
),
None,
)
# if a valid order was provided, request continuation and replace original response
if continuation:
additionalParams = get_reloadable_continuation_params(
{"continuations": [continuation["continuation"]]}
)
response = request_func(additionalParams)
results = nav(response, SECTION_LIST_CONTINUATION + CONTENT)
else:
raise ValueError(f"Invalid order parameter {order}")
else:
# just use the results from the first request
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST_ITEM)
contents = nav(results, GRID_ITEMS, True) or nav(results, CAROUSEL_CONTENTS)
albums = parse_albums(contents)
results = nav(results, GRID, True)
if "continuations" in results:
remaining_limit = None if limit is None else (limit - len(albums))
albums.extend(
get_continuations(results, "gridContinuation", remaining_limit, request_func, parse_func)
)
return albums
def get_user(self, channelId: str) -> dict:
"""
Retrieve a user's page. A user may own videos or playlists.
Use :py:func:`get_user_playlists` to retrieve all playlists::
result = get_user(channelId)
get_user_playlists(channelId, result["playlists"]["params"])
Similarly, use :py:func:`get_user_videos` to retrieve all videos::
get_user_videos(channelId, result["videos"]["params"])
:param channelId: channelId of the user
:return: Dictionary with information about a user.
Example::
{
"name": "4Tune - No Copyright Music",
"videos": {
"browseId": "UC44hbeRoCZVVMVg5z0FfIww",
"results": [
{
"title": "Epic Music Soundtracks 2019",
"videoId": "bJonJjgS2mM",
"playlistId": "RDAMVMbJonJjgS2mM",
"thumbnails": [
{
"url": "https://i.ytimg.com/vi/bJon...",
"width": 800,
"height": 450
}
],
"views": "19K"
}
]
},
"playlists": {
"browseId": "UC44hbeRoCZVVMVg5z0FfIww",
"results": [
{
"title": "♚ Machinimasound | Playlist",
"playlistId": "PLRm766YvPiO9ZqkBuEzSTt6Bk4eWIr3gB",
"thumbnails": [
{
"url": "https://i.ytimg.com/vi/...",
"width": 400,
"height": 225
}
]
}
],
"params": "6gO3AUNvWU..."
}
}
"""
endpoint = "browse"
body = {"browseId": channelId}
response = self._send_request(endpoint, body)
user = {"name": nav(response, [*HEADER_MUSIC_VISUAL, *TITLE_TEXT])}
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST)
user.update(self.parser.parse_channel_contents(results))
return user
def get_user_playlists(self, channelId: str, params: str) -> list[dict]:
"""
Retrieve a list of playlists for a given user.
Call this function again with the returned ``params`` to get the full list.
:param channelId: channelId of the user.
:param params: params obtained by :py:func:`get_user`
:return: List of user playlists in the format of :py:func:`get_library_playlists`
"""
endpoint = "browse"
body = {"browseId": channelId, "params": params}
response = self._send_request(endpoint, body)
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST_ITEM + GRID_ITEMS, True)
if not results:
return []
user_playlists = parse_content_list(results, parse_playlist)
return user_playlists
def get_user_videos(self, channelId: str, params: str) -> list[dict]:
"""
Retrieve a list of videos for a given user.
Call this function again with the returned ``params`` to get the full list.
:param channelId: channelId of the user.
:param params: params obtained by :py:func:`get_user`
:return: List of user videos
"""
endpoint = "browse"
body = {"browseId": channelId, "params": params}
response = self._send_request(endpoint, body)
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST_ITEM + GRID_ITEMS, True)
if not results:
return []
user_videos = parse_content_list(results, parse_video)
return user_videos
def get_album_browse_id(self, audioPlaylistId: str) -> Optional[str]:
"""
Get an album's browseId based on its audioPlaylistId
:param audioPlaylistId: id of the audio playlist (starting with `OLAK5uy_`)
:return: browseId (starting with ``MPREb_``)
"""
params = {"list": audioPlaylistId}
response = self._send_get_request(YTM_DOMAIN + "/playlist", params)
with warnings.catch_warnings():
# merge this with statement with catch_warnings on Python>=3.11
warnings.simplefilter(action="ignore", category=DeprecationWarning)
decoded = response.text.encode("utf8").decode("unicode_escape")
matches = re.search(r"\"MPRE.+?\"", decoded)
browse_id = None
if matches:
browse_id = matches.group().strip('"')
return browse_id
def get_album(self, browseId: str) -> dict:
"""
Get information and tracks of an album
:param browseId: browseId of the album, for example
returned by :py:func:`search`
:return: Dictionary with album and track metadata.
The result is in the following format::
{
"title": "Revival",
"type": "Album",
"thumbnails": [],
"description": "Revival is the...",
"artists": [
{
"name": "Eminem",
"id": "UCedvOgsKFzcK3hA5taf3KoQ"
}
],
"year": "2017",
"trackCount": 19,
"duration": "1 hour, 17 minutes",
"audioPlaylistId": "OLAK5uy_nMr9h2VlS-2PULNz3M3XVXQj_P3C2bqaY",
"tracks": [
{
"videoId": "iKLU7z_xdYQ",
"title": "Walk On Water (feat. Beyoncé)",
"artists": [
{
"name": "Eminem",
"id": "UCedvOgsKFzcK3hA5taf3KoQ"
}
],
"album": "Revival",
"likeStatus": "INDIFFERENT",
"thumbnails": null,
"isAvailable": true,
"isExplicit": true,
"duration": "5:03",
"duration_seconds": 303,
"trackNumber": 0,
"feedbackTokens": {
"add": "AB9zfpK...",
"remove": "AB9zfpK..."
}
}
],
"other_versions": [
{
"title": "Revival",
"year": "Eminem",
"browseId": "MPREb_fefKFOTEZSp",
"thumbnails": [...],
"isExplicit": false
},
],
"duration_seconds": 4657
}
"""
if not browseId or not browseId.startswith("MPRE"):
raise YTMusicUserError("Invalid album browseId provided, must start with MPRE.")
body = {"browseId": browseId}
endpoint = "browse"
response = self._send_request(endpoint, body)
album = parse_album_header_2024(response)
results = nav(response, [*TWO_COLUMN_RENDERER, "secondaryContents", *SECTION_LIST_ITEM, *MUSIC_SHELF])
album["tracks"] = parse_playlist_items(results["contents"], is_album=True)
other_versions = nav(
response, [*TWO_COLUMN_RENDERER, "secondaryContents", *SECTION_LIST, 1, *CAROUSEL], True
)
if other_versions is not None:
album["other_versions"] = parse_content_list(other_versions["contents"], parse_album)
album["duration_seconds"] = sum_total_duration(album)
for i, track in enumerate(album["tracks"]):
album["tracks"][i]["album"] = album["title"]
album["tracks"][i]["artists"] = album["tracks"][i]["artists"] or album["artists"]
return album
def get_song(self, videoId: str, signatureTimestamp: Optional[int] = None) -> dict:
"""
Returns metadata and streaming information about a song or video.
:param videoId: Video id
:param signatureTimestamp: Provide the current YouTube signatureTimestamp.
If not provided a default value will be used, which might result in invalid streaming URLs
:return: Dictionary with song metadata.
Example::
{
"playabilityStatus": {
"status": "OK",
"playableInEmbed": true,
"audioOnlyPlayability": {
"audioOnlyPlayabilityRenderer": {
"trackingParams": "CAEQx2kiEwiuv9X5i5H1AhWBvlUKHRoZAHk=",
"audioOnlyAvailability": "FEATURE_AVAILABILITY_ALLOWED"
}
},
"miniplayer": {
"miniplayerRenderer": {
"playbackMode": "PLAYBACK_MODE_ALLOW"
}
},
"contextParams": "Q0FBU0FnZ0M="
},
"streamingData": {
"expiresInSeconds": "21540",
"adaptiveFormats": [
{
"itag": 140,
"url": "https://rr1---sn-h0jelnez.c.youtube.com/videoplayback?expire=1641080272...",
"mimeType": "audio/mp4; codecs=\"mp4a.40.2\"",
"bitrate": 131007,
"initRange": {
"start": "0",
"end": "667"
},
"indexRange": {
"start": "668",
"end": "999"
},
"lastModified": "1620321966927796",
"contentLength": "3967382",
"quality": "tiny",
"projectionType": "RECTANGULAR",
"averageBitrate": 129547,
"highReplication": true,
"audioQuality": "AUDIO_QUALITY_MEDIUM",
"approxDurationMs": "245000",
"audioSampleRate": "44100",
"audioChannels": 2,
"loudnessDb": -1.3000002
}
]
},
"playbackTracking": {
"videostatsPlaybackUrl": {
"baseUrl": "https://s.youtube.com/api/stats/playback?cl=491307275&docid=AjXQiKP5kMs&ei=Nl2HY-6MH5WE8gPjnYnoDg&fexp=1714242%2C9405963%2C23804281%2C23858057%2C23880830%2C23880833%2C23882685%2C23918597%2C23934970%2C23946420%2C23966208%2C23983296%2C23998056%2C24001373%2C24002022%2C24002025%2C24004644%2C24007246%2C24034168%2C24036947%2C24077241%2C24080738%2C24120820%2C24135310%2C24135692%2C24140247%2C24161116%2C24162919%2C24164186%2C24169501%2C24175560%2C24181174%2C24187043%2C24187377%2C24187854%2C24191629%2C24197450%2C24199724%2C24200839%2C24209349%2C24211178%2C24217535%2C24219713%2C24224266%2C24241378%2C24248091%2C24248956%2C24255543%2C24255545%2C24262346%2C24263796%2C24265426%2C24267564%2C24268142%2C24279196%2C24280220%2C24283426%2C24283493%2C24287327%2C24288045%2C24290971%2C24292955%2C24293803%2C24299747%2C24390674%2C24391018%2C24391537%2C24391709%2C24392268%2C24392363%2C24392401%2C24401557%2C24402891%2C24403794%2C24406605%2C24407200%2C24407665%2C24407914%2C24408220%2C24411766%2C24413105%2C24413820%2C24414162%2C24415866%2C24416354%2C24420756%2C24421162%2C24425861%2C24428962%2C24590921%2C39322504%2C39322574%2C39322694%2C39322707&ns=yt&plid=AAXusD4TIOMjS5N4&el=detailpage&len=246&of=Jx1iRksbq-rB9N1KSijZLQ&osid=MWU2NzBjYTI%3AAOeUNAagU8UyWDUJIki5raGHy29-60-yTA&uga=29&vm=CAEQABgEOjJBUEV3RWxUNmYzMXNMMC1MYVpCVnRZTmZWMWw1OWVZX2ZOcUtCSkphQ245VFZwOXdTQWJbQVBta0tETEpWNXI1SlNIWEJERXdHeFhXZVllNXBUemt5UHR4WWZEVzFDblFUSmdla3BKX2R0dXk3bzFORWNBZmU5YmpYZnlzb3doUE5UU0FoVGRWa0xIaXJqSWgB",
"headers": [
{
"headerType": "USER_AUTH"
},
{
"headerType": "VISITOR_ID"
},
{
"headerType": "PLUS_PAGE_ID"
}
]
},
"videostatsDelayplayUrl": {(as above)},
"videostatsWatchtimeUrl": {(as above)},
"ptrackingUrl": {(as above)},
"qoeUrl": {(as above)},
"atrUrl": {(as above)},
"videostatsScheduledFlushWalltimeSeconds": [
10,
20,
30
],
"videostatsDefaultFlushIntervalSeconds": 40
},
"videoDetails": {
"videoId": "AjXQiKP5kMs",
"title": "Sparks",
"lengthSeconds": "245",
"channelId": "UCvCk2zFqkCYzpnSgWfx0qOg",
"isOwnerViewing": false,
"isCrawlable": false,
"thumbnail": {
"thumbnails": []
},
"allowRatings": true,
"viewCount": "12",
"author": "Thomas Bergersen",
"isPrivate": true,
"isUnpluggedCorpus": false,
"musicVideoType": "MUSIC_VIDEO_TYPE_PRIVATELY_OWNED_TRACK",
"isLiveContent": false
},
"microformat": {
"microformatDataRenderer": {
"urlCanonical": "https://music.youtube.com/watch?v=AjXQiKP5kMs",
"title": "Sparks - YouTube Music",
"description": "Uploaded to YouTube via YouTube Music Sparks",
"thumbnail": {
"thumbnails": [
{
"url": "https://i.ytimg.com/vi/AjXQiKP5kMs/hqdefault.jpg",
"width": 480,
"height": 360
}
]
},
"siteName": "YouTube Music",
"appName": "YouTube Music",
"androidPackage": "com.google.android.apps.youtube.music",
"iosAppStoreId": "1017492454",
"iosAppArguments": "https://music.youtube.com/watch?v=AjXQiKP5kMs",
"ogType": "video.other",
"urlApplinksIos": "vnd.youtube.music://music.youtube.com/watch?v=AjXQiKP5kMs&feature=applinks",
"urlApplinksAndroid": "vnd.youtube.music://music.youtube.com/watch?v=AjXQiKP5kMs&feature=applinks",
"urlTwitterIos": "vnd.youtube.music://music.youtube.com/watch?v=AjXQiKP5kMs&feature=twitter-deep-link",
"urlTwitterAndroid": "vnd.youtube.music://music.youtube.com/watch?v=AjXQiKP5kMs&feature=twitter-deep-link",
"twitterCardType": "player",
"twitterSiteHandle": "@YouTubeMusic",
"schemaDotOrgType": "http://schema.org/VideoObject",
"noindex": true,
"unlisted": true,
"paid": false,
"familySafe": true,
"pageOwnerDetails": {
"name": "Music Library Uploads",
"externalChannelId": "UCvCk2zFqkCYzpnSgWfx0qOg",
"youtubeProfileUrl": "http://www.youtube.com/channel/UCvCk2zFqkCYzpnSgWfx0qOg"
},
"videoDetails": {
"externalVideoId": "AjXQiKP5kMs",
"durationSeconds": "246",
"durationIso8601": "PT4M6S"
},
"linkAlternates": [
{
"hrefUrl": "android-app://com.google.android.youtube/http/youtube.com/watch?v=AjXQiKP5kMs"
},
{
"hrefUrl": "ios-app://544007664/http/youtube.com/watch?v=AjXQiKP5kMs"
},
{
"hrefUrl": "https://www.youtube.com/oembed?format=json&url=https%3A%2F%2Fmusic.youtube.com%2Fwatch%3Fv%3DAjXQiKP5kMs",
"title": "Sparks",
"alternateType": "application/json+oembed"
},
{
"hrefUrl": "https://www.youtube.com/oembed?format=xml&url=https%3A%2F%2Fmusic.youtube.com%2Fwatch%3Fv%3DAjXQiKP5kMs",
"title": "Sparks",
"alternateType": "text/xml+oembed"
}
],
"viewCount": "12",
"publishDate": "1969-12-31",
"category": "Music",
"uploadDate": "1969-12-31"
}
}
}
"""
endpoint = "player"
if not signatureTimestamp:
signatureTimestamp = get_datestamp() - 1
params = {
"playbackContext": {"contentPlaybackContext": {"signatureTimestamp": signatureTimestamp}},
"video_id": videoId,
}
response = self._send_request(endpoint, params)
keys = ["videoDetails", "playabilityStatus", "streamingData", "microformat", "playbackTracking"]
for k in list(response.keys()):
if k not in keys:
del response[k]
return response
def get_song_related(self, browseId: str):
"""
Gets related content for a song. Equivalent to the content
shown in the "Related" tab of the watch panel.
:param browseId: The ``related`` key in the ``get_watch_playlist`` response.
Example::
[
{
"title": "You might also like",
"contents": [
{
"title": "High And Dry",
"videoId": "7fv84nPfTH0",
"artists": [{
"name": "Radiohead",
"id": "UCr_iyUANcn9OX_yy9piYoLw"
}],
"thumbnails": [
{
"url": "https://lh3.googleusercontent.com/TWWT47cHLv3yAugk4h9eOzQ46FHmXc_g-KmBVy2d4sbg_F-Gv6xrPglztRVzp8D_l-yzOnvh-QToM8s=w60-h60-l90-rj",
"width": 60,
"height": 60
}
],
"isExplicit": false,
"album": {
"name": "The Bends",
"id": "MPREb_xsmDKhqhQrG"
}
}
]
},
{
"title": "Recommended playlists",
"contents": [
{
"title": "'90s Alternative Rock Hits",
"playlistId": "RDCLAK5uy_m_h-nx7OCFaq9AlyXv78lG0AuloqW_NUA",
"thumbnails": [...],
"description": "Playlist • YouTube Music"
}
]
},
{
"title": "Similar artists",
"contents": [
{
"title": "Noel Gallagher",
"browseId": "UCu7yYcX_wIZgG9azR3PqrxA",
"subscribers": "302K",
"thumbnails": [...]
}
]
},
{
"title": "Oasis",
"contents": [
{
"title": "Shakermaker",
"year": "2014",
"browseId": "MPREb_WNGQWp5czjD",
"thumbnails": [...]
}
]
},
{
"title": "About the artist",
"contents": "Oasis were a rock band consisting of Liam Gallagher, Paul ... (full description shortened for documentation)"
}
]
"""
if not browseId:
raise YTMusicUserError("Invalid browseId provided.")
response = self._send_request("browse", {"browseId": browseId})
sections = nav(response, ["contents", *SECTION_LIST])
return parse_mixed_content(sections)
@overload
def get_lyrics(self, browseId: str, timestamps: Literal[False] = False) -> Optional[Lyrics]:
"""overload for mypy only"""
@overload
def get_lyrics(
self, browseId: str, timestamps: Literal[True] = True
) -> Optional[Union[Lyrics, TimedLyrics]]:
"""overload for mypy only"""
def get_lyrics(
self, browseId: str, timestamps: Optional[bool] = False
) -> Optional[Union[Lyrics, TimedLyrics]]:
"""
Returns lyrics of a song or video. When `timestamps` is set, lyrics are returned with
timestamps, if available.
:param browseId: Lyrics browseId obtained from :py:func:`get_watch_playlist` (startswith ``MPLYt...``).
:param timestamps: Optional. Whether to return bare lyrics or lyrics with timestamps, if available. (Default: `False`)
:return: Dictionary with song lyrics or ``None``, if no lyrics are found.
The ``hasTimestamps``-key determines the format of the data.
Example when `timestamps=False`, or no timestamps are available::
{
"lyrics": "Today is gonna be the day\\nThat they're gonna throw it back to you\\n",
"source": "Source: LyricFind",
"hasTimestamps": False
}
Example when `timestamps` is set to `True` and timestamps are available::
{
"lyrics": [
LyricLine(
text="I was a liar",
start_time=9200,
end_time=10630,
id=1
),
LyricLine(
text="I gave in to the fire",
start_time=10680,
end_time=12540,
id=2
),
],
"source": "Source: LyricFind",
"hasTimestamps": True
}
"""
if not browseId:
raise YTMusicUserError("Invalid browseId provided. This song might not have lyrics.")
if timestamps:
# changes and restores the client to get lyrics with timestamps (mobile only)
with self.as_mobile():
response = self._send_request("browse", {"browseId": browseId})
else:
response = self._send_request("browse", {"browseId": browseId})
# unpack the response
lyrics: Union[Lyrics, TimedLyrics]
if timestamps and (data := nav(response, TIMESTAMPED_LYRICS, True)) is not None:
# we got lyrics with timestamps
assert isinstance(data, dict)
if "timedLyricsData" not in data: # pragma: no cover
return None
lyrics = TimedLyrics(
lyrics=list(map(LyricLine.from_raw, data["timedLyricsData"])),
source=data.get("sourceMessage"),
hasTimestamps=True,
)
else:
lyrics_str = nav(
response, ["contents", *SECTION_LIST_ITEM, *DESCRIPTION_SHELF, *DESCRIPTION], True
)
if lyrics_str is None: # pragma: no cover
return None
lyrics = Lyrics(
lyrics=lyrics_str,
source=nav(response, ["contents", *SECTION_LIST_ITEM, *DESCRIPTION_SHELF, *RUN_TEXT], True),
hasTimestamps=False,
)
return cast(Union[Lyrics, TimedLyrics], lyrics)
def get_basejs_url(self) -> str:
"""
Extract the URL for the `base.js` script from YouTube Music.
:return: URL to `base.js`
"""
response = self._send_get_request(url=YTM_DOMAIN)
match = re.search(r'jsUrl"\s*:\s*"([^"]+)"', response.text)
if match is None:
raise YTMusicError("Could not identify the URL for base.js player.")
return YTM_DOMAIN + match.group(1)
def get_signatureTimestamp(self, url: Optional[str] = None) -> int:
"""
Fetch the `base.js` script from YouTube Music and parse out the
``signatureTimestamp`` for use with :py:func:`get_song`.
:param url: Optional. Provide the URL of the `base.js` script. If this
isn't specified a call will be made to :py:func:`get_basejs_url`.
:return: ``signatureTimestamp`` string
"""
if url is None:
url = self.get_basejs_url()
response = self._send_get_request(url=url)
match = re.search(r"signatureTimestamp[:=](\d+)", response.text)
if match is None:
raise YTMusicError("Unable to identify the signatureTimestamp.")
return int(match.group(1))
def get_tasteprofile(self) -> dict:
"""
Fetches suggested artists from taste profile (music.youtube.com/tasteprofile).
Tasteprofile allows users to pick artists to update their recommendations.
Only returns a list of suggested artists, not the actual list of selected entries
:return: Dictionary with artist and their selection & impression value
Example::
{
"Drake": {
"selectionValue": "tastebuilder_selection=/m/05mt_q"
"impressionValue": "tastebuilder_impression=/m/05mt_q"
}
}
"""
response = self._send_request("browse", {"browseId": "FEmusic_tastebuilder"})
profiles = nav(response, TASTE_PROFILE_ITEMS)
taste_profiles = {}
for itemList in profiles:
for item in itemList["tastebuilderItemListRenderer"]["contents"]:
artist = nav(item["tastebuilderItemRenderer"], TASTE_PROFILE_ARTIST)[0]["text"]
taste_profiles[artist] = {
"selectionValue": item["tastebuilderItemRenderer"]["selectionFormValue"],
"impressionValue": item["tastebuilderItemRenderer"]["impressionFormValue"],
}
return taste_profiles
def set_tasteprofile(self, artists: list[str], taste_profile: Optional[dict] = None) -> None:
"""
Favorites artists to see more recommendations from the artist.
Use :py:func:`get_tasteprofile` to see which artists are available to be recommended
:param artists: A List with names of artists, must be contained in the tasteprofile
:param taste_profile: tasteprofile result from :py:func:`get_tasteprofile`.
Pass this if you call :py:func:`get_tasteprofile` anyway to save an extra request.
:return: None if successful
"""
if taste_profile is None:
taste_profile = self.get_tasteprofile()
formData = {
"impressionValues": [taste_profile[profile]["impressionValue"] for profile in taste_profile],
"selectedValues": [],
}
for artist in artists:
if artist not in taste_profile:
raise YTMusicUserError(f"The artist {artist} was not present in taste!")
formData["selectedValues"].append(taste_profile[artist]["selectionValue"])
body = {"browseId": "FEmusic_home", "formData": formData}
self._send_request("browse", body)
|