|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from time import sleep |
| 5 | +from typing import Optional, List |
| 6 | + |
| 7 | +import yaml |
| 8 | +from obswebsocket import requests |
| 9 | +from slugify import slugify |
| 10 | + |
| 11 | +from sleuthdeck.deck import Action, KeyScene, Key, ClickType |
| 12 | +from sleuthdeck.plugins.obs import OBS |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class Event: |
| 17 | + title: str |
| 18 | + sections: List[Section] |
| 19 | + id: Optional[str] = None |
| 20 | + |
| 21 | + @property |
| 22 | + def slug(self): |
| 23 | + return slugify(self.title) |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class Section: |
| 28 | + title: str |
| 29 | + byline: str |
| 30 | + |
| 31 | + |
| 32 | +class Presentation: |
| 33 | + def __init__(self, obs: OBS, path: str, title_scene_item="Section title", |
| 34 | + byline_scene_item="Section byline", |
| 35 | + title_scene="Title", overlay_scene="Overlay", |
| 36 | + guest_name_item="Guest 1", |
| 37 | + guest_title_item="Guest 2"): |
| 38 | + with open(path, "r") as stream: |
| 39 | + try: |
| 40 | + data = yaml.safe_load(stream) |
| 41 | + except yaml.YAMLError as exc: |
| 42 | + print(exc) |
| 43 | + self.event = Event("Missing", []) |
| 44 | + return |
| 45 | + |
| 46 | + sections = [] |
| 47 | + if "sections" in data: |
| 48 | + sections = [Section(title=s["title"], byline=s["byline"]) for s in data["sections"]] |
| 49 | + |
| 50 | + if "guest" in data: |
| 51 | + obs.obs(requests.SetTextFreetype2Properties(guest_name_item, text=data["guest"]["name"])) |
| 52 | + obs.obs(requests.SetTextFreetype2Properties(guest_title_item, text=data["guest"]["title"])) |
| 53 | + |
| 54 | + self.event = Event( |
| 55 | + title=data["title"], |
| 56 | + sections=sections, |
| 57 | + ) |
| 58 | + self.obs = obs |
| 59 | + self.current_section_idx = 0 |
| 60 | + self.title_scene_item = title_scene_item |
| 61 | + self.byline_scene_item = byline_scene_item |
| 62 | + self.title_scene = title_scene |
| 63 | + self.overlay_scene = overlay_scene |
| 64 | + self.reset() |
| 65 | + |
| 66 | + def next_section(self) -> Action: |
| 67 | + def action(scene: KeyScene, key: Key, click: ClickType): |
| 68 | + self._update_labels(self._next_section()) |
| 69 | + |
| 70 | + return action |
| 71 | + |
| 72 | + def reset(self) -> Action: |
| 73 | + def action(scene: KeyScene, key: Key, click: ClickType): |
| 74 | + self.current_section_idx = 0 |
| 75 | + self._update_labels(self.event.sections[0], new_scene=False) |
| 76 | + |
| 77 | + return action |
| 78 | + |
| 79 | + def previous_section(self) -> Action: |
| 80 | + def action(scene: KeyScene, key: Key, click: ClickType): |
| 81 | + self._update_labels(self._previous_section()) |
| 82 | + |
| 83 | + return action |
| 84 | + |
| 85 | + def _update_labels(self, section, new_scene=True): |
| 86 | + self.obs.obs(requests.SetSceneItemRender(self.title_scene_item, False, self.overlay_scene)) |
| 87 | + self.obs.obs(requests.SetSceneItemRender(self.byline_scene_item, False, self.overlay_scene)) |
| 88 | + self.obs.obs(requests.SetTextFreetype2Properties(self.title_scene_item, text=section.title)) |
| 89 | + self.obs.obs(requests.SetTextFreetype2Properties(self.byline_scene_item, text=section.byline)) |
| 90 | + if new_scene: |
| 91 | + self.obs.obs(requests.SetCurrentScene(self.title_scene)) |
| 92 | + sleep(.3) |
| 93 | + self.obs.obs(requests.SetSceneItemRender(self.title_scene_item, True, self.overlay_scene)) |
| 94 | + self.obs.obs(requests.SetSceneItemRender(self.byline_scene_item, True, self.overlay_scene)) |
| 95 | + |
| 96 | + def _next_section(self) -> Section: |
| 97 | + if len(self.event.sections) == self.current_section_idx: |
| 98 | + self.current_section_idx = 0 |
| 99 | + else: |
| 100 | + self.current_section_idx += 1 |
| 101 | + |
| 102 | + return self.event.sections[self.current_section_idx] |
| 103 | + |
| 104 | + def _previous_section(self) -> Section: |
| 105 | + if self.current_section_idx == 0: |
| 106 | + self.current_section_idx = len(self.event.sections) - 1 |
| 107 | + else: |
| 108 | + self.current_section_idx -= 1 |
| 109 | + |
| 110 | + return self.event.sections[self.current_section_idx] |
0 commit comments