Files
ai-playlists/main.py
T

1020 lines
38 KiB
Python

import os
import random
import time
import json
import sqlite3
import hashlib
import requests
from google import genai
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
# --- CONFIGURATION ---
GONIC_URL = os.getenv("GONIC_URL", "http://gonic:8080/rest")
GONIC_USER = os.getenv("GONIC_USER")
GONIC_PASS = os.getenv("GONIC_PASS")
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-3.8-flash")
DB_DIR = os.getenv("DB_DIR")
if DB_DIR:
DB_PATH = os.path.abspath(os.path.join(DB_DIR, "library.db"))
HASH_PATH = os.path.abspath(os.path.join(DB_DIR, "library.hash"))
else:
_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
DB_PATH = os.path.join(_SCRIPT_DIR, "library.db")
HASH_PATH = os.path.join(_SCRIPT_DIR, "library.hash")
# gemini-3.8-flash free tier = 20 RPD. Keep a small buffer for retries.
GEMINI_DAILY_BUDGET = int(os.getenv("GEMINI_DAILY_BUDGET", "18"))
# Max tracks per Gemini review batch. 150 tracks at ~60 chars each is ~9k
# tokens, well within flash input limits and keeps most playlists in one call.
REVIEW_BATCH_SIZE = 150
# Words filtered out of keyword extraction so that prompt meta-language
# (e.g. "STRICT EXCLUSIONS: Exclude classical") doesn't pollute DB searches.
KEYWORD_STOP_WORDS = {
"about", "also", "with", "that", "this", "from", "into", "your",
"they", "them", "their", "been", "have", "were", "will", "would",
"could", "should", "being", "does", "done", "each", "every",
"keep", "those", "focus", "track", "tracks", "music", "songs",
"song", "genre", "genres", "style", "styles", "vibe", "vibes",
"sound", "sounds", "feel", "mood", "energy", "pieces", "piece",
"exclude", "exclusions", "exclusion", "strict", "absolutely",
"entirely", "strictly", "includes", "including", "features",
"featuring", "designed", "perfect", "great", "built", "over",
"under", "like", "such", "standard", "generic", "merely",
"without", "authentic", "genuine", "truly", "pure", "real",
"when", "something", "needs", "there",
}
AI_PLAYLIST_DEFINITIONS = [
{
"name": "AI: Christmas Bangers",
"prompt": (
"Upbeat Christmas classics and energetic winter anthems to get things moving. "
"STRICT EXCLUSIONS: No traditional classical orchestral pieces or solo choral "
"carols (keep those for the classical playlist)."
)
},
{
"name": "AI: Reflective Misery",
"prompt": (
"Melancholic, atmospheric tracks with deep emotional weight, great for "
"late-night listening. STRICT EXCLUSIONS: Strictly skip classical tracks, "
"acoustic chamber music, and long orchestral suites - focus entirely on "
"modern alternative, dark indie, sad electronic/pop, and similar genres."
)
},
{
"name": "AI: Classical Christmas",
"prompt": (
"Classical Christmas orchestral arrangements, traditional choral carols, "
"solemn holiday brass, and festive symphonic masterpieces."
)
},
{
"name": "AI: Pure Dopamine",
"prompt": (
"Unapologetically joyful, high-energy, uplifting tracks with infectious "
"grooves, bright melodies, and an undeniable feel-good factor to instantly "
"boost the mood. STRICT EXCLUSIONS: Exclude classical compositions, slow "
"movements, and traditional orchestral pieces."
)
},
{
"name": "AI: Midnight Transit",
"prompt": (
"Hypnotic, rhythm-driven electronic, dark synth, driving indie rock, and "
"propulsive bass-heavy tracks built for moving through the dark. Steady "
"forward momentum, cinematic atmospheres, and immersive nocturnal energy. "
"STRICT EXCLUSIONS: Exclude traditional classical/orchestral pieces, slow "
"ambient tracks with no percussion, high-tempo EDM club bangers, and "
"generic acoustic pop."
)
},
{
"name": "AI: Halloween & Horror",
"prompt": (
"Spooky, eerie, and Halloween-party-ready tracks with high nocturnal energy. "
"Includes classic Halloween bops (e.g. Thriller, Time Warp), dark pop "
"(e.g. Disturbia), creepy alt-rock with supernatural themes (e.g. Zombie), "
"anything with a suitably themed title (e.g. Vampire, Dracula, Ghost), "
"and punchy dramatic classical pieces or dark film scores. "
"STRICT EXCLUSIONS: Exclude any track with duration_seconds over 480 "
"(8 minutes) such as multi-movement symphonies, extended concertos, or long "
"suites. Also exclude standard emo/pop-punk songs (no blink-182), and generic "
"alt-rock that merely has a dark title without an element of the title in the song or a "
"supernatural vibe."
)
},
{
"name": "AI: Lazy Sunday Morning",
"prompt": (
"Warm, acoustic, gentle indie folk and stripped-back singer-songwriter tracks "
"with an organic, cozy, and laid-back feel. Perfect for a slow weekend morning. "
"STRICT EXCLUSIONS: Exclude heavy percussion, electronic club beats, aggressive "
"rock, and complex orchestral classical music."
)
},
{
"name": "AI: Deep Focus Lab",
"prompt": (
"Hypnotic, steady-tempo instrumental electronic, ambient techno, IDM, and "
"minimal synth beats designed for deep concentration."
"STRICT EXCLUSIONS: Absolutely zero vocals or spoken word, no "
"acoustic singer-songwriter tracks, and no traditional classical or orchestral arrangements."
)
},
{
"name": "AI: Invisible Soundscape",
"prompt": (
"Truly background music - when something needs to be playing but it shouldn't "
"be noticed. Tracks that are pure ambiance with no loud dynamic peaks and no "
"strong melody, just texture to fill silence. STRICT EXCLUSIONS: No pop, no "
"rock, no hip-hop, no tracks with prominent vocals or lyrics, no strong "
"rhythmic beats, and no catchy hooks or memorable melodies."
)
},
{
"name": "AI: Classical Serenity",
"prompt": (
"Calming, restorative classical and neoclassical music with enough presence, "
"resonance, and steady volume to be enjoyed on the go over headphones. Think "
"Ludovico Einaudi, Max Richter, rich melodic solo piano, lush string adagios, "
"and warm, flowing chamber or minimalist orchestral pieces that soothe emotions "
"without disappearing into background noise. STRICT EXCLUSIONS: Exclude "
"whisper-quiet, ultra-delicate, or sparse pianissimo tracks with extreme "
"dynamic drops that are too faint for headphones on the move. Also exclude "
"bombastic orchestral marches, loud brass fanfares, dramatic or intense "
"symphonic allegros, jarring dissonant modern classical, Christmas carols, "
"and non-classical genres (no pop, rock, or electronic)."
)
},
{
"name": "AI: Deep Cuts",
"prompt": (
"Visceral, raw-emotion tracks that hit straight to the core—soaring, "
"vulnerable, or cathartic vocal performances, heart-on-sleeve lyrics, and "
"intense emotional build-ups. Think Faouzia (e.g. UNETHICAL), Twenty One Pilots, "
"dramatic alt-pop, confessional indie, and emotionally charged alt-rock. "
"STRICT EXCLUSIONS: Exclude classical, orchestral, and purely instrumental "
"tracks, as well as lightweight bubblegum pop or detached, emotionless club "
"tracks."
)
},
{
"name": "AI: Classical Cuts",
"prompt": (
"Deeply moving, poignant, and emotionally devastating classical masterpieces "
"with sweeping pathos and weight. Think Barber's Adagio for Strings, Bach's "
"Air on a G String, Elgar's Nimrod, Albinoni's Adagio in G minor, and soaring "
"elegiac orchestral or string works that stir the soul. STRICT EXCLUSIONS: "
"Exclude lighthearted or upbeat dances, Christmas carols, bombastic military "
"marches, and all non-classical genres (no pop, rock, or electronic)."
)
},
{
"name": "AI: Summer in the Garden",
"prompt": (
"Sunny, crowd-pleasing, multi-generational tracks for a garden party or "
"weekend barbecue. Punchy, upbeat singalongs, timeless pop-rock, breezy indie "
"hits, and feel-good classics across the 70s to today that everyone from kids "
"to grandparents can get behind. STRICT EXCLUSIONS: Exclude heavy metal, "
"abrasive or aggressive rock, gloomy ballads, experimental electronic, and "
"classical orchestral works."
)
},
{
"name": "AI: Kinetic Voltage",
"prompt": (
"High-octane, adrenaline-fueled tracks with relentless forward momentum—hard-hitting "
"rock, alt-metal, energetic punk, and aggressive electro or synth-rock built for "
"workouts, running, or powering through high-effort tasks. STRICT EXCLUSIONS: "
"Exclude slow ballads, gentle acoustic folk, calm ambient, and traditional "
"classical music."
)
},
{
"name": "AI: Golden Hour Drive",
"prompt": (
"Warm, anthemic, sun-drenched tracks built for daytime driving with the windows "
"down. Nostalgic classic rock, uplifting indie anthems, breezy pop-rock, and "
"steady-cruising feel-good songs with a sense of open space. STRICT EXCLUSIONS: "
"Exclude dark nocturnal synthwave, heavy metal, melancholic/gloomy tracks, and "
"classical orchestral suites."
)
},
{
"name": "AI: Widescreen Horizon",
"prompt": (
"Sweeping, larger-than-life music with dramatic, cinematic scale—epic film "
"scores, soaring orchestral-hybrid pieces, and grandiose rock or electronic "
"builds that feel like a movie climax. STRICT EXCLUSIONS: Exclude lo-fi ambient, "
"stripped-back acoustic folk, lighthearted bubblegum pop, Christmas carols, and "
"Halloween novelty tracks."
)
}
]
# Gemini call counter for the current run
gemini_calls_made = 0
# Optional startup jitter to avoid precise scheduling thundering herds
MAX_JITTER = int(os.getenv("MAX_JITTER", "300"))
if MAX_JITTER > 0:
jitter = random.randint(0, MAX_JITTER)
print(f"[init] Sleeping for jitter={jitter}s", flush=True)
time.sleep(jitter)
gemini_client = genai.Client(api_key=GEMINI_API_KEY)
class QuotaExhaustedError(Exception):
"""Raised when the Gemini API daily free-tier quota is exhausted."""
pass
# ---------------------------------------------------------------------------
# Gonic / Subsonic API helpers
# ---------------------------------------------------------------------------
def _get_auth_params():
return {
"u": GONIC_USER,
"p": GONIC_PASS,
"v": "1.16.1",
"c": "AI-Playlist-Agent",
"f": "json"
}
def _ensure_list(val):
"""Ensure Subsonic XML-to-JSON values are always lists (single items often unwrap to dict)."""
if val is None:
return []
if isinstance(val, list):
return val
if isinstance(val, dict):
return [val]
return []
def gonic_request(endpoint, extra_params=None, param_list=None, context=""):
"""Make a request to the Gonic/Subsonic API with full error logging.
Use *extra_params* (dict) for simple key=value params merged with auth.
Use *param_list* (list of tuples) when duplicate keys are needed
(e.g. multiple songIdToAdd or songIndexToRemove values).
Returns the inner ``subsonic-response`` dict on success, or ``None``
on any HTTP, JSON-parse, or API-level error.
"""
auth = _get_auth_params()
if param_list is not None:
params = [(k, v) for k, v in auth.items()] + param_list
else:
params = {**auth, **(extra_params or {})}
url = f"{GONIC_URL}/{endpoint}"
try:
resp = requests.get(url, params=params, timeout=30)
except requests.RequestException as e:
print(f"[gonic] Request failed endpoint={endpoint} context='{context}' error={e}", flush=True)
return None
if resp.status_code != 200:
print(f"[gonic] HTTP {resp.status_code} endpoint={endpoint} context='{context}' body={resp.text[:500]}", flush=True)
return None
try:
data = resp.json()
except ValueError:
print(f"[gonic] Invalid JSON endpoint={endpoint} context='{context}' body={resp.text[:500]}", flush=True)
return None
sub_resp = data.get("subsonic-response", {})
if sub_resp.get("status") == "failed":
error = sub_resp.get("error", {})
print(
f"[gonic] API error endpoint={endpoint} context='{context}' "
f"code={error.get('code')} message={error.get('message')}",
flush=True
)
return None
return sub_resp
# ---------------------------------------------------------------------------
# Database
# ---------------------------------------------------------------------------
def get_db():
"""Return an active SQLite connection, guaranteeing all tables and schema migrations exist."""
db_dir = os.path.dirname(DB_PATH)
if db_dir:
os.makedirs(db_dir, exist_ok=True)
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS tracks (
id TEXT PRIMARY KEY,
title TEXT,
artist TEXT,
album TEXT,
genre TEXT,
year INTEGER,
duration INTEGER DEFAULT 0
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS playlist_state (
name TEXT PRIMARY KEY,
last_curated INTEGER DEFAULT 0,
last_reviewed INTEGER DEFAULT 0
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS rotation_state (
key TEXT PRIMARY KEY,
value INTEGER DEFAULT 0
)
""")
# Check and apply migrations for older database schemas
cursor.execute("PRAGMA table_info(playlist_state)")
cols_playlist = {row[1] for row in cursor.fetchall()}
if "last_reviewed" not in cols_playlist:
cursor.execute("ALTER TABLE playlist_state ADD COLUMN last_reviewed INTEGER DEFAULT 0")
cursor.execute("PRAGMA table_info(tracks)")
cols_tracks = {row[1] for row in cursor.fetchall()}
if "duration" not in cols_tracks:
cursor.execute("ALTER TABLE tracks ADD COLUMN duration INTEGER DEFAULT 0")
conn.commit()
return conn
def init_db():
"""Initialize database and ensure tables and columns exist."""
conn = get_db()
conn.close()
def _tracks_table_empty():
conn = get_db()
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM tracks")
count = cursor.fetchone()[0]
conn.close()
return count == 0
# ---------------------------------------------------------------------------
# Library sync
# ---------------------------------------------------------------------------
def sync_library_to_sqlite():
print("[sync] Fetching full track catalog from Gonic", flush=True)
res = gonic_request("getArtists.view", context="sync artists")
if not res:
print("[sync] Failed to fetch artist index, aborting sync", flush=True)
return
indexes = _ensure_list(res.get("artists", {}).get("index", []))
all_tracks = []
for index_group in indexes:
for artist in _ensure_list(index_group.get("artist", [])):
artist_id = artist.get("id")
if not artist_id:
continue
artist_res = gonic_request(
"getArtist.view", {"id": artist_id},
context=f"artist id={artist_id}"
)
if not artist_res:
continue
for album in _ensure_list(artist_res.get("artist", {}).get("album", [])):
album_id = album.get("id")
if not album_id:
continue
album_res = gonic_request(
"getAlbum.view", {"id": album_id},
context=f"album id={album_id}"
)
if not album_res:
continue
for song in _ensure_list(album_res.get("album", {}).get("song", [])):
song_id = song.get("id")
if song_id is not None:
all_tracks.append((
str(song_id),
song.get("title", "Unknown"),
song.get("artist", "Unknown"),
song.get("album", "Unknown"),
song.get("genre", "Unknown"),
int(song.get("year", 0) or 0),
int(song.get("duration", 0) or 0),
))
if not all_tracks:
print("[sync] Warning: No tracks found during sync; keeping existing database intact", flush=True)
return
conn = get_db()
cursor = conn.cursor()
cursor.execute("DELETE FROM tracks")
cursor.executemany("INSERT OR REPLACE INTO tracks VALUES (?, ?, ?, ?, ?, ?, ?)", all_tracks)
conn.commit()
conn.close()
print(f"[sync] Indexed {len(all_tracks)} tracks", flush=True)
def check_library_changed():
res = gonic_request("getIndexes.view", context="library hash check")
if not res:
print("[sync] Could not check library hash, assuming changed", flush=True)
return True
indexes_data = str(res.get("indexes", {}))
current_hash = hashlib.md5(indexes_data.encode()).hexdigest()
if os.path.exists(HASH_PATH):
try:
with open(HASH_PATH, "r") as f:
stored_hash = f.read().strip()
if stored_hash == current_hash:
return False
except Exception as e:
print(f"[sync] Error reading hash file: {e}", flush=True)
try:
with open(HASH_PATH, "w") as f:
f.write(current_hash)
except Exception as e:
print(f"[sync] Error writing hash file: {e}", flush=True)
return True
# ---------------------------------------------------------------------------
# Candidate selection
# ---------------------------------------------------------------------------
def _extract_keywords(prompt):
"""Pull meaningful search terms from a prompt, filtering stop-words."""
words = set()
for token in prompt.lower().split():
cleaned = token.strip(".,;:!?\"'()-/")
if len(cleaned) > 3 and cleaned not in KEYWORD_STOP_WORDS:
words.add(cleaned)
return list(words)
def query_candidate_tracks(prompt):
conn = get_db()
cursor = conn.cursor()
keywords = _extract_keywords(prompt)
rows = []
# Keyword-matched candidates (capped at 200)
if keywords:
clauses = " OR ".join(
["genre LIKE ? OR title LIKE ? OR artist LIKE ? OR album LIKE ?"] * len(keywords)
)
params = []
for w in keywords:
params.extend([f"%{w}%"] * 4)
cursor.execute(
f"SELECT id, title, artist, album, genre, year, duration FROM tracks WHERE {clauses} LIMIT 200",
params
)
rows = cursor.fetchall()
# Pad with random tracks for serendipity (target pool = 350)
target = 350
seen = {r[0] for r in rows}
if len(rows) < target:
needed = target - len(rows)
cursor.execute(
"SELECT id, title, artist, album, genre, year, duration FROM tracks ORDER BY RANDOM() LIMIT ?",
(needed,)
)
for r in cursor.fetchall():
if r[0] not in seen:
rows.append(r)
seen.add(r[0])
conn.close()
return [
{
"id": str(r[0]),
"title": r[1] or "Unknown",
"artist": r[2] or "Unknown",
"album": r[3] or "Unknown",
"genre": r[4] or "Unknown",
"year": r[5] or 0,
"duration_seconds": r[6] or 0
}
for r in rows
]
# ---------------------------------------------------------------------------
# Gemini helpers
# ---------------------------------------------------------------------------
def _classify_error(e):
"""Classify a Gemini exception as 'quota', 'rate_limit', 'unavailable', or None."""
s = str(e).lower()
if "quota" in s or "daily limit" in s:
return "quota"
if "429" in str(e) or "resource_exhausted" in s or "rate limit" in s:
return "rate_limit"
if "503" in str(e) or "unavailable" in s:
return "unavailable"
return None
def _normalize_id_list(raw_val):
"""Safely extract list of clean string IDs from various LLM response structures."""
if not isinstance(raw_val, list):
return []
clean_ids = []
for item in raw_val:
if isinstance(item, (str, int)):
clean_ids.append(str(item))
elif isinstance(item, dict) and "id" in item:
clean_ids.append(str(item["id"]))
return clean_ids
def _call_gemini(system_instruction, user_content):
"""Central Gemini call with retry + quota detection.
Increments ``gemini_calls_made`` on success.
Raises ``QuotaExhaustedError`` when the daily free-tier limit is hit.
"""
global gemini_calls_made
max_retries = 6
delay = 10
consecutive_rate_limits = 0
for attempt in range(max_retries):
try:
response = gemini_client.models.generate_content(
model=GEMINI_MODEL,
contents=user_content,
config=dict(
system_instruction=system_instruction,
temperature=0.3,
response_mime_type="application/json"
)
)
gemini_calls_made += 1
text = getattr(response, "text", "") or ""
text = text.strip()
# Handle possible markdown formatting fences
if text.startswith("```"):
lines = text.splitlines()
if lines and lines[0].startswith("```"):
lines = lines[1:]
if lines and lines[-1].startswith("```"):
lines = lines[:-1]
text = "\n".join(lines).strip()
try:
result = json.loads(text)
except (json.JSONDecodeError, TypeError) as e:
print(f"[gemini] Bad JSON in response: {e} raw={text[:300]}", flush=True)
return []
return _normalize_id_list(result)
except Exception as e:
kind = _classify_error(e)
if kind == "quota":
raise QuotaExhaustedError(str(e))
if kind == "rate_limit":
consecutive_rate_limits += 1
if consecutive_rate_limits >= 4:
raise QuotaExhaustedError(
f"Persistent rate limiting after {consecutive_rate_limits} retries: {e}"
)
print(f"[gemini] Rate limited, retrying in {delay}s (attempt {attempt + 1}/{max_retries})", flush=True)
time.sleep(delay)
delay = min(delay * 2, 120)
continue
if kind == "unavailable":
if attempt < max_retries - 1:
print(f"[gemini] Service unavailable, retrying in {delay}s (attempt {attempt + 1}/{max_retries})", flush=True)
time.sleep(delay)
delay = min(delay * 2, 120)
continue
print(f"[gemini] Unrecoverable error: {e}", flush=True)
raise
print("[gemini] All retries exhausted, returning empty result", flush=True)
return []
def ask_gemini_for_tracks(prompt, candidates):
system_instruction = (
"You are an expert music archivist, tastemaker, and DJ. "
"Curate tracks from the candidate pool that match the given thematic prompt. "
"CRITICAL INSTRUCTIONS:\n"
"1. Use your deep knowledge of music, artist styles, moods, and instrumentation. "
"Do not rely solely on database tags or genres - metadata can be messy or absent.\n"
"2. Prioritize stylistic and emotional fit. Quality matters far more than quantity: "
"do not pad the selection with weak matches.\n"
"3. Select anywhere from zero to every track that genuinely fits. No quotas or limits.\n"
"4. Each track includes a duration_seconds field. Use it to enforce any "
"duration-based rules in the prompt.\n"
"5. Return ONLY a JSON array of string IDs of chosen tracks.\n"
"6. No markdown, no code blocks, no conversational text."
)
user_content = f"Prompt/Theme: {prompt}\n\nCandidate Tracks:\n{json.dumps(candidates, indent=2)}"
return _call_gemini(system_instruction, user_content)
def ask_gemini_to_review_tracks(prompt, tracks_in_playlist):
system_instruction = (
"You are an expert music archivist performing quality control on an existing playlist. "
"Review the tracks and identify any that do not genuinely fit the thematic brief. "
"CRITICAL INSTRUCTIONS:\n"
"1. Maintain a cohesive, high-quality playlist. Flag tracks that feel out of place, "
"break the intended mood, or fall outside the core genres/styles requested—even if they "
"are good songs in their own right.\n"
"2. Use your deep knowledge of music - artist styles, moods, instrumentation, and "
"arrangement. Do not rely solely on genre tags.\n"
"3. STRICT EXCLUSIONS and duration limits are hard rules: ALWAYS flag any track that "
"violates an explicit exclusion or exceeds a specified duration_seconds limit (e.g. "
"duration_seconds > 480 when capped at 8 minutes).\n"
"4. Balance firmness with consistency: do not nitpick or remove tracks that genuinely "
"belong, as we want a stable playlist rather than daily churn, but do not hesitate to "
"prune clear misfits or borderline tracks that dilute the vibe.\n"
"5. Return ONLY a JSON array of string IDs of tracks to REMOVE. "
"Return [] if all tracks are acceptable.\n"
"6. No markdown, no code blocks, no conversational text."
)
user_content = (
f"Playlist Brief: {prompt}\n\n"
f"Tracks currently in this playlist:\n{json.dumps(tracks_in_playlist, indent=2)}"
)
return _call_gemini(system_instruction, user_content)
# ---------------------------------------------------------------------------
# Rotation / scheduling
# ---------------------------------------------------------------------------
def _get_rotation_index(key):
conn = get_db()
cursor = conn.cursor()
cursor.execute("SELECT value FROM rotation_state WHERE key = ?", (key,))
row = cursor.fetchone()
conn.close()
return row[0] if row else 0
def _set_rotation_index(key, value):
conn = get_db()
cursor = conn.cursor()
cursor.execute("INSERT OR REPLACE INTO rotation_state (key, value) VALUES (?, ?)", (key, value))
conn.commit()
conn.close()
def _pick_next_playlist(action, now):
"""Pick the next eligible playlist for the given action via round-robin.
Returns the playlist definition dict, or None if all playlists have
been processed within the 20-hour cooldown window.
"""
num = len(AI_PLAYLIST_DEFINITIONS)
if num == 0:
return None
rotation_key = f"rotation_{action}"
start = _get_rotation_index(rotation_key)
conn = get_db()
cursor = conn.cursor()
for offset in range(num):
idx = (start + offset) % num
defn = AI_PLAYLIST_DEFINITIONS[idx]
cursor.execute(
"SELECT last_curated, last_reviewed FROM playlist_state WHERE name = ?",
(defn["name"],)
)
row = cursor.fetchone()
ts = (row[0] if action == "curate" else row[1]) if row else 0
# 20-hour minimum between same-action passes on one playlist
if (now - ts) > 72000:
conn.close()
_set_rotation_index(rotation_key, (idx + 1) % num)
return defn
conn.close()
return None
# ---------------------------------------------------------------------------
# Playlist operations
# ---------------------------------------------------------------------------
def _get_all_user_playlists():
"""Fetch all playlists owned by user as a dict {name: id}."""
res = gonic_request("getPlaylists.view", context="list playlists")
if not res:
return {}
playlists = _ensure_list(res.get("playlists", {}).get("playlist", []))
return {pl.get("name"): pl.get("id") for pl in playlists if pl.get("name") and pl.get("id")}
def _ensure_playlist_exists(name, cache=None):
"""Return the playlist ID, creating the playlist first if needed."""
if cache is not None and name in cache:
return cache[name]
playlists = _get_all_user_playlists()
if name in playlists:
if cache is not None:
cache[name] = playlists[name]
return playlists[name]
print(f"[playlist] Creating '{name}'", flush=True)
create_res = gonic_request(
"createPlaylist.view",
{"name": name, "public": "true"},
context=f"create '{name}'"
)
if not create_res:
return None
playlists = _get_all_user_playlists()
pid = playlists.get(name)
if pid and cache is not None:
cache[name] = pid
return pid
def _get_playlist_entries(playlist_id, name):
res = gonic_request("getPlaylist.view", {"id": playlist_id}, context=f"entries '{name}'")
if not res:
return []
return _ensure_list(res.get("playlist", {}).get("entry", []))
def _update_timestamp(name, field, now):
conn = get_db()
cursor = conn.cursor()
if field == "last_curated":
cursor.execute(
"INSERT INTO playlist_state (name, last_curated, last_reviewed) VALUES (?, ?, 0) "
"ON CONFLICT(name) DO UPDATE SET last_curated = ?",
(name, now, now)
)
else:
cursor.execute(
"INSERT INTO playlist_state (name, last_curated, last_reviewed) VALUES (?, 0, ?) "
"ON CONFLICT(name) DO UPDATE SET last_reviewed = ?",
(name, now, now)
)
conn.commit()
conn.close()
def curate_playlist(defn, playlist_cache=None):
"""Add matching tracks to a playlist. Consumes 1 Gemini call."""
name = defn["name"]
prompt = defn["prompt"]
now = int(time.time())
playlist_id = _ensure_playlist_exists(name, cache=playlist_cache)
if not playlist_id:
print(f"[curate] Could not find/create playlist='{name}', skipping", flush=True)
return
existing_entries = _get_playlist_entries(playlist_id, name)
print(f"[curate] Processing playlist='{name}' existing_tracks={len(existing_entries)}", flush=True)
candidates = query_candidate_tracks(prompt)
if not candidates:
print(f"[curate] No candidates in local index for playlist='{name}'", flush=True)
_update_timestamp(name, "last_curated", now)
return
selected_ids = ask_gemini_for_tracks(prompt, candidates)
existing_ids = {str(e.get("id")) for e in existing_entries if e.get("id") is not None}
new_ids = [sid for sid in selected_ids if sid not in existing_ids]
print(f"[curate] Gemini selected={len(selected_ids)} new={len(new_ids)} playlist='{name}'", flush=True)
if new_ids:
param_list = [
("playlistId", playlist_id),
("name", name),
("comment", prompt),
("public", "true"),
]
for sid in new_ids:
param_list.append(("songIdToAdd", sid))
result = gonic_request(
"updatePlaylist.view", param_list=param_list,
context=f"add {len(new_ids)} tracks to '{name}'"
)
if result:
print(f"[curate] Added {len(new_ids)} tracks to playlist='{name}'", flush=True)
else:
print(f"[curate] Failed adding tracks to playlist='{name}'", flush=True)
_update_timestamp(name, "last_curated", now)
def review_playlist(defn, playlist_cache=None):
"""Review and prune ill-fitting tracks. Consumes 1+ Gemini calls."""
name = defn["name"]
prompt = defn["prompt"]
now = int(time.time())
playlist_id = _ensure_playlist_exists(name, cache=playlist_cache)
if not playlist_id:
print(f"[review] Could not find/create playlist='{name}', skipping", flush=True)
return
existing_entries = _get_playlist_entries(playlist_id, name)
if not existing_entries:
print(f"[review] Playlist='{name}' is empty, nothing to review", flush=True)
_update_timestamp(name, "last_reviewed", now)
return
print(f"[review] Processing playlist='{name}' tracks={len(existing_entries)}", flush=True)
track_info = [
{
"id": str(e.get("id")),
"title": e.get("title", "Unknown"),
"artist": e.get("artist", "Unknown"),
"album": e.get("album", "Unknown"),
"genre": e.get("genre", "Unknown"),
"year": e.get("year", 0),
"duration_seconds": e.get("duration", 0),
}
for e in existing_entries
if e.get("id") is not None
]
ids_to_remove = []
total_batches = (len(track_info) + REVIEW_BATCH_SIZE - 1) // REVIEW_BATCH_SIZE
for batch_start in range(0, len(track_info), REVIEW_BATCH_SIZE):
if gemini_calls_made >= GEMINI_DAILY_BUDGET:
print(f"[review] Daily budget cap reached during review of '{name}', completing partial pass", flush=True)
break
batch = track_info[batch_start:batch_start + REVIEW_BATCH_SIZE]
batch_num = (batch_start // REVIEW_BATCH_SIZE) + 1
if total_batches > 1:
print(f"[review] Batch {batch_num}/{total_batches} ({len(batch)} tracks) playlist='{name}'", flush=True)
flagged = ask_gemini_to_review_tracks(prompt, batch)
if flagged:
ids_to_remove.extend(flagged)
if ids_to_remove:
remove_set = set(ids_to_remove)
indices = []
for i, entry in enumerate(existing_entries):
if str(entry.get("id")) in remove_set:
indices.append(i)
print(
f"[review] Removing '{entry.get('title', '?')}' "
f"by {entry.get('artist', '?')} from playlist='{name}'",
flush=True
)
if indices:
# Descending order prevents index-shift corruption on servers
# that process removals sequentially.
indices.sort(reverse=True)
param_list = [
("playlistId", playlist_id),
("name", name),
("comment", prompt),
("public", "true"),
]
for idx in indices:
param_list.append(("songIndexToRemove", idx))
result = gonic_request(
"updatePlaylist.view", param_list=param_list,
context=f"remove {len(indices)} tracks from '{name}'"
)
if result:
print(f"[review] Removed {len(indices)} tracks from playlist='{name}'", flush=True)
else:
print(f"[review] Failed removing tracks from playlist='{name}'", flush=True)
else:
print(f"[review] All tracks pass review for playlist='{name}'", flush=True)
_update_timestamp(name, "last_reviewed", now)
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
global gemini_calls_made
init_db()
print("[init] AI Playlist Sync started", flush=True)
try:
# Sync library if changed or local index is empty
lib_changed = check_library_changed()
if lib_changed or _tracks_table_empty():
print("[sync] Library change detected or index empty, syncing", flush=True)
sync_library_to_sqlite()
else:
print("[sync] Library unchanged, using cached index", flush=True)
# Pre-cache and ensure all playlists exist
playlist_cache = _get_all_user_playlists()
for defn in AI_PLAYLIST_DEFINITIONS:
pid = _ensure_playlist_exists(defn["name"], cache=playlist_cache)
if not pid:
print(f"[init] Could not create playlist='{defn['name']}'", flush=True)
now = int(time.time())
gemini_calls_made = 0
curate_done = False
review_done = False
# Greedy loop: alternate curate / review rounds until budget is spent
# or no more playlists are eligible for either action.
while gemini_calls_made < GEMINI_DAILY_BUDGET:
did_work = False
if not curate_done and gemini_calls_made < GEMINI_DAILY_BUDGET:
defn = _pick_next_playlist("curate", now)
if defn:
curate_playlist(defn, playlist_cache=playlist_cache)
did_work = True
else:
curate_done = True
print("[curate] No more playlists eligible for curation", flush=True)
if not review_done and gemini_calls_made < GEMINI_DAILY_BUDGET:
defn = _pick_next_playlist("review", now)
if defn:
review_playlist(defn, playlist_cache=playlist_cache)
did_work = True
else:
review_done = True
print("[review] No more playlists eligible for review", flush=True)
if not did_work:
break
print(f"[done] Completed, gemini_calls={gemini_calls_made}", flush=True)
except QuotaExhaustedError as e:
print(f"[quota] Daily limit reached, stopping gracefully, gemini_calls={gemini_calls_made} detail={e}", flush=True)
except Exception as e:
print(f"[error] Unhandled exception: {e}", flush=True)
raise
if __name__ == "__main__":
main()