Dec-12-2019, 06:56 PM
Hello dear forum,
we have a web radio and the following rules apply:
- There are two folders from which the script (playlist.py) pulls the songs. Once from 10-0 and once from 0-10. Now we want the following structure:
0 - 6> NightRotation
6 - 12> Midday rotation
12 - 18> DayRotation
18 - 24> evening rotation
And that the jingles are played every 5 songs.
Unfortunately, we are completely noobs in terms of Python, and because of our studies, our PRG is no longer able to participate. Therefore I would be happy if you solve this problem for us :-)
we have a web radio and the following rules apply:
- There are two folders from which the script (playlist.py) pulls the songs. Once from 10-0 and once from 0-10. Now we want the following structure:
0 - 6> NightRotation
6 - 12> Midday rotation
12 - 18> DayRotation
18 - 24> evening rotation
And that the jingles are played every 5 songs.
Unfortunately, we are completely noobs in terms of Python, and because of our studies, our PRG is no longer able to participate. Therefore I would be happy if you solve this problem for us :-)
#!/usr/bin/env python heavyRotationPercentage = 1.0 / 2.0 import os import random import pickle import datetime directory = os.path.dirname(os.path.abspath(__file__)) heavyRotationDir = os.path.join(directory, "HeavyRotation") normalRotationDir = os.path.join(directory, "NormalRotation") oldRotationDir = os.path.join(directory, "OldRotation") jinglesDir = os.path.join(directory, "Jingles") statusFile = os.path.join(directory, "status.txt") currentHour = datetime.datetime.now().hour status = [('','')]*8 if os.path.isfile(statusFile): with open(statusFile, "rb") as f: status = pickle.load(f) lastFiles = [i[0] for i in status] fileListFilter = lambda fileList: [fileName for fileName in fileList if fileName not in lastFiles and fileName[-4:] == ".mp3"] choosenFileDir = False choosenFileList = False if status[-1][1]!=jinglesDir: #Letzte Track war kein Jingle choosenFileDir = jinglesDir choosenFileList = fileListFilter(os.listdir(jinglesDir)) if currentHour >= 10 and choosenFileDir == False: #Von 10:00 bis 24:00 aus der normalen / Heavy Rotation waehlen filesInHeavyRotation = fileListFilter(os.listdir(heavyRotationDir)) filesInNormalRotation = fileListFilter(os.listdir(normalRotationDir)) if random.random() <= heavyRotationPercentage and len(filesInHeavyRotation) > 0: choosenFileDir = heavyRotationDir choosenFileList = filesInHeavyRotation elif len(filesInNormalRotation) > 0: choosenFileDir = normalRotationDir choosenFileList = filesInNormalRotation if choosenFileDir == False: # Von 00:00 bis 10:00 (oder wenn nichts anderes gefunden wurde) aus der alten Rotation waehlen choosenFileDir = oldRotationDir choosenFileList = fileListFilter(os.listdir(oldRotationDir)) choosenFile = random.choice(choosenFileList) with open(statusFile, "wb") as f: status.append((choosenFile, choosenFileDir)) pickle.dump(status[1:],f) print os.path.join(choosenFileDir, choosenFile)