diff --git a/website/spotify/client.py b/website/spotify/client.py index 446c972..a9f969b 100644 --- a/website/spotify/client.py +++ b/website/spotify/client.py @@ -13,7 +13,7 @@ def is_valid_playlist(playlist_id: str) -> bool: def get_playlist(playlist_id: str) -> dict: playlist_response = requests.get( f"https://{settings.SPOTIFY_PROXY_HOST}/v1/playlists/{playlist_id}", - params={"fields": "name,external_urls.spotify,tracks.total"}, + params={"fields": "name,external_urls.spotify,tracks.total,description"}, ) playlist_response.raise_for_status() playlist_data = playlist_response.json() @@ -25,7 +25,7 @@ def get_playlist(playlist_id: str) -> dict: params={ "offset": str(offset), "limit": str(API_LIMIT), - "fields": "items(track(name,album.name,album.images,artists.name,external_urls.spotify,preview_url))", + "fields": "items(track(name,album.name,album.images,artists.name,external_urls.spotify,preview_url,duration_ms))", }, ) tracks_response.raise_for_status() diff --git a/website/spotify/migrations/0002_remove_spotifyplaylistpage_subtitle.py b/website/spotify/migrations/0002_remove_spotifyplaylistpage_subtitle.py new file mode 100644 index 0000000..ed13a4a --- /dev/null +++ b/website/spotify/migrations/0002_remove_spotifyplaylistpage_subtitle.py @@ -0,0 +1,17 @@ +# Generated by Django 4.0.6 on 2022-08-18 12:51 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("spotify", "0001_initial"), + ] + + operations = [ + migrations.RemoveField( + model_name="spotifyplaylistpage", + name="subtitle", + ), + ] diff --git a/website/spotify/models.py b/website/spotify/models.py index 22599e3..5cb367c 100644 --- a/website/spotify/models.py +++ b/website/spotify/models.py @@ -1,3 +1,4 @@ +from datetime import timedelta from functools import cached_property from django.core.cache import cache @@ -23,23 +24,38 @@ class SpotifyPlaylistPage(BaseContentPage): max_length=32, unique=True, validators=[validate_playlist_id] ) - content_panels = BaseContentPage.content_panels + [ - FieldPanel("spotify_playlist_id") - ] + content_panels = [ + panel + for panel in BaseContentPage.content_panels + if panel.field_name != "subtitle" + ] + [FieldPanel("spotify_playlist_id")] @property def table_of_contents(self) -> list: return [] - @property + @cached_property def reading_time(self) -> int: - return 0 + return int( + timedelta( + milliseconds=sum( + track["track"]["duration_ms"] + for track in self.playlist_data["tracks"] + ) + ).total_seconds() + / 60 + ) + + @cached_property + def subtitle(self) -> int: + return self.playlist_data["description"] @cached_property def playlist_cache_key(self) -> str: return f"spotify_playlist_{self.spotify_playlist_id}" - def get_playlist_data(self) -> dict: + @cached_property + def playlist_data(self) -> dict: playlist_data = cache.get(self.playlist_cache_key) if playlist_data is None: @@ -51,5 +67,5 @@ class SpotifyPlaylistPage(BaseContentPage): def get_context(self, request: HttpRequest) -> dict: context = super().get_context(request) - context["playlist"] = self.get_playlist_data() + context["playlist"] = self.playlist_data return context