forked from info-beamer/36c3-cms
-
Notifications
You must be signed in to change notification settings - Fork 2
/
syncer.py
164 lines (145 loc) · 4.71 KB
/
syncer.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from datetime import datetime
from json import dumps as json_dumps
from logging import getLogger
from conf import CONFIG
from helper import Asset, State, get_all_live_assets, get_assets, user_is_admin
from ib_hosted import ib
from notifier import Notifier
FADE_TIME = 0.5
SLIDE_TIME = 10
log = getLogger("Syncer")
log.info("Starting sync")
def asset_to_tiles(asset: Asset):
log.debug("adding {} to Page".format(asset.id))
tiles = []
if asset.filetype == "video":
tiles.append(
{
"type": "rawvideo",
"asset": asset.id,
"x1": 0,
"y1": 0,
"x2": 1920,
"y2": 1080,
"config": {
"fade_time": FADE_TIME,
"layer": -5,
"looped": True,
},
}
)
else:
tiles.append(
{
"type": "image",
"asset": asset.id,
"x1": 0,
"y1": 0,
"x2": 1920,
"y2": 1080,
"config": {"fade_time": FADE_TIME},
}
)
if not user_is_admin(asset.user):
tiles.append(
{
"type": "flat",
"asset": "flat.png",
"x1": 0,
"y1": 1040,
"x2": 1920,
"y2": 1080,
"config": {"color": "#000000", "alpha": 230, "fade_time": FADE_TIME},
}
)
tiles.append(
{
"type": "markup",
"asset": "default-font.ttf",
"x1": 150,
"y1": 1048,
"x2": 1900,
"y2": 1080,
"config": {
"font_size": 25,
"fade_time": FADE_TIME,
"text": "Project by @{user} - visit {url} to share your own.".format(
user=asset.user,
url=CONFIG["DOMAIN"],
),
"color": "#dddddd",
},
}
)
if "EXTRA_ASSETS" in CONFIG:
tiles.extend(CONFIG["EXTRA_ASSETS"])
return tiles
if datetime.now().minute == int(CONFIG.get("NOTIFIER", {}).get("ALERT_MINUTE", 7)):
n = Notifier()
asset_states = {}
for asset in get_assets():
if asset.state not in (State.CONFIRMED, State.REJECTED, State.DELETED):
if asset.state not in asset_states:
asset_states[asset.state] = 0
asset_states[asset.state] += 1
log.info(
f"asset {asset.id} is in state {asset.state}, uploaded by {asset.user}"
)
msg = []
for state, count in sorted(asset_states.items()):
msg.append(f"{count} assets in state {state}.")
if msg:
n.message(" ".join(msg), level="WARN")
pages = []
assets_visible = set()
for asset in get_all_live_assets():
pages.append(
{
"auto_duration": SLIDE_TIME,
"duration": SLIDE_TIME
- (
FADE_TIME * 2
), # Because it seems like the fade time is exclusive of the 10 sec, so videos play for 11 secs.
"interaction": {"key": ""},
"layout_id": -1, # Use first layout
"overlap": 0,
"tiles": asset_to_tiles(asset),
}
)
assets_visible.add(asset.id)
log.info(
"There are currently {} pages visible with asset ids: {}".format(
len(pages), ", ".join([str(i) for i in sorted(assets_visible)])
)
)
for setup_id in CONFIG["SETUP_IDS"]:
slog = getLogger(f"Setup {setup_id}")
slog.info("Getting old config")
config = ib.get(f"setup/{setup_id}")["config"][""]
setup_changed = False
for schedule in config["schedules"]:
if schedule["name"] == "User Content":
slog.info('Found schedule "User Content"')
assets_shown = set()
for page in schedule["pages"]:
for tile in page["tiles"]:
if tile["type"] in ("image", "rawvideo"):
assets_shown.add(tile["asset"])
slog.info(
"schedule shows assets: {}".format(
", ".join([str(i) for i in sorted(assets_shown)])
)
)
if assets_visible != assets_shown:
schedule["pages"] = pages
setup_changed = True
if setup_changed:
slog.warning("Config has changed, updating")
ib.post(
f"setup/{setup_id}",
config=json_dumps({"": config}),
mode="update",
)
else:
slog.info("Config has not changed, skipping update")
log.info("updated everything")