dmx/getseq/getspread.py

72 lines
2.4 KiB
Python
Raw Normal View History

2024-08-07 11:57:25 +00:00
#!/usr/bin/python3
import io
import os
import csv
import json
import requests
import subprocess
#PLAYLIGHTS = 'https://calc...'
PLAYLIGHTS = 'some.csv'
# get lights from online / local CSV, convert to JSON
# saves ledisndh.csv and converts to sequence.json
pathbase = os.path.expanduser("~/bin/dmx")
def row2dict(csvrow):
thisLED = {"channel": csvrow["channel"]}
if csvrow["seq (position on timeline in seconds)"]:
thisLED["seq"] = int(csvrow["seq (position on timeline in seconds)"])
if csvrow["duration (sec)"]:
thisLED["duration"] = int(csvrow["duration (sec)"])
if csvrow["brightness (optional)"]:
thisLED["brightness"] = int(csvrow["brightness (optional)"])
if csvrow["fadein_opt (ms)"]:
thisLED["fadein"] = int(csvrow["fadein_opt (ms)"])
if csvrow["fadeout_opt (ms)"]:
thisLED["fadeout"] = int(csvrow["fadeout_opt (ms)"])
if csvrow["start_opt"] and csvrow["start_opt"].lower().strip() == "start":
thisLED["start_opt"] = "start"
return(thisLED)
# convert csv to json
def update_playlights():
playlights = []
if PLAYLIGHTS.startswith('http'):
print(f"\n - Getting CSV from {PLAYLIGHTS.replace('.csv','')} ...")
calc_content = requests.get(PLAYLIGHTS, timeout=5).text
# get, save CSV from calc.thing
csv_file = f"{pathbase}/getseq/{os.path.basename(PLAYLIGHTS)}"
with open(csv_file,'w') as f:
f.write(calc_content)
print(f"\n - Converting local CSV file {PLAYLIGHTS.split('/')[-1]} ...")
# convert CSV from local file
with open(csv_file, newline='') as cf:
reader = csv.DictReader(cf)
for row in reader:
if row['channel'] == '':
continue
# convert row to dict
thisLED = row2dict(row)
# append it to list of all LEDs (channels)
playlights.append(thisLED)
return playlights
playlights = update_playlights()
# backup current sequence.json
count = 1
backup = f"{pathbase}/getseq/seqbakup/sequence.{count}.json"
while os.path.exists(backup):
count += 1
backup = f"{pathbase}/getseq/seqbakup/sequence.{count}.json"
print(f"\n - Backup last sequence.json: {backup}")
subprocess.call(['mv', f"{pathbase}/sequence.json", backup])
# write local JSON file
with open(f"{pathbase}/sequence.json", 'w') as f:
json.dump(playlights, f, indent=2)
print(f" - Updated sequence: {pathbase}/sequence.json\n ___\n")