From c06bf813d8b508976095bf6ce61d8b6ec1c7a337 Mon Sep 17 00:00:00 2001 From: tim Date: Fri, 23 Oct 2015 16:34:21 +0200 Subject: [PATCH] Add MPD backup --- backup_mpd_playlist.py | 45 +++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/backup_mpd_playlist.py b/backup_mpd_playlist.py index 6827af5..90ef8c8 100755 --- a/backup_mpd_playlist.py +++ b/backup_mpd_playlist.py @@ -1,17 +1,5 @@ #!/usr/bin/env python -# -# Back up the state of MPD (current playlist and so on); this allows you -# to recover when you accidentally clear your playlist. -# -# This uses the python-mpd2 library from : -# sudo pip3 install python-mpd2 -# -# I run it from cron every 20 minutes or so (as user mpd, but you can -# run it as any user, passing in the backup directory with -d): -# cat /etc/cron.d/mpd_backup -# */20 * * * * mpd [ -x /var/lib/mpd/backup_mpd_playlist.py ] && python /var/lib/mpd/backup_mpd_playlist.py -# -# By Timothy Allen +# Back up the state of MPD (current playlist and so on) import ast import datetime @@ -21,6 +9,7 @@ import sys import pprint import mpd from optparse import OptionParser +from subprocess import Popen, PIPE parser = OptionParser() parser.add_option("-s", "--server", dest="host", @@ -37,9 +26,14 @@ parser.add_option("-f", "--file", dest="load_file", host = options.host or 'localhost' port = options.port or '6600' -backup_dir = options.dest or '/var/lib/mpd/backups' +#backup_dir = options.dest or '/var/lib/mpd/backups' +backup_dir = options.dest or '/home/tim/.mpd/backups' now = datetime.datetime.now().strftime("%Y%m%d@%H:%M") +ps = Popen(['ps', 'ax'], shell=False, stdout=PIPE).communicate()[0] +if not re.search("\d+\s+mpd\s+", ps): + sys.exit() + client = mpd.MPDClient() client.timeout = 10 client.idletimeout = 10 @@ -117,25 +111,26 @@ else: state = {} playlist = [] for song in get_playlist: - playlist.append(song["file"]) + playlist.append(song.get('file', '')) state = dict( playlist = playlist, src = host+":"+port, - consume = get_status['consume'], - current = get_currentsong['file'] if get_currentsong['file'] else '', - random = get_status['random'], - repeat = get_status['repeat'], - single = get_status['single'], - state = get_status['state'], - time = get_status['time'].split(":")[0] if get_status['time'] else '', - volume = get_status['volume'], + consume = get_status.get('consume', ''), + current = get_currentsong.get('file', ''), + time = get_status.get('time', '').split(":")[0], + random = get_status.get('random', ''), + repeat = get_status.get('repeat', ''), + single = get_status.get('single', ''), + state = get_status.get('state', ''), + volume = get_status.get('volume', ''), ) pp = pprint.PrettyPrinter(indent=4) current_output = pp.pformat(state) - # We don't care if the volume or currently-playing song has changed, only the playlist + # We don't care if the volume or currently-playing song has changed, only + # the playlist strip = re.compile(r'\s*?[\'\"](consume|current|random|repeat|single|state|time|volume)[\'\"]:\s*?[\'\"].*?[\'\"],?'); curr = strip.sub('', current_output) prev = strip.sub('', backup_output) @@ -151,4 +146,4 @@ else: client.close() client.disconnect() -# vim: set expandtab shiftwidth=4 softtabstop=4 : +# vim: set expandtab shiftwidth=4 softtabstop=4 textwidth=79: