Skip to content

Commit

Permalink
add get_user_videos (#632)
Browse files Browse the repository at this point in the history
* add get_user_videos

* add to docs
  • Loading branch information
sigma67 committed Jul 24, 2024
1 parent 858995e commit a15d90c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/source/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Browsing
.. automethod:: YTMusic.get_album_browse_id
.. automethod:: YTMusic.get_user
.. automethod:: YTMusic.get_user_playlists
.. automethod:: YTMusic.get_user_videos
.. automethod:: YTMusic.get_song
.. automethod:: YTMusic.get_song_related
.. automethod:: YTMusic.get_lyrics
Expand Down
9 changes: 9 additions & 0 deletions tests/mixins/test_browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def test_get_user_playlists(self, yt, yt_auth):
results_empty = yt.get_user_playlists(channel, user["playlists"]["params"])
assert len(results_empty) == 0

def test_get_user_videos(self, yt, yt_auth):
channel = "UCus8EVJ7Oc9zINhs-fg8l1Q" # Turbo
user = yt_auth.get_user(channel)
results = yt_auth.get_user_videos(channel, user["videos"]["params"])
assert len(results) > 100

results_empty = yt.get_user_videos(channel, user["videos"]["params"])
assert len(results_empty) == 0

def test_get_album_browse_id(self, yt, sample_album):
warnings.filterwarnings(action="ignore", category=DeprecationWarning)
browse_id = yt.get_album_browse_id("OLAK5uy_nMr9h2VlS-2PULNz3M3XVXQj_P3C2bqaY")
Expand Down
40 changes: 38 additions & 2 deletions ytmusicapi/mixins/browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
)
from ytmusicapi.helpers import YTM_DOMAIN, sum_total_duration
from ytmusicapi.parsers.albums import parse_album_header_2024
from ytmusicapi.parsers.browsing import parse_album, parse_content_list, parse_mixed_content, parse_playlist
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

Expand Down Expand Up @@ -355,6 +361,15 @@ 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.
Expand Down Expand Up @@ -413,7 +428,7 @@ def get_user_playlists(self, channelId: str, params: str) -> list[dict]:
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_artist`
:param params: params obtained by :py:func:`get_user`
:return: List of user playlists in the format of :py:func:`get_library_playlists`
"""
Expand All @@ -428,6 +443,27 @@ def get_user_playlists(self, channelId: str, params: str) -> list[dict]:

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
Expand Down

0 comments on commit a15d90c

Please sign in to comment.