48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
from config import *
|
|
from lastfm_helpers import lf_request, recent_artists
|
|
from lidarr_helpers import lidarr_api_add_artist
|
|
from musicbrainz_helpers import load_cache, save_cache
|
|
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"🎧 Analyserar {len(recent)} senaste artister från Last.fm")
|
|
|
|
new = 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:
|
|
js = lf_request("artist.getSimilar", mbid=mbid, limit=MAX_SIMILAR_PER_ART)
|
|
sims = js.get("similarartists", {}).get("artist", []) if js else []
|
|
similar_cache[mbid] = {"ts": time.time(), "data": sims}
|
|
save_cache(cache)
|
|
|
|
for sim in sims:
|
|
sid = sim.get("mbid")
|
|
match = float(sim.get("match", 0))
|
|
if not sid or sid in added_artists or match < SIMILAR_MATCH_MIN:
|
|
continue
|
|
|
|
log.info(f"✨ Ny artist: {sim.get('name')} (match {match:.2f})")
|
|
if lidarr_api_add_artist(sid):
|
|
added_artists.add(sid)
|
|
new += 1
|
|
cache["added_artists"] = list(added_artists)
|
|
save_cache(cache)
|
|
|
|
log.info(f"✅ Klar! {new} nya artister tillagda på {((time.time()-start)/60):.1f} min")
|
|
|
|
if __name__ == "__main__":
|
|
sync()
|