diff --git a/.gitignore b/.gitignore index 36b13f1..bf29cee 100644 --- a/.gitignore +++ b/.gitignore @@ -174,3 +174,7 @@ cython_debug/ # PyPI configuration file .pypirc +# Local SQLite & sync hashes +library.db +library.hash + diff --git a/main.py b/main.py index b910590..bfc5975 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,4 @@ import os -import sys import random import time import json @@ -7,66 +6,164 @@ import sqlite3 import hashlib import requests from google import genai -from dotenv import load_dotenv - -load_dotenv() +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") -CURATION_COOLDOWN = 7 * 86400 # Re-curate existing playlists every 7 days for fresh rotation +GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-3.8-flash") -DB_DIR = os.getenv("DB_DIR", ".") -DB_PATH = os.path.join(DB_DIR, "library.db") -HASH_PATH = os.path.join(DB_DIR, "library.hash") +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", +} -# Declare your AI playlists and prompts here. -# The script will automatically create and own them under the script's user credentials. 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)." + "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, and sad electronic/pop." + "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, and sad electronic/pop." + ) }, { "name": "AI: Classical Christmas", - "prompt": "Classical Christmas orchestral arrangements, traditional choral carols, solemn holiday brass, and festive symphonic masterpieces." + "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." + "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. Features a 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." + "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), and punchy, dramatic classical pieces or dark film scores (standalone tone poems up to 8 minutes like Danse Macabre or Fossils). STRICT EXCLUSIONS: Exclude multi-movement symphonies, extended concertos, or suites over 8 minutes (no 15+ minute works like Totentanz), standard emo/pop-punk songs (no blink-182), and generic alt-rock songs that merely have dark titles without an authentic Halloween or supernatural vibe." + "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), " + "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 authentic Halloween or " + "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." + "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, coding, and technical workflows. STRICT EXCLUSIONS: Absolutely zero vocals or spoken word, no acoustic singer-songwriter tracks, and no classical or orchestral arrangements." + "prompt": ( + "Hypnotic, steady-tempo instrumental electronic, ambient techno, IDM, and " + "minimal synth beats designed for deep concentration, coding, and technical " + "workflows. STRICT EXCLUSIONS: Absolutely zero vocals or spoken word, no " + "acoustic singer-songwriter tracks, and no 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." + ) } ] -jitter = random.randint(0, 300) -print(f"Jitter active: sleeping for {jitter} seconds...", flush=True) -time.sleep(jitter) +# 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) -# Initialize Gemini Client gemini_client = genai.Client(api_key=GEMINI_API_KEY) -def get_auth_params(): + +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, @@ -75,9 +172,75 @@ def get_auth_params(): "f": "json" } -def init_db(): - if DB_DIR and DB_DIR != ".": - os.makedirs(DB_DIR, exist_ok=True) + +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(""" @@ -87,129 +250,255 @@ def init_db(): artist TEXT, album TEXT, genre TEXT, - year INTEGER + year INTEGER, + duration INTEGER DEFAULT 0 ) """) cursor.execute(""" CREATE TABLE IF NOT EXISTS playlist_state ( name TEXT PRIMARY KEY, - last_curated INTEGER + 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("Fetching full track catalog from Gonic API...", flush=True) - auth = get_auth_params() - - res = requests.get(f"{GONIC_URL}/getArtists.view", params=auth).json() - indexes = res.get("subsonic-response", {}).get("artists", {}).get("index", []) - - all_tracks = [] - for index_group in indexes: - for artist in index_group.get("artist", []): - artist_id = artist["id"] - artist_res = requests.get(f"{GONIC_URL}/getArtist.view", params={**auth, "id": artist_id}).json() - albums = artist_res.get("subsonic-response", {}).get("artist", {}).get("album", []) - - for album in albums: - album_id = album["id"] - album_res = requests.get(f"{GONIC_URL}/getAlbum.view", params={**auth, "id": album_id}).json() - songs = album_res.get("subsonic-response", {}).get("album", {}).get("song", []) - - for song in songs: - all_tracks.append(( - str(song.get("id")), - song.get("title", "Unknown"), - song.get("artist", "Unknown"), - song.get("album", "Unknown"), - song.get("genre", "Unknown"), - song.get("year", 0) - )) + print("[sync] Fetching full track catalog from Gonic", flush=True) - conn = sqlite3.connect(DB_PATH) + 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") # Full refresh - cursor.executemany("INSERT OR REPLACE INTO tracks VALUES (?, ?, ?, ?, ?, ?)", all_tracks) + cursor.execute("DELETE FROM tracks") + cursor.executemany("INSERT OR REPLACE INTO tracks VALUES (?, ?, ?, ?, ?, ?, ?)", all_tracks) conn.commit() conn.close() - print(f"Synced {len(all_tracks)} tracks to local SQLite index.", flush=True) + print(f"[sync] Indexed {len(all_tracks)} tracks", flush=True) + def check_library_changed(): - auth = get_auth_params() - res = requests.get(f"{GONIC_URL}/getIndexes.view", params=auth).json() - indexes_data = str(res.get("subsonic-response", {}).get("indexes", {})) + 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): - with open(HASH_PATH, "r") as f: - stored_hash = f.read() - if stored_hash == current_hash: - return False + 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) - with open(HASH_PATH, "w") as f: - f.write(current_hash) 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 = sqlite3.connect(DB_PATH) + conn = get_db() cursor = conn.cursor() - - words = [w for w in prompt.lower().split() if len(w) > 3] + + keywords = _extract_keywords(prompt) rows = [] - - # 1. Grab a focused subset of keyword matches (capped at 200 so it doesn't drown out the random pool) - if words: - query_clauses = " OR ".join([f"genre LIKE ? OR title LIKE ? OR artist LIKE ? OR album LIKE ?" for _ in words]) + + # 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 words: - params.extend([f"%{w}%", f"%{w}%", f"%{w}%", f"%{w}%"]) - - cursor.execute(f"SELECT id, title, artist, album, genre, year FROM tracks WHERE {query_clauses} LIMIT 200", 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() - - # 2. Pad aggressively with random tracks to reach a target pool of 350 - # This ensures high serendipity and unearths unexpected gems from across the library. - target_pool_size = 350 - existing_ids = {r[0] for r in rows} - - if len(rows) < target_pool_size: - needed = target_pool_size - len(rows) - cursor.execute("SELECT id, title, artist, album, genre, year FROM tracks ORDER BY RANDOM() LIMIT ?", (needed,)) - extra_rows = cursor.fetchall() - - for r in extra_rows: - if r[0] not in existing_ids: + + # 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) - existing_ids.add(r[0]) - + seen.add(r[0]) + conn.close() - - return [{"id": r[0], "title": r[1], "artist": r[2], "album": r[3], "genre": r[4], "year": r[5]} for r in rows] + 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 + ] -def ask_gemini_for_tracks(prompt, candidates): - system_instruction = ( - "You are an expert music archivist, tastemaker, and DJ. " - "Your task is to curate tracks from a candidate pool that match a given thematic prompt or vibe. " - "CRITICAL INSTRUCTIONS:\n" - "1. Leverage your deep internal knowledge of music, artist styles, moods, and instrumentation. Do not rely solely on database tags or genres, as metadata can be messy or absent; use your understanding of the artist and song.\n" - "2. Prioritize absolute stylistic and emotional fit. Quality and vibe alignment matter far more than quantity: do not pad the selection with weak or unrelated matches.\n" - "3. You have complete freedom over the count: select anywhere from zero up to every single track that genuinely fits the criteria. There are no quotas, minimums, or maximum limits.\n" - "4. Return ONLY a raw JSON array containing the string IDs of the chosen tracks.\n" - "5. Output NO markdown, NO code blocks (do not use ```json ... ```), and NO conversational text whatsoever." - ) - - user_content = f"Prompt/Theme: {prompt}\n\nCandidate Tracks:\n{json.dumps(candidates, indent=2)}" - max_retries = 10 - delay = 5 - response = None +# --------------------------------------------------------------------------- +# 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-3.8-flash", + model=GEMINI_MODEL, contents=user_content, config=dict( system_instruction=system_instruction, @@ -217,145 +506,432 @@ def ask_gemini_for_tracks(prompt, candidates): response_mime_type="application/json" ) ) - break + 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: - if "503" in str(e) or "UNAVAILABLE" in str(e): - if attempt < max_retries - 1: - print(f"Gemini capacity spike (503). Retrying in {delay}s (Attempt {attempt + 1}/{max_retries})...", flush=True) - time.sleep(delay) - delay *= 2 - continue - raise e + kind = _classify_error(e) - if not response: - return [] + if kind == "quota": + raise QuotaExhaustedError(str(e)) - try: - return json.loads(response.text) - except Exception as e: - print(f"Error parsing Gemini response: {e}\nRaw output: {response.text}", flush=True) - return [] - -def ensure_and_process_ai_playlists(library_has_changed): - auth = get_auth_params() - now = int(time.time()) - - # Fetch existing playlists owned by our script user - res = requests.get(f"{GONIC_URL}/getPlaylists.view", params=auth).json() - existing_playlists = { - pl.get("name"): pl.get("id") - for pl in res.get("subsonic-response", {}).get("playlists", {}).get("playlist", []) - } - - conn = sqlite3.connect(DB_PATH) - cursor = conn.cursor() - - for defn in AI_PLAYLIST_DEFINITIONS: - name = defn["name"] - prompt = defn["prompt"] - playlist_id = existing_playlists.get(name) - - # 1. Create playlist automatically if it doesn't exist for this user account - if not playlist_id: - print(f"Playlist '{name}' not found for user. Creating it...", flush=True) - requests.get(f"{GONIC_URL}/createPlaylist.view", params={ - **auth, - "name": name, - "public": "true" - }) - - # Re-fetch to grab the newly generated ID - res = requests.get(f"{GONIC_URL}/getPlaylists.view", params=auth).json() - for pl in res.get("subsonic-response", {}).get("playlists", {}).get("playlist", []): - if pl.get("name") == name: - playlist_id = pl.get("id") - break - - if not playlist_id: - print(f"Failed to create or locate playlist '{name}'. Skipping.", flush=True) + 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 - # 2. Fetch current playlist contents from Gonic - pl_detail_res = requests.get(f"{GONIC_URL}/getPlaylist.view", params={**auth, "id": playlist_id}).json() - existing_entries = pl_detail_res.get("subsonic-response", {}).get("playlist", {}).get("entry", []) + 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 - # 3. Check last curation timestamp from SQLite state table - cursor.execute("SELECT last_curated FROM playlist_state WHERE name = ?", (name,)) - row = cursor.fetchone() - last_curated = row[0] if row else 0 + print(f"[gemini] Unrecoverable error: {e}", flush=True) + raise - # Decision Matrix for running Gemini: - # - Run if empty/new - # - Run if weekly cooldown (7 days) has expired (periodic refresh for variety) - # - Run if library changed AND it's been at least 24 hours since last curation - is_empty = not existing_entries - cooldown_expired = (now - last_curated) > CURATION_COOLDOWN - library_refresh_due = library_has_changed and (now - last_curated) > 86400 + print("[gemini] All retries exhausted, returning empty result", flush=True) + return [] - if not (is_empty or cooldown_expired or library_refresh_due): - print(f"Playlist '{name}' is up to date (Last curated {(now - last_curated) // 3600}h ago). Skipping Gemini.", flush=True) - continue - reason = "Empty/New" if is_empty else ("Periodic refresh" if cooldown_expired else "Library update") - print(f"Processing AI Playlist ({reason}): '{name}' with prompt: '{prompt}'", flush=True) - - candidates = query_candidate_tracks(prompt) - if not candidates: - print("No matching candidate tracks found in local index.", flush=True) - continue - - selected_ids = ask_gemini_for_tracks(prompt, candidates) - print(f"Gemini selected {len(selected_ids)} tracks.", flush=True) - - if selected_ids: - existing_song_ids = {str(song.get("id")) for song in existing_entries} - tracks_to_add = [sid for sid in selected_ids if sid not in existing_song_ids] - - if tracks_to_add: - query_list = [ - ("u", auth["u"]), ("p", auth["p"]), ("v", auth["v"]), - ("c", auth["c"]), ("f", auth["f"]), - ("playlistId", playlist_id), ("name", name), - ("comment", prompt), - ("public", "true") - ] - for sid in tracks_to_add: - query_list.append(("songIdToAdd", sid)) - - update_res = requests.get(f"{GONIC_URL}/updatePlaylist.view", params=query_list).json() - if update_res.get("subsonic-response", {}).get("status") == "ok": - print(f"Successfully added {len(tracks_to_add)} new tracks to '{name}'!", flush=True) - else: - print(f"Failed to update playlist: {update_res}") - else: - print(f"Playlist '{name}' has no new unique tracks to add from this selection.", flush=True) +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) - # Update last curated timestamp in SQLite state table - cursor.execute("INSERT OR REPLACE INTO playlist_state (name, last_curated) VALUES (?, ?)", (name, now)) - conn.commit() +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 CLEARLY do not belong based on the thematic brief. " + "CRITICAL INSTRUCTIONS:\n" + "1. Be VERY CONSERVATIVE. Only flag tracks you are HIGHLY CONFIDENT are a bad fit. " + "If there is any reasonable argument for keeping a track, KEEP IT.\n" + "2. Use your deep knowledge of music - artist styles, moods, instrumentation. " + "Do not rely solely on genre tags.\n" + "3. Respect STRICT EXCLUSIONS in the prompt - tracks violating an explicit " + "exclusion rule are fair game for removal.\n" + "4. Each track includes a duration_seconds field. Use it to enforce any " + "duration-based rules in the prompt.\n" + "5. When in doubt, KEEP the track.\n" + "6. Return ONLY a JSON array of string IDs of tracks to REMOVE. " + "Return [] if all tracks are acceptable.\n" + "7. 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("AI Playlist Sync one-shot execution started.", flush=True) - + print("[init] AI Playlist Sync started", flush=True) + try: - # 1. Sync library catalog only if changes are detected or local DB is missing + # Sync library if changed or local index is empty lib_changed = check_library_changed() - if lib_changed or not os.path.exists(DB_PATH): - print("Library changes detected (or missing local index). Updating local SQLite index...", flush=True) + if lib_changed or _tracks_table_empty(): + print("[sync] Library change detected or index empty, syncing", flush=True) sync_library_to_sqlite() else: - print("Library catalog unchanged. Using existing local SQLite index.", flush=True) - - # 2. Check and process playlists using the time-based curation cooldown matrix - ensure_and_process_ai_playlists(lib_changed) - print("AI Playlist Sync execution completed successfully.", flush=True) - + 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 during execution: {e}", flush=True) - raise e + print(f"[error] Unhandled exception: {e}", flush=True) + raise + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/requirements.txt b/requirements.txt index 7775a46..bd988ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ requests google-genai -dotenv \ No newline at end of file +python-dotenv