Skip to content

Commit

Permalink
Extract song count from text (#609)
Browse files Browse the repository at this point in the history
* Extract song count from text

* Add fix in parsers

* add test

---------

Co-authored-by: sigma67 <sigma67.github@gmail.com>
  • Loading branch information
JohnHKoh and sigma67 authored Jul 13, 2024
1 parent 8cc72e3 commit 08d9f30
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 9 additions & 0 deletions tests/mixins/test_playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import pytest

from ytmusicapi import YTMusic
from ytmusicapi.constants import SUPPORTED_LANGUAGES


class TestPlaylists:
@pytest.mark.parametrize(
Expand Down Expand Up @@ -57,6 +60,12 @@ def test_get_playlist_foreign_new_format(self, yt_empty):
assert len(playlist["tracks"]) > 200
assert len(playlist["related"]) == 0

@pytest.mark.parametrize("language", SUPPORTED_LANGUAGES)
def test_get_playlist_languages(self, language):
yt = YTMusic(language=language)
result = yt.get_playlist("PLj4BSJLnVpNyIjbCWXWNAmybc97FXLlTk")
assert result["trackCount"] == 255

def test_get_playlist_owned(self, config, yt_brand):
playlist = yt_brand.get_playlist(config["playlists"]["own"], related=True, suggestions_limit=21)
assert len(playlist["tracks"]) < 100
Expand Down
6 changes: 4 additions & 2 deletions ytmusicapi/mixins/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ def _parse_new_playlist_format(
playlist["duration"] = (
None if not has_duration else second_subtitle_runs[has_views + has_duration]["text"]
)
song_count = second_subtitle_runs[has_views + 0]["text"].split(" ")
song_count = to_int(song_count[0]) if len(song_count) > 1 else 0
song_count_text = second_subtitle_runs[has_views + 0]["text"]
song_count_search = re.search(r"\d+", song_count_text)
# extract the digits from the text, return 0 if no match
song_count = to_int(song_count_search.group()) if song_count_search is not None else 0
else:
song_count = len(section_list["contents"])

Expand Down
6 changes: 4 additions & 2 deletions ytmusicapi/parsers/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ def parse_playlist_header(response: Dict) -> Dict[str, Any]:
playlist["duration"] = (
None if not has_duration else second_subtitle_runs[has_views + has_duration]["text"]
)
song_count = second_subtitle_runs[has_views + 0]["text"].split(" ")
song_count = to_int(song_count[0]) if len(song_count) > 1 else 0
song_count_text = second_subtitle_runs[has_views + 0]["text"]
song_count_search = re.search(r"\d+", song_count_text)
# extract the digits from the text, return 0 if no match
song_count = to_int(song_count_search.group()) if song_count_search is not None else 0
playlist["trackCount"] = song_count

return playlist
Expand Down

0 comments on commit 08d9f30

Please sign in to comment.