Page 1 of 1

Trying to get a RPi to play a delayed playlist using MPD

Posted: May 11th, 2015, 3:11 am
by jb_bak
I moved to the other side of the world a few months back, and am trying to get my local streaming radio station delayed so that it feels like it's still on the same timezone as it use to be.

I have MPD and icecast configured on a raspberry pi working good. Have streamripper ripping MP3s from my streaming station. That works well. So where I am having troulbe is getting the files added and updated on the proper time sequence using crontab. Here is a stupid script I made that isn't sorting properly. So it deletes anything older than 12 hours, and then is suppose to sort by date and time (part not working). What am I doing wrong?

Thanks for the help!

#!/bin/sh

rm -r /var/lib/mpd/music/incomplete
sleep 1
find /var/lib/mpd/music/ -type f -mmin +720 -exec rm {} \;
sleep 10
/usr/bin/mpc clear
sleep 5
/usr/bin/mpc update
sleep 5
ls -qaltr | /usr/bin/mpc ls | /usr/bin/mpc add
sleep 5
/usr/bin/mpc play

Re: Trying to get a RPi to play a delayed playlist using MPD

Posted: May 11th, 2015, 8:28 am
by max
Do you seek assistance with writing shell scripts, or do you seek assistance with MPD?

Re: Trying to get a RPi to play a delayed playlist using MPD

Posted: May 11th, 2015, 9:12 am
by jb_bak
I need help with scripts, obviously I don't know what i'm doing here :)

The script just needs to delete all but the last 12 hours of mp3s (stream ripper writes individual songs as they are completed) then sort them from oldest to newest, and add the playlist. maybe run a cron that does that ever 12 hours or so.

Re: Trying to get a RPi to play a delayed playlist using MPD

Posted: May 12th, 2015, 7:58 pm
by Rasi
use python3. needs the python-mpd2 module:

Code: Select all

#!/usr/bin/env python3

from mpd import MPDClient
c=MPDClient()

c.connect('127.0.0.1', '6600')

c.clear()
c.update()
dirs=c.lsinfo('/')
sorted_dirs = sorted(dirs, key=lambda k: k['last-modified']) 

for i in sorted_dirs:
    if 'directory' in i:
        c.add(i['directory'])

c.play()
save the script somewhere in your PATH.

then edit your original script (which i havent checked now, but I trust your words, it worked :P)

Code: Select all

#!/bin/sh

rm -r /var/lib/mpd/music/incomplete
find /var/lib/mpd/music/ -type f -mmin +720 -exec rm {} \;
/path/to/saved_script.py
#########################################

Alternatively a rather ugly solution in pure bash:
for this to work, make sure your mpd has a unix-socket defined in config
complete script:

Code: Select all

#!/bin/bash

rm -r /var/lib/mpd/music/incomplete
find /var/lib/mpd/music/ -type f -mmin +720 -exec rm {} \;
dir="/path/to/your/files"
cd "${dir}"
for i in $(ls -t); do mpc add "file://{dir}/{i}"; done

Re: Trying to get a RPi to play a delayed playlist using MPD

Posted: May 13th, 2015, 2:58 am
by jb_bak
hi thanks so much for your help. I tried both solutions. neither I could get to work. First one i did a pip install python-mpd2 and put the script in /usr/bin/ as script.py and chmod +x it. Then modified my orginial to include the path. The script runs without errors, but no playlist.

The second solution give me a directory error:
error adding file://{dir}/{i}: No such song

I'm certain the diretory is right so i dont understand that one.

Re: Trying to get a RPi to play a delayed playlist using MPD

Posted: May 14th, 2015, 8:27 am
by Rasi
I tested the python script, it works perfectly here.

anyway. 2nd solution was missing a "$" - but again, this needs a connection via unix socket.

Code: Select all

export MPD_HOST=/path/to/mpd_socket_file

Code: Select all

for i in $(ls -t); do mpc add "file://"${dir}"/"${i}""; done

Re: Trying to get a RPi to play a delayed playlist using MPD

Posted: May 15th, 2015, 12:49 am
by jb_bak
Thanks again Raspi for all the help. Sure wish the python script would work. Have no idea why it doesn't, as there are no errors, but the playlist is empty after running the script.

For the second way, I have removed the hash in front of
# And for Unix Socket
bind_to_address "/run/mpd/socket"
and restarted and ran the export command with that dir as you suggested.

When I run my script now with the updated $s that were missing, i'm now getting:
error adding file:///var/lib/mpd/music/-: Not found
error adding file:///var/lib/mpd/music/Up: Not found
error adding file:///var/lib/mpd/music/in: Not found
error adding file:///var/lib/mpd/music/the: Not found
error adding file:///var/lib/mpd/music/Dark.mp3: Not found
error adding file:///var/lib/mpd/music/Frankie: Not found
error adding file:///var/lib/mpd/music/Rose: Not found


I am thinking this has to do with the spaces in my song files? I wish I was smart enough to figure out how to fix this. Thanks again for all the help. Almost there!

Re: Trying to get a RPi to play a delayed playlist using MPD

Posted: May 15th, 2015, 6:37 pm
by Rasi
try again with latest edit

Re: Trying to get a RPi to play a delayed playlist using MPD

Posted: May 16th, 2015, 2:34 am
by jb_bak
Thank you!! That worked nicely! Ahh finally got this going, so cool. I owe you a beer!