This commit is contained in:
2025-11-12 01:34:56 +01:00
parent 08262a80dc
commit d356f78efa

View File

@ -67,21 +67,33 @@ def fetch_nationer_open_times(url):
def send_to_discord_webhook(data, webhook_url): def send_to_discord_webhook(data, webhook_url):
if not webhook_url: if not webhook_url:
raise ValueError("DISCORD_WEBHOOK_URL not set") raise ValueError("DISCORD_WEBHOOK_URL not set")
content = "Nationer Öppna Idag:\n" if not data:
for item in data: payload = {"content": "Inga öppettider hittades."}
content += f"**{item['nation']}**\nEvent: {item['event']}\nTid: {item['open_time']}\n---\n" try:
# Discord message limit is 2000 characters response = requests.post(webhook_url, json=payload)
if len(content) > 1900: response.raise_for_status()
content = content[:1900] + "... (truncated)" return response.status_code
payload = {"content": content} except requests.exceptions.HTTPError as e:
try: print(f"Discord webhook error: {e}\nPayload: {payload}")
return None
response = requests.post(webhook_url, json=payload) # Send data in batches of up to 40 events per message
response.raise_for_status() batch_size = 40
return response.status_code total_batches = (len(data) + batch_size - 1) // batch_size
except requests.exceptions.HTTPError as e: for i in range(0, len(data), batch_size):
print(f"Discord webhook error: {e}\nPayload: {payload}") batch = data[i:i+batch_size]
return None content = f"Nationer Öppna Idag (batch {i//batch_size+1}/{total_batches}):\n"
for item in batch:
content += f"**{item['nation']}**\nEvent: {item['event']}\nTid: {item['open_time']}\n---\n"
# Discord message limit is 2000 characters
if len(content) > 1900:
content = content[:1900] + "... (truncated)"
payload = {"content": content}
try:
response = requests.post(webhook_url, json=payload)
response.raise_for_status()
print(f"Sent batch {i//batch_size+1} to Discord.")
except requests.exceptions.HTTPError as e:
print(f"Discord webhook error: {e}\nPayload: {payload}")
def main(): def main():
@ -95,5 +107,9 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
main() import time
while True:
main()
print("Sleeping for 24 hours...")
time.sleep(86400)