Skip to content

Instantly share code, notes, and snippets.

Created June 30, 2013 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5896406 to your computer and use it in GitHub Desktop.
Save anonymous/5896406 to your computer and use it in GitHub Desktop.
Prints the timetable of upcoming playlist entries for MPD. The following code is provided as public domain.
#!/usr/bin/python
# Configuration
HOST = 'localhost'
PORT = 6600
PASSWORD = ''
TIME_FORMAT = r'%H:%M' #use `man strftime` for more info
# End of configuration
import mpd
import sys
import time
def connect_to_mpd(HOST='localhost',PORT=6600,PASSWORD=False):
'''Make connecting to MPD easier'''
client = mpd.MPDClient()
try:
client.connect(host=HOST, port=PORT)
except socket.error:
sys.exit(1)
if PASSWORD:
try:
client.password(PASSWORD)
except mpd.CommandError:
sys.exit(1)
return client
def formatted_time(unix_time):
return time.strftime(TIME_FORMAT,time.localtime(unix_time))
def print_mpd_schedule(client):
'''Print a schedule for current MPD playlist'''
start_time = time.time()
playlist = client.playlistinfo()
current_song = playlist.index(client.currentsong())
deduction = float(client.status()['time'].split(':')[0])
for song in playlist[current_song:]:
end_time = start_time + float(song['time']) - deduction
print '[%s-%s] "%s" by %s' % (formatted_time(start_time), formatted_time(end_time), song['title'], song['artist'])
start_time = end_time
deduction = 0
def main():
mpd_client = connect_to_mpd(HOST,PORT,PASSWORD)
if mpd_client.status()['state'] == 'play' and not mpd_client.status()['random'] == '1':
print_mpd_schedule(mpd_client)
else:
sys.stderr.write('MPD is not playing anything or is in shuffle mode\n')
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment