Random choice among cached songs

This commit is contained in:
Lucas Verney 2016-09-29 02:42:34 +02:00
parent 14c2ea65db
commit 38e43ea6db
1 changed files with 20 additions and 14 deletions

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"]
try:
mpd_password, mpd_host = mpd_host.split("@") 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,18 +168,21 @@ 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 ]
if len(cached_distances_close_enough) > 0:
# If there are some close enough songs in the cache
random_close_enough = random.choice(cached_distances_close_enough)
# Push it on the queue # Push it on the queue
client.add(cached_distances[0]["filename"]) client.add(random_close_enough["filename"])
# Continue using latest pushed song as current song # Continue using latest pushed song as current song
logging.info("Using cached distance. Found %s. Distance is (%f, %f)." % logging.info("Using cached distance. Found %s. Distance is (%f, %f)." %
(cached_distances[0]["filename"], (random_close_enough["filename"],
cached_distances[0]["distance"], random_close_enough["distance"],
cached_distances[0]["similarity"])) random_close_enough["similarity"]))
current_song_coords = cached_distances[0] current_song_coords = random_close_enough
continue continue
# Get all other songs coordinates and iterate randomly on them # Get all other songs coordinates and iterate randomly on them