Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Recomended Playlist - MPD
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import random
from mpd import (MPDClient, CommandError)
from audioscrobbler import AudioScrobblerQuery
""" MPD """
mpd_host = 'localhost'
mpd_port = '6600'
mpd_connection = {'host': mpd_host, 'port': mpd_port}
client = MPDClient()
COUNT_SONGS = 5
def mpd_connect():
try:
client.connect(**mpd_connection)
except SocketError:
return False
return True
def main():
if not mpd_connect():
sys.exit(1)
# Meh, no se si se hace esto, pero bueh
recommended_artists = []
recommended_songs = []
# Obtengo la cancion que esta sonando en este momento
current = client.currentsong()
# Limpio el artista del diccionario que me devuelve current
current_artist = AudioScrobblerQuery(artist=current['artist'])
# En base a todos los artistas similares, me fijo cuales tengo en el
# server MPD, y los almaceno para usarlos despues.
for artist in current_artist.similar():
if float(client.count("artist", artist.name)['songs']) > 0:
recommended_artists.append(artist.name)
# Podria hacerlo mas efectivo, pero la idea es hacerlo lo mas random que
# pueda, asi que desordeno la lista de similares, que siempre viene ya
# ordenada por compatibilidad del current_artist
random.shuffle(recommended_artists)
while len(recommended_songs) < COUNT_SONGS:
artist = random.choice(recommended_artists)
recommended_songs.append(
random.choice(client.search('artist', artist)))
for song in recommended_songs:
print song["artist"], "-", song["title"]
client.add(song["file"])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment