discoverylidarr/discovery_sync.py

50 lines
No EOL
1.8 KiB
Python

# discovery_sync.py
from config import *
from lastfm_helpers import recent_artists, get_similar_artists_pylast # Korrekt
from lidarr_helpers import lidarr_api_add_artist # Antar att lidarr_helpers bara har denna funktion att exportera
from musicbrainz_helpers import load_cache, save_cache # Antar att musicbrainz_helpers bara har dessa funktioner att exportera
import time, logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("DiscoveryLidarr")
def sync():
start = time.time()
cache = load_cache()
added_artists = set(cache.get("added_artists", []))
similar_cache = cache.setdefault("similar_cache", {})
recent = recent_artists()
log.info(f"🎧 Analyzing {len(recent)} recent artists from Last.fm")
new_artists_added_count = 0
for name, mbid in recent:
if not mbid or mbid in added_artists:
continue
if mbid in similar_cache:
sims = similar_cache[mbid]["data"]
else:
sims = get_similar_artists_pylast(mbid, MAX_SIMILAR_PER_ART)
similar_cache[mbid] = {"ts": time.time(), "data": sims}
save_cache(cache)
for sim_artist_obj, match in sims:
sid = sim_artist_obj.mbid
match_float = float(match)
if not sid or sid in added_artists or match_float < SIMILAR_MATCH_MIN:
continue
log.info(f"✨ New artist: {sim_artist_obj.name} (match {match_float:.2f})")
if lidarr_api_add_artist(sid):
added_artists.add(sid)
new_artists_added_count += 1
cache["added_artists"] = list(added_artists)
save_cache(cache)
log.info(f"✅ Done! {new_artists_added_count} new artists added in {((time.time()-start)/60):.1f} min")
if __name__ == "__main__":
sync()