Hilfe bei einem Script
Verfasst: Samstag 23. November 2019, 20:59
Hallo liebes Forum,
wir haben ein Webradio und da läuft folgende Regelung:
- Es gibt zwei Ordner, aus denen sich das Script (playlist.py) die Songs zieht. Einmal von 10-0 und einmal von 0 - 10. Nun wollen wir aber folgende Struktur:
0 – 6 > NightRotation
6 – 12 > MiddayRotation
12 – 18 > DayRotation
18 – 24 > AbendRotation
Leider sind wir was Python angeht völlige Noobs, und unser PRogrammierer ist aufgrund von Studium zeitlich nicht mehr in der Lage, mitzumachen. Daher würde ich mich freuen, wenn ihr uns dieses Problem löst
Vielen lieben dank
wir haben ein Webradio und da läuft folgende Regelung:
- Es gibt zwei Ordner, aus denen sich das Script (playlist.py) die Songs zieht. Einmal von 10-0 und einmal von 0 - 10. Nun wollen wir aber folgende Struktur:
0 – 6 > NightRotation
6 – 12 > MiddayRotation
12 – 18 > DayRotation
18 – 24 > AbendRotation
Leider sind wir was Python angeht völlige Noobs, und unser PRogrammierer ist aufgrund von Studium zeitlich nicht mehr in der Lage, mitzumachen. Daher würde ich mich freuen, wenn ihr uns dieses Problem löst

Code: Alles auswählen
#!/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)
Vielen lieben dank
