Skip to content

Commit

Permalink
Captions
Browse files Browse the repository at this point in the history
  • Loading branch information
KenwoodFox committed Sep 16, 2023
1 parent fb150a1 commit 1e8399e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 23 deletions.
7 changes: 5 additions & 2 deletions admin_bot/admin_bot/cogs/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ async def clearPromo(self, ctx: discord.Interaction):

@app_commands.command(name="submit_promo")
@commands.has_any_role("Leads", "Adult Mentor", "Student Mentor")
async def submitPromo(self, ctx: discord.Interaction, img: str, days: int):
async def submitPromo(
self, ctx: discord.Interaction, img: str, days: int, caption: str = None
):
"""
Submits a promotional image to be displayed in rotation.
"""
Expand All @@ -73,6 +75,7 @@ async def submitPromo(self, ctx: discord.Interaction, img: str, days: int):
imgData = {
"expires": f"{int(time.time()) + (86400*days)}",
"author": f"Submitted by {ctx.user.nick}",
"caption": caption,
}

json_object = json.dumps(imgData, indent=4)
Expand All @@ -83,7 +86,7 @@ async def submitPromo(self, ctx: discord.Interaction, img: str, days: int):
subprocess.run(["wget", img, "-O", f"{filename}.png"])

await ctx.followup.send(
f"Done! Added {filename}, expires in {days} days.",
f"Done! Added `{filename}`, expires in `{days}` days! Caption was `{caption}`.",
file=discord.File(f"{filename}.png"),
)
except Exception as e:
Expand Down
5 changes: 4 additions & 1 deletion admin_interface/admin_interface/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,16 @@ def webhook():
# Push a websocket update! Do this a lot.
def websocket_push():
while True:
# Get promotional image data
_promoData = getNextImage()
data = {
"version": getVersion(),
"bot_version": bot_version,
"date": get_current_datetime(),
"discord": discord_logs,
"next_meeting": getNextEvent(),
"promo_path": getNextImage(),
"promo_path": _promoData[0], # File path
"promo_caption": _promoData[1], # Caption
}

socketio.emit("updateSensorData", data)
Expand Down
18 changes: 18 additions & 0 deletions admin_interface/admin_interface/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ main {
align-items: stretch;
}

.caption {

This comment has been minimized.

Copy link
@KenwoodFox

KenwoodFox Sep 16, 2023

Author Member

@dublUayaychtee this is literally so messy pls halp

position: absolute;
top: 83%;
left: 23%;
/* transform: translate(-250%, 260%); */
background-color: #1d1d1d;
color: #fff;
padding: 10px 20px;
font-size: 30px;
text-align: center;
}

.image-container img {
display: block;
width: 100%;
height: auto;
}

.timerText {
/* This is used for the timer big text */
padding: 0;
Expand Down
3 changes: 3 additions & 0 deletions admin_interface/admin_interface/static/js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ $(document).ready(function () {
$("#botVersion").text(msg.bot_version);
$("#intVersion").text(msg.version);
$("#connected").text("👍");

$("#caption").text(msg.promo_caption);

if (msg.promo_path != null) {
$("#promoImage").attr("src", msg.promo_path);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<div id="promo" class="promo">
<img src="static/placeholder/320x240.png" alt="promoImage" , id="promoImage">
<div id="caption" class="caption">Your text here</div>
</div>
2 changes: 0 additions & 2 deletions admin_interface/admin_interface/tools/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ def getNextEvent():
hours = int(duration.seconds / (60 * 60))
minutes = int((duration.seconds - int(hours * (60 * 60))) / 60)

logging.debug(f"Found {event['SUMMARY']} only {days} away.")

# Make sure we're adding the closest meeting.
if days < _days and days >= 0:
_ret = f"Next meeting in {days} days, {hours} hours, {minutes} minutes."
Expand Down
44 changes: 26 additions & 18 deletions admin_interface/admin_interface/tools/promo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,29 @@
promo_path = "static/promo/"
current_index = 0
valid_promos = []
last_run = 0
time_per_image = 25 # Seconds

placeholderImg = "static/placeholder/default.png"


def getNextImage(prune=True):
def getNextImage(prune=True) -> tuple:
global current_index
global valid_promos
global last_run
global placeholderImg

try:
files = [f for f in listdir(promo_path) if isfile(join(promo_path, f))]
except FileNotFoundError:
logging.debug("Not running in prod environment or docker volume missing.")
return placeholderImg
return placeholderImg, None

global current_index
global valid_promos
_now = int(time.time())

if int(time.time()) % time_per_image == 0:
valid_promos = []
if last_run + time_per_image < _now:
last_run = _now
valid_promos = [] # Clear valid promo's

for file in files:
if ".png" in file:
Expand All @@ -36,28 +42,30 @@ def getNextImage(prune=True):
fdata = json.load(open(join(promo_path, f"{fname}.json")))
logging.debug(f"Loaded file with metadata {fdata}")

if int(fdata["expires"]) < int(time.time()):
if int(fdata["expires"]) < int(time.time() and prune):
logging.info("Pruning image file with expired date")
remove(join(promo_path, file))
remove(join(promo_path, f"{fname}.json"))
else:
# Its valid!
logging.debug(f"Added {file} to valid promo's list")
valid_promos.append(file)

_caption = fdata["caption"]
valid_promos.append([file, _caption])
else:
logging.info("Pruning image file with no matching json")
logging.warn("Pruning image file with no matching json")
remove(join(promo_path, file))

current_index += 1

if len(valid_promos) > 0:
if len(valid_promos) - 1 < current_index:
current_index = 0
if len(valid_promos) - 1 < current_index:
current_index = 0

if len(valid_promos) == 0:
logging.debug("Nothing in rotation, showing default")
return placeholderImg, None

promo = f"static/promo/{valid_promos[current_index]}"
logging.debug(f"Sending: {promo}")
logging.debug("No need to update image yet.")
_promo_path = f"static/promo/{valid_promos[current_index][0]}"

return promo
else:
logging.debug("Nothing in rotation, showing default")
return placeholderImg
return _promo_path, valid_promos[current_index][1]

0 comments on commit 1e8399e

Please sign in to comment.