Implement get_account_info() to get authenticated user's account info (#556) (#557)

* implement get_account_info to provide information about the authenticated user's account (#556)

* Apply suggestions from code review to tests/README.rst

Co-authored-by: sigma67 <16363825+sigma67@users.noreply.github.com>

* simplify get_account_info by moving code out of parsers.py and navigation.py

* test with non-auth account

* format

* update .pre-commit-config.yaml

---------

Co-authored-by: sigma67 <16363825+sigma67@users.noreply.github.com>
Co-authored-by: sigma67 <sigma67.github@gmail.com>
This commit is contained in:
Alex Pastel 2024-03-03 11:55:06 -07:00 • committed by GitHub
commit c412d7ce34
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 60 additions and 1 deletions

View file

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

View file

@ -61,6 +61,7 @@ Library
.. automethod:: YTMusic.rate_playlist
.. automethod:: YTMusic.subscribe_artists
.. automethod:: YTMusic.unsubscribe_artists
.. automethod:: YTMusic.get_account_info
Playlists
---------

View file

@ -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. <https://www.youtube.com/create_channel?action_create_new_channel_redirect=true>`_

View file

@ -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"]))

View file

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

View file

@ -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,
}