Created
June 30, 2013 19:01
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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