1

am new to Python and have been struggling to create a workable script for a friends project - am hoping someone can point me in the right direction!

The script is to control playback of mp3 files from certain directories on certain days. So, for example, on May 1 2019 at 10:00 I want to play all the mp3 files in directory 000 and I want them to repeat until 10:00 the next day. Then, on May 2 at 10:00 I want to play all the mp3 files from directory 001, May 3/003, May 4/004 etc.

I thought it would be quite straightforward but seem to be getting myself into a bit of a pickle. I've managed to use the crontab to play the files in one directory (using omxplayer) at 10:00 each day and I've cobbled together a script that plays the files in a loop and have looked into timedate and system functions but I don’t know where I should start to put this together.

I want to do it in an elegant and DRY fashion but am limited on the backend stuff (I do frontend mostly).

My initial thinking is that I need to have a lookup table to map the days to the correct directories - would that be right? Then I could, I presume, have something along the lines of ‘if day is x then run files in directory y’ with a control for resetting it at 10:00 the next day. My original plan for that was to just reboot the RPi using the crontab at 09:58 to a, halt the loop and b, rest everything for the new day.

Figure there must be a 'correct' way to do this as I just seem to be murdering the code by hacking bits and pieces together.

Also - the RPi won’t be online and will have wi-fi turned off - am using a real-time clock for timing.

I’ve been trying to figure all this out but hadn’t anticipated having to learn Python to quite the level that it seems is needed. Am learning some awesome stuff though - which is good as I've just bought my kids a Pi starter kit on the back of this.

Any help/pointers/snippets appreciated.

0

It’s difficult to thoroughly test something like this since I’m not going spend the next month or two making sure that it’s still all working well. That being said, I think that this code will be a good starting point for you.

Put the script in a directory that has 7 directories below it : Mon Tue Wed Thu Fri Sat Sun. Place the songs in those folders. At ten o’clock each day, the script should switch to the folder of the day and then play those songs over and over until ten o’clock the next day. If you have trouble with this, let me know I I’ll see if I can fix it for you.

from time import strftime
import subprocess
import os

# strftime ('%a, %d %b %Y %H:%M:%S')
#     Tue, 16 Apr 2019 17:38:01

waiting_for_ten = True
os.chdir ('Tue')

def just_turned_ten () :
    global waiting_for_ten
    hour = strftime ('%H')
    if hour == '10' and waiting_for_ten :
        waiting_for_ten = False
        return True
    elif hour != '10' and not waiting_for_ten :
        waiting_for_ten = True
    return False


def get_song_list () :
    pathname = '../' + strftime ('%a')
    os.chdir (pathname)
    raw_data = str (subprocess.check_output ('ls'))
    song_list = raw_data [2: -3].split ('\\n')
    return song_list

while True :
    todays_songs = get_song_list ()
    for song in todays_songs :
        print ('Now playing "{}".'.format (song))
        subprocess.run (['omxplayer', song])
        if just_turned_ten () : break

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.