adding embed and map pictures to map notifications

This commit is contained in:
jenz 2026-02-25 17:05:27 +01:00
parent 8a5b46565f
commit fa3405d51b

View File

@ -10,18 +10,21 @@ from flask_cors import CORS
from werkzeug.middleware.proxy_fix import ProxyFix
import threading
import time
import difflib
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1)
CORS(app)
map_images = []
#runs every 3 minutes to inform about a new map
def insert_maps(fixed):
def insert_maps(fixed_param):
with get_connection_unloze_playtime() as conn:
with conn.cursor() as cur:
placeholders = ','.join(['(%s)'] * len(fixed))
flattened_values = [row for row in fixed]
placeholders = ','.join(['(%s)'] * len(fixed_param))
flattened_values = [row for row in fixed_param]
sql_statement = f"""
INSERT IGNORE INTO unloze_playtimestats.map_notifications
(mapname)
@ -126,7 +129,7 @@ def stoat_send_valid_map_notifications(thing, valid_map_notification_cmds):
}
response = requests.post(stoat_url_map_notifications, headers=headers, json=data)
def check_new_map_notification_status(fixed, last_msg_id):
def check_new_map_notification_status(fixed_param, last_msg_id):
headers = {
"x-bot-token": stoat_token
}
@ -142,12 +145,18 @@ def check_new_map_notification_status(fixed, last_msg_id):
else:
response = requests.get(stoat_url_50_last_messages + last_msg_id, headers=headers)
data = response.json()
try:
data = response.json()
except:
#well that should not happen
err = traceback.format_exc()
print("err: ", err)
return None, None, None, None, None
for index, msg in enumerate(data['messages']):
#print(msg)
msg_id = msg['_id']
author = msg['author']
content = msg['content']
last_msg_id = msg_id
dont_process_bot_msg = False
for user in data['users']:
@ -157,16 +166,17 @@ def check_new_map_notification_status(fixed, last_msg_id):
if dont_process_bot_msg: continue
try:
content = msg['content']
if content.startswith("add "):
content = content.split("add ")[1]
if content in fixed:
if content in fixed_param:
valid_map_notification_cmds_add.append([content, author])
else:
invalid_map_notification_cmds.append([content, author])
elif content.startswith("remove "):
content = content.split("remove ")[1]
if content in fixed:
if content in fixed_param:
valid_map_notification_cmds_remove.append([content, author])
else:
invalid_map_notification_cmds.append([content, author])
@ -177,6 +187,7 @@ def check_new_map_notification_status(fixed, last_msg_id):
except:
err = traceback.format_exc()
print("err: ", err)
print("msg in error: ", msg)
#print('valid_map_notification_cmds_add: ', valid_map_notification_cmds_add)
#print('valid_map_notification_cmds_remove: ', valid_map_notification_cmds_remove)
@ -208,10 +219,17 @@ def send_post_notify_message_to_stoat(list_of_people_to_notify, content, infomap
"x-bot-token": f"{stoat_token}",
"Content-Type": "application/json"
}
match = difflib.get_close_matches(infomap, map_images, n=1, cutoff=0.6)
thumbnail_name = match[0] if match else "ze_atix_panic"
image_url = f"https://vauff.com/mapimgs/240/{thumbnail_name}.jpg"
data = {
"content": message
"content": image_url,
"embeds": [{
"description": message,
}]
}
#print('messages: ', message)
response = requests.post(stoat_url_map_notifications, headers=headers, json=data)
def update_last_msg_id(msg_id):
@ -244,7 +262,6 @@ def got_new_map_to_announce():
try:
content = request.get_json()["content"]
#print('content: ', content)
infomap = content.split("Map changed to: ")[1].split("\nPlayerCount:")[0]
list_of_people_to_notify = notify_of_new_map(infomap)
send_post_notify_message_to_stoat(list_of_people_to_notify, content, infomap)
@ -263,13 +280,18 @@ def main_loop():
bz2 = bz2.split('.bsp')[0]
fixed.append(bz2)
r_map_images = requests.get("https://vauff.com/mapimgs/list.php")
d = r_map_images.json().get("240", [])
for map_img in d:
map_images.append(map_img)
#insert maps if they are not already.
insert_maps(fixed)
#check last 5 messages for new map notification updates
while True:
last_msg_id = get_last_msg_id_read()
valid_map_notification_cmds_add, valid_map_notification_cmds_remove, invalid_map_notification_cmds, last_msg_id, show_map_notifications = check_new_map_notification_status(fixed, last_msg_id)
valid_map_notification_cmds_add, valid_map_notification_cmds_remove, invalid_map_notification_cmds, last_msg_id, show_map_notifications = check_new_map_notification_status(fixed, last_msg_id)
if valid_map_notification_cmds_add:
stoat_send_valid_map_notifications("added", valid_map_notification_cmds_add)
@ -280,7 +302,8 @@ def main_loop():
if show_map_notifications:
stoat_send_map_notification_list(show_map_notifications)
update_last_msg_id(last_msg_id)
if last_msg_id:
update_last_msg_id(last_msg_id)
time.sleep(5)
if __name__ == '__main__':