Add MPD backup
This commit is contained in:
parent
fd50ebe6fd
commit
c06bf813d8
@ -1,17 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
#
|
# Back up the state of MPD (current playlist and so on)
|
||||||
# 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 <https://pypi.python.org/pypi/python-mpd2>:
|
|
||||||
# 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 <tim@treehouse.org.za>
|
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
import datetime
|
import datetime
|
||||||
@ -21,6 +9,7 @@ import sys
|
|||||||
import pprint
|
import pprint
|
||||||
import mpd
|
import mpd
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
from subprocess import Popen, PIPE
|
||||||
|
|
||||||
parser = OptionParser()
|
parser = OptionParser()
|
||||||
parser.add_option("-s", "--server", dest="host",
|
parser.add_option("-s", "--server", dest="host",
|
||||||
@ -37,9 +26,14 @@ parser.add_option("-f", "--file", dest="load_file",
|
|||||||
|
|
||||||
host = options.host or 'localhost'
|
host = options.host or 'localhost'
|
||||||
port = options.port or '6600'
|
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")
|
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 = mpd.MPDClient()
|
||||||
client.timeout = 10
|
client.timeout = 10
|
||||||
client.idletimeout = 10
|
client.idletimeout = 10
|
||||||
@ -117,25 +111,26 @@ else:
|
|||||||
state = {}
|
state = {}
|
||||||
playlist = []
|
playlist = []
|
||||||
for song in get_playlist:
|
for song in get_playlist:
|
||||||
playlist.append(song["file"])
|
playlist.append(song.get('file', ''))
|
||||||
|
|
||||||
state = dict(
|
state = dict(
|
||||||
playlist = playlist,
|
playlist = playlist,
|
||||||
src = host+":"+port,
|
src = host+":"+port,
|
||||||
consume = get_status['consume'],
|
consume = get_status.get('consume', ''),
|
||||||
current = get_currentsong['file'] if get_currentsong['file'] else '',
|
current = get_currentsong.get('file', ''),
|
||||||
random = get_status['random'],
|
time = get_status.get('time', '').split(":")[0],
|
||||||
repeat = get_status['repeat'],
|
random = get_status.get('random', ''),
|
||||||
single = get_status['single'],
|
repeat = get_status.get('repeat', ''),
|
||||||
state = get_status['state'],
|
single = get_status.get('single', ''),
|
||||||
time = get_status['time'].split(":")[0] if get_status['time'] else '',
|
state = get_status.get('state', ''),
|
||||||
volume = get_status['volume'],
|
volume = get_status.get('volume', ''),
|
||||||
)
|
)
|
||||||
|
|
||||||
pp = pprint.PrettyPrinter(indent=4)
|
pp = pprint.PrettyPrinter(indent=4)
|
||||||
current_output = pp.pformat(state)
|
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*?[\'\"].*?[\'\"],?');
|
strip = re.compile(r'\s*?[\'\"](consume|current|random|repeat|single|state|time|volume)[\'\"]:\s*?[\'\"].*?[\'\"],?');
|
||||||
curr = strip.sub('', current_output)
|
curr = strip.sub('', current_output)
|
||||||
prev = strip.sub('', backup_output)
|
prev = strip.sub('', backup_output)
|
||||||
@ -151,4 +146,4 @@ else:
|
|||||||
client.close()
|
client.close()
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|
||||||
# vim: set expandtab shiftwidth=4 softtabstop=4 :
|
# vim: set expandtab shiftwidth=4 softtabstop=4 textwidth=79:
|
||||||
|
Loading…
Reference in New Issue
Block a user