Bezugnehmend auf mein Thema: viewtopic.php?t=47037
habe ich einen Freelancer gebeten, sich der Sache anzunehmen. Er hat daraufhin ein Script in Python3 geschrieben, welcher das Problem aus dem anderen Thread lösen sollte. Sprich:
- Es gibt vier Ordner + ein Jingle-Ordner
- es gibt verschiedene Zeitzonen (z.B. 6-12 Uhr), und dann ist jeweils ein bestimmter Ordner aktiv
- Aus dem derzeitign Ordner wird dann jeweils ein Song herausgeholt
- nach allen zwei Songs kommt ein Song aus dem Jingle-Ordner
Könntet ihr mal gucken, ob das so funktionieren würde?
Wenn der Code so funktioniert, reicht es dann, die derzeit aktiven Dateien einfach zu überschreiben oder muss der gesamte Server neugestartet werden?
Code: Alles auswählen
import os
import random
import datetime
import glob
import json
def songToPlay(directory, alreadyPlayed, totalRuns):
os.chdir(directory)
songsfiles = []
for file in glob.glob("*.mp3"):
songsfiles.append(file)
length = len(songsfiles)
count = 0
while True:
selectedSong = random.choice(songsfiles)
if selectedSong not in alreadyPlayed:
alreadyPlayed.append(selectedSong)
totalRuns += 1
if totalRuns > 2:
totalRuns = 0
return os.path.join(directory, selectedSong), alreadyPlayed, totalRuns
if count == length:
for file in glob.glob("*.mp3"):
try:
alreadyPlayed.remove(file)
except:
pass
count = 0
count += 1
def main():
directory = os.path.dirname(os.path.abspath(__file__))
DayRotation = os.path.join(directory, "DayRotation")
EveRotation = os.path.join(directory, "EveRotation")
MiddayRotation = os.path.join(directory, "MiddayRotation")
NightRotation = os.path.join(directory, "NightRotation")
jinglesDir = os.path.join(directory, "Jingles")
currentHour = datetime.datetime.now().hour
jsonFile = open('status.json', 'r')
data = json.load(jsonFile)
totalRuns = data["totalRuns"]
alreadyPlayed = data["playedSongs"]
if 0 <= currentHour <= 6 and totalRuns < 2:
print('Playing Night Rotations')
url, alreadyPlayed, totalRuns = songToPlay(NightRotation, alreadyPlayed, totalRuns)
elif 6 < currentHour <= 12 and totalRuns < 2:
print('Playing Midday Rotation')
url, alreadyPlayed, totalRuns = songToPlay(MiddayRotation, alreadyPlayed, totalRuns)
elif 12 < currentHour <= 18 and totalRuns < 2:
print('Playing Day Rotation')
url, alreadyPlayed, totalRuns = songToPlay(DayRotation, alreadyPlayed, totalRuns)
elif 18 < currentHour <= 24 and totalRuns < 2:
print('Playing Eve Rotation')
url, alreadyPlayed, totalRuns = songToPlay(EveRotation, alreadyPlayed, totalRuns)
elif totalRuns == 2:
print('Playing jingles')
url, alreadyPlayed, totalRuns = songToPlay(jinglesDir, alreadyPlayed, totalRuns)
songsDict = {"totalRuns": totalRuns, "playedSongs": alreadyPlayed}
print(url)
os.chdir(directory)
with open('status.json', 'w+') as outfile:
json.dump(songsDict, outfile)
main()
Code: Alles auswählen
{"totalRuns": 0, "playedSongs": []}