Extract song count from text (#609)

* Extract song count from text

* Add fix in parsers

* add test

---------

Co-authored-by: sigma67 <sigma67.github@gmail.com>
This commit is contained in:
JohnHKoh 2024-07-13 14:28:00 -04:00 • committed by GitHub
commit 08d9f3049c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 4 deletions

View file

@ -5,6 +5,9 @@ from unittest import mock
import pytest
from ytmusicapi import YTMusic
from ytmusicapi.constants import SUPPORTED_LANGUAGES
class TestPlaylists:
@pytest.mark.parametrize(
@ -57,6 +60,12 @@ class TestPlaylists:
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

View file

@ -211,8 +211,10 @@ class PlaylistsMixin(MixinProtocol):
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"])

View file

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