diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 47106a6..1447a6c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.2.0 + rev: v0.3.0 hooks: # Run the linter. - id: ruff diff --git a/docs/source/reference.rst b/docs/source/reference.rst index 3c8ca8f..bbba18f 100644 --- a/docs/source/reference.rst +++ b/docs/source/reference.rst @@ -61,6 +61,7 @@ Library .. automethod:: YTMusic.rate_playlist .. automethod:: YTMusic.subscribe_artists .. automethod:: YTMusic.unsubscribe_artists +.. automethod:: YTMusic.get_account_info Playlists --------- diff --git a/tests/README.rst b/tests/README.rst index 6df9acc..e4a1a8c 100644 --- a/tests/README.rst +++ b/tests/README.rst @@ -24,6 +24,9 @@ The entry descriptions should be self-explanatory. For the headers_raw, you need to indent the overflowing lines with a tab character. For the upload test you need a suitable music file in the test directory. Adjust the file to contain appropriate information for your YouTube account and local setup. +``account_name`` and ``channel_handle`` for test.cfg can be obtained by visiting either YouTube or YouTube Music and +clicking your profile picture icon in the top right while signed in with your test account. + Brand accounts can be created by first signing into the google account you wish to have as the parent/controlling account then navigating `here. `_ diff --git a/tests/mixins/test_library.py b/tests/mixins/test_library.py index ef79eac..b2e6052 100644 --- a/tests/mixins/test_library.py +++ b/tests/mixins/test_library.py @@ -1,3 +1,5 @@ +from urllib.parse import urlparse + import pytest @@ -109,3 +111,12 @@ class TestLibrary: def test_subscribe_artists(self, yt_auth): yt_auth.subscribe_artists(["UCUDVBtnOQi4c7E8jebpjc9Q"]) yt_auth.unsubscribe_artists(["UCUDVBtnOQi4c7E8jebpjc9Q"]) + + def test_get_account_info(self, config, yt, yt_oauth): + with pytest.raises(Exception, match="Please provide authentication"): + yt.get_account_info() + + account_info = yt_oauth.get_account_info() + assert account_info["accountName"] == config.get("auth", "account_name") + assert account_info["channelHandle"] == config.get("auth", "channel_handle") + assert bool(urlparse(account_info["accountPhotoUrl"])) diff --git a/tests/test.example.cfg b/tests/test.example.cfg index c593ea8..0b458c3 100644 --- a/tests/test.example.cfg +++ b/tests/test.example.cfg @@ -1,6 +1,8 @@ [auth] brand_account = 101234229123420379537 brand_account_empty = 1123456629123420379537 +account_name = Sample Account +channel_handle = @SampleAccount headers = headers_auth_json_as_string headers_empty = headers_account_with_empty_library_as_json_as_string browser_file = ./browser.json diff --git a/ytmusicapi/mixins/library.py b/ytmusicapi/mixins/library.py index 5f6e9fc..340ef9f 100644 --- a/ytmusicapi/mixins/library.py +++ b/ytmusicapi/mixins/library.py @@ -324,3 +324,45 @@ class LibraryMixin(MixinProtocol): body = {"channelIds": channelIds} endpoint = "subscription/unsubscribe" return self._send_request(endpoint, body) + + def get_account_info(self) -> Dict: + """ + Gets information about the currently authenticated user's account. + + :return: Dictionary with user's account name, channel handle, and URL of their account photo. + + Example:: + + { + "accountName": "Sample User", + "channelHandle": "@SampleUser + "accountPhotoUrl": "https://yt3.ggpht.com/sample-user-photo" + } + """ + self._check_auth() + endpoint = "account/account_menu" + response = self._send_request(endpoint, {}) + + ACCOUNT_INFO = [ + "actions", + 0, + "openPopupAction", + "popup", + "multiPageMenuRenderer", + "header", + "activeAccountHeaderRenderer", + ] + ACCOUNT_RUNS_TEXT = ["runs", 0, "text"] + ACCOUNT_NAME = [*ACCOUNT_INFO, "accountName", *ACCOUNT_RUNS_TEXT] + ACCOUNT_CHANNEL_HANDLE = [*ACCOUNT_INFO, "channelHandle", *ACCOUNT_RUNS_TEXT] + ACCOUNT_PHOTO_URL = [*ACCOUNT_INFO, "accountPhoto", "thumbnails", 0, "url"] + + account_name = nav(response, ACCOUNT_NAME) + channel_handle = nav(response, ACCOUNT_CHANNEL_HANDLE) + account_photo_url = nav(response, ACCOUNT_PHOTO_URL) + + return { + "accountName": account_name, + "channelHandle": channel_handle, + "accountPhotoUrl": account_photo_url, + }