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

Need help with MPD?
Post Reply
jb_bak
Posts: 5
Joined: May 11th, 2015, 3:07 am

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

Post 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
max
Forum team
Posts: 1139
Joined: January 15th, 2013, 3:43 pm

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

Post by max »

Do you seek assistance with writing shell scripts, or do you seek assistance with MPD?
jb_bak
Posts: 5
Joined: May 11th, 2015, 3:07 am

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

Post 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.
Rasi
Forum team
Posts: 141
Joined: January 16th, 2013, 7:29 pm

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

Post 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
jb_bak
Posts: 5
Joined: May 11th, 2015, 3:07 am

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

Post 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.
Rasi
Forum team
Posts: 141
Joined: January 16th, 2013, 7:29 pm

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

Post 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
jb_bak
Posts: 5
Joined: May 11th, 2015, 3:07 am

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

Post 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!
Rasi
Forum team
Posts: 141
Joined: January 16th, 2013, 7:29 pm

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

Post by Rasi »

try again with latest edit
jb_bak
Posts: 5
Joined: May 11th, 2015, 3:07 am

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

Post by jb_bak »

Thank you!! That worked nicely! Ahh finally got this going, so cool. I owe you a beer!
Post Reply