Random choice among cached songs

This commit is contained in:
Lucas Verney 2016-09-29 02:42:34 +02:00
parent 14c2ea65db
commit 38e43ea6db

View File

@ -107,7 +107,10 @@ def main(queue_length):
# Get MPD connection settings # Get MPD connection settings
try: try:
mpd_host = os.environ["MPD_HOST"] mpd_host = os.environ["MPD_HOST"]
mpd_password, mpd_host = mpd_host.split("@") try:
mpd_password, mpd_host = mpd_host.split("@")
except ValueError:
mpd_password = None
except KeyError: except KeyError:
mpd_host = "localhost" mpd_host = "localhost"
mpd_password = None mpd_password = None
@ -165,19 +168,22 @@ def main(queue_length):
if ("file: %s" % (row["filename"],)) not in client.playlist()] if ("file: %s" % (row["filename"],)) not in client.playlist()]
cached_distances_songs = [i["filename"] for i in cached_distances] cached_distances_songs = [i["filename"] for i in cached_distances]
# If distance to closest song is ok, just add the song # Get the songs close enough
if len(cached_distances) > 0: cached_distances_close_enough = [
if(cached_distances[0]["distance"] < _DISTANCE_THRESHOLD and row for row in cached_distances
cached_distances[0]["similarity"] > _SIMILARITY_THRESHOLD): if row["distance"] < _DISTANCE_THRESHOLD and row["similarity"] > _SIMILARITY_THRESHOLD ]
# Push it on the queue if len(cached_distances_close_enough) > 0:
client.add(cached_distances[0]["filename"]) # If there are some close enough songs in the cache
# Continue using latest pushed song as current song random_close_enough = random.choice(cached_distances_close_enough)
logging.info("Using cached distance. Found %s. Distance is (%f, %f)." % # Push it on the queue
(cached_distances[0]["filename"], client.add(random_close_enough["filename"])
cached_distances[0]["distance"], # Continue using latest pushed song as current song
cached_distances[0]["similarity"])) logging.info("Using cached distance. Found %s. Distance is (%f, %f)." %
current_song_coords = cached_distances[0] (random_close_enough["filename"],
continue random_close_enough["distance"],
random_close_enough["similarity"]))
current_song_coords = random_close_enough
continue
# Get all other songs coordinates and iterate randomly on them # Get all other songs coordinates and iterate randomly on them
closest_song = None closest_song = None