Adds the per-kind generator modules and registers them with the
PlaylistKindRegistry so the manager's `refresh_playlist` can dispatch
to any of them.
Generators (each in its own module under
`core/personalized/generators/`):
Singletons (variant=''):
- hidden_gems -> wraps service.get_hidden_gems
- discovery_shuffle -> wraps service.get_discovery_shuffle
- popular_picks -> wraps service.get_popular_picks
Variant-bearing kinds:
- time_machine -> variant = decade label ('1980s', '1990s', ...).
Variant resolver returns 7 standard decades.
Generator parses '1980s' -> 1980 + delegates
to service.get_decade_playlist.
- genre_playlist -> variant = URL-safe genre key
('electronic_dance', 'hip_hop_rap', ...).
Resolver normalizes parent-genre keys from
service.GENRE_MAPPING; free-form keywords
pass through to service.get_genre_playlist.
- daily_mix -> variant = top-genre rank ('1' / '2' / '3' / '4').
Generator looks up user's Nth-ranked library
genre and returns discovery picks within it.
Library half (was a stub returning []) is
intentionally dropped: tracks table has no
source IDs, so library rows can't sync. Fixed
the stub to return [] cleanly without the
misleading log warning.
- fresh_tape -> Spotify Release Radar. Reads curated track
IDs from discovery_curated_playlists (tries
'release_radar_<source>' first, falls back to
'release_radar') and hydrates against the
discovery pool.
- archives -> Spotify Discover Weekly. Same hydration path
as fresh_tape but uses 'discovery_weekly'.
- seasonal_mix -> variant = season key ('halloween' / 'christmas'
/ 'valentines' / 'summer' / 'spring' / 'autumn').
Reads curated IDs via SeasonalDiscoveryService
then hydrates from seasonal_tracks (which
carries full track_data_json).
Each module:
- Defines `generate(deps, variant, config) -> List[Track]`.
- Defines `SPEC = PlaylistKindSpec(...)` and registers it on import
(idempotent — re-import safe via `if registry.get(...) is None`).
- For variant-bearing kinds, also defines `variant_resolver(deps)`.
Shared helpers in `_common.py`:
- `get_service(deps)` pulls the legacy
`PersonalizedPlaylistsService` instance (deps.service or
deps['service']).
- `coerce_tracks(rows)` runs each dict through `Track.from_dict`,
tolerates None / non-list inputs.
Tests (50 new, total 85 across personalized subsystem):
- Singletons: registration + display name + dispatch + limit
forwarding + empty/None tolerance + missing-deps error +
dict-form deps acceptance (16 tests).
- Variants: variant_resolver listing + label parsing + invalid
variant errors + parent-key normalization + free-form passthrough
(13 tests).
- Curated/hybrid: daily_mix rank-to-genre resolution + rank-out-of-
range empty + invalid-variant error; fresh_tape & archives
hydration order + missing-id skip + source-specific-then-fallback
key dispatch + limit + missing-database-dep error; seasonal_mix
curated-id hydration order + missing-id skip + JSON round-trip +
empty-curated empty + limit + missing-service error (21 tests).
3304+ tests pass. No regression on existing 62 personalized tests.
66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
"""Time Machine generator — by-decade discovery picks.
|
|
|
|
Variant = decade label like ``'1980s'`` / ``'1990s'`` / ``'2000s'``.
|
|
The variant resolver returns the standard decade set; users see one
|
|
playlist per decade (each independently configurable / refreshable).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, List
|
|
|
|
from core.personalized.generators._common import coerce_tracks, get_service
|
|
from core.personalized.specs import PlaylistKindSpec, get_registry
|
|
from core.personalized.types import PlaylistConfig, Track
|
|
|
|
|
|
KIND = 'time_machine'
|
|
|
|
|
|
# Standard decades the UI exposes. Adjust here when adding eras.
|
|
_DEFAULT_DECADES = ('1960s', '1970s', '1980s', '1990s', '2000s', '2010s', '2020s')
|
|
|
|
|
|
def _decade_to_year(variant: str) -> int:
|
|
"""'1980s' -> 1980. Tolerates ' 1980 ', '1980'.
|
|
|
|
Raises ValueError for anything that doesn't look like a decade
|
|
label so the manager surfaces a clear error instead of generating
|
|
garbage."""
|
|
cleaned = (variant or '').strip().rstrip('sS')
|
|
try:
|
|
year = int(cleaned)
|
|
except ValueError as exc:
|
|
raise ValueError(f"Time Machine variant {variant!r} not a decade label") from exc
|
|
if year < 1900 or year > 2100:
|
|
raise ValueError(f"Time Machine variant {variant!r} out of range")
|
|
return year
|
|
|
|
|
|
def generate(deps: Any, variant: str, config: PlaylistConfig) -> List[Track]:
|
|
service = get_service(deps)
|
|
decade_year = _decade_to_year(variant)
|
|
rows = service.get_decade_playlist(decade=decade_year, limit=config.limit)
|
|
return coerce_tracks(rows)
|
|
|
|
|
|
def variant_resolver(deps: Any) -> List[str]:
|
|
"""Return the standard decade set. Future enhancement: filter to
|
|
decades that actually have data in the discovery pool."""
|
|
return list(_DEFAULT_DECADES)
|
|
|
|
|
|
SPEC = PlaylistKindSpec(
|
|
kind=KIND,
|
|
name_template='Time Machine — {variant}',
|
|
description='Tracks from a specific decade. One playlist per decade.',
|
|
default_config=PlaylistConfig(limit=100, max_per_album=3, max_per_artist=5),
|
|
generator=generate,
|
|
variant_resolver=variant_resolver,
|
|
requires_variant=True,
|
|
tags=['time'],
|
|
)
|
|
|
|
|
|
if get_registry().get(KIND) is None:
|
|
get_registry().register(SPEC)
|