Derive more page context from spotify API

This commit is contained in:
Jake Howard 2022-08-18 14:14:46 +01:00
parent e094974f20
commit fcf327e429
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 42 additions and 9 deletions

View file

@ -13,7 +13,7 @@ def is_valid_playlist(playlist_id: str) -> bool:
def get_playlist(playlist_id: str) -> dict: def get_playlist(playlist_id: str) -> dict:
playlist_response = requests.get( playlist_response = requests.get(
f"https://{settings.SPOTIFY_PROXY_HOST}/v1/playlists/{playlist_id}", 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_response.raise_for_status()
playlist_data = playlist_response.json() playlist_data = playlist_response.json()
@ -25,7 +25,7 @@ def get_playlist(playlist_id: str) -> dict:
params={ params={
"offset": str(offset), "offset": str(offset),
"limit": str(API_LIMIT), "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() tracks_response.raise_for_status()

View file

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

View file

@ -1,3 +1,4 @@
from datetime import timedelta
from functools import cached_property from functools import cached_property
from django.core.cache import cache from django.core.cache import cache
@ -23,23 +24,38 @@ class SpotifyPlaylistPage(BaseContentPage):
max_length=32, unique=True, validators=[validate_playlist_id] max_length=32, unique=True, validators=[validate_playlist_id]
) )
content_panels = BaseContentPage.content_panels + [ content_panels = [
FieldPanel("spotify_playlist_id") panel
] for panel in BaseContentPage.content_panels
if panel.field_name != "subtitle"
] + [FieldPanel("spotify_playlist_id")]
@property @property
def table_of_contents(self) -> list: def table_of_contents(self) -> list:
return [] return []
@property @cached_property
def reading_time(self) -> int: 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 @cached_property
def playlist_cache_key(self) -> str: def playlist_cache_key(self) -> str:
return f"spotify_playlist_{self.spotify_playlist_id}" 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) playlist_data = cache.get(self.playlist_cache_key)
if playlist_data is None: if playlist_data is None:
@ -51,5 +67,5 @@ class SpotifyPlaylistPage(BaseContentPage):
def get_context(self, request: HttpRequest) -> dict: def get_context(self, request: HttpRequest) -> dict:
context = super().get_context(request) context = super().get_context(request)
context["playlist"] = self.get_playlist_data() context["playlist"] = self.playlist_data
return context return context