""" Per-Show Setlist Importer Fetches setlist for each show individually """ import requests from sqlmodel import Session, select from database import engine from models import Show, Song, Performance BASE_URL = "https://elgoose.net/api/v2" def fetch_show_setlist(api_show_id): """Fetch setlist for a specific show""" url = f"{BASE_URL}/setlists/showid/{api_show_id}.json" try: response = requests.get(url, timeout=30) response.raise_for_status() data = response.json() if data.get('error') == 1: return None return data.get('data', []) except Exception as e: return None def main(): print("PER-SHOW SETLIST IMPORTER") print("=" * 40) with Session(engine) as session: # Get all shows shows = session.exec(select(Show)).all() print(f"Found {len(shows)} shows in database") # Build song map by title songs = session.exec(select(Song)).all() song_map = {s.title.lower(): s.id for s in songs} print(f"Mapped {len(song_map)} songs") # Get existing performances existing = set() perfs = session.exec( select(Performance.show_id, Performance.song_id, Performance.position) ).all() for p in perfs: existing.add((p[0], p[1], p[2])) print(f"Found {len(existing)} existing performances") # We need API show IDs. The ElGoose API shows endpoint returns show_id. # Let's fetch and correlate by date print("Fetching API shows to get API IDs...") api_shows = {} # date_str -> api_show_id page = 1 while True: url = f"{BASE_URL}/shows.json" try: resp = requests.get(url, params={"artist": 1, "page": page}, timeout=30) data = resp.json().get('data', []) if not data: break for s in data: date_str = s['showdate'] api_shows[date_str] = s['show_id'] page += 1 if page > 50: break except: break print(f"Got {len(api_shows)} API show IDs") # Now import setlists for each show total_added = 0 processed = 0 for show in shows: date_str = show.date.strftime('%Y-%m-%d') api_show_id = api_shows.get(date_str) if not api_show_id: continue # Check if we already have performances for this show existing_for_show = session.exec( select(Performance).where(Performance.show_id == show.id) ).first() if existing_for_show: continue # Skip shows that already have performances # Fetch setlist setlist = fetch_show_setlist(api_show_id) if not setlist: continue added = 0 for item in setlist: song_title = item.get('songname', '').lower() song_id = song_map.get(song_title) if not song_id: continue position = item.get('position', 0) key = (show.id, song_id, position) if key in existing: continue perf = Performance( show_id=show.id, song_id=song_id, position=position, set_name=item.get('set'), segue=bool(item.get('segue', 0)), notes=item.get('footnote') ) session.add(perf) existing.add(key) added += 1 total_added += 1 if added > 0: session.commit() processed += 1 print(f"Show {date_str}: +{added} songs ({total_added} total)") print(f"\\n✓ Added {total_added} performances from {processed} shows") if __name__ == "__main__": main()