-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (62 loc) · 2.63 KB
/
main.py
File metadata and controls
77 lines (62 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
from models import Schedule, Config, System
from tracker import PlayedTracker, QueuedTracker
from planner import QueuePlanner
from player import PlaylistManager
from utils import *
from datetime import datetime
from utils import setup_logging
from webui import *
DURATIONS_JSON = "durations.json"
DURATIONS_SCRIPT = "durationanalyzer.py"
# Pick the config file by OS
CONFIG_FILE_NAME = "config_pi.json" if os.name != "nt" else "config_nt.json"
def main():
# Load config
if not os.path.exists(CONFIG_FILE_NAME):
print(f"{CONFIG_FILE_NAME} does not exist!")
return
with open(CONFIG_FILE_NAME, "r") as f:
raw = json.load(f)
# Build objects and setup logging
schedules = {name: Schedule.from_dict(data) for name, data in raw["schedules"].items()}
system = System.from_dict(raw["system"])
config = Config(schedules=schedules, system=system)
setup_logging(config.system) # enable logging as per flag in system part of config
logging.debug("Initialization complete")
# spin off background thread that restarts script at specified time
start_restart_thread(system)
# Start Flask in a background thread
flask_thread = threading.Thread(target=run_flask, daemon=True)
flask_thread.start()
# Call method to ensure durations for all files have been calculated, if not they will be re-calculated
ensure_durations_have_been_calculated(schedules)
# Now onto the main work - read durations json but this time not just by_path, json will always exist, we made sure in above method
with open(DURATIONS_JSON, "r", encoding="utf-8") as f:
durations_json = json.load(f)
# construct objects
tracker = PlayedTracker() # Track played items
queued_tracker = QueuedTracker(config) # Track queued items
planner = QueuePlanner(config, tracker, queued_tracker, durations_json, system) # plans the queue of shows/ads/bumpers
# build the playlist
plan = planner.build_playlist_until_restart(datetime.now())
if not plan:
print("[INFO] Nothing fits before restart. Exiting.")
return
# Create VLC manager, add planned items with categories
manager = PlaylistManager(config, tracker)
for file_path, category in plan:
manager.add_to_playlist(file_path, category)
# Start playback & go fullscreen
manager.start_playback()
time.sleep(1)
manager.set_fullscreen(True)
# Keep alive so VLC events fire
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
manager.stop_playback()
manager.set_fullscreen(False)
if __name__ == "__main__":
main()