diff --git a/crawler.py b/crawler.py index 8eaebca..e4eec4b 100644 --- a/crawler.py +++ b/crawler.py @@ -96,21 +96,40 @@ def send_to_discord_webhook(data, webhook_url): def format_batch(events, title): content = f"**{title}**\n" for item in events: - 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)" + content += ( + f"> **{item['nation']}**\n" + f"> **Event:** {item['event']}\n" + f"> **Tid:** {item['open_time']}\n" + f"> ————————————————\n" + ) return content + + def send_in_batches(content, webhook_url): + max_len = 2000 + lines = content.split('\n') + batch = '' + for line in lines: + if len(batch) + len(line) + 1 > max_len: + payload = {"content": batch} + try: + response = requests.post(webhook_url, json=payload) + response.raise_for_status() + except requests.exceptions.HTTPError as e: + print(f"Discord webhook error: {e}\nPayload: {payload}") + batch = '' + batch += line + '\n' + if batch.strip(): + payload = {"content": batch} + try: + response = requests.post(webhook_url, json=payload) + response.raise_for_status() + except requests.exceptions.HTTPError as e: + print(f"Discord webhook error: {e}\nPayload: {payload}") # Do not send non-pub events to Discord # Send pub events second, as normal message if pub_events: - payload = {"content": format_batch(pub_events, "Pub-aktiviteter")} - try: - response = requests.post(webhook_url, json=payload) - response.raise_for_status() - print("Sent pub batch to Discord.") - except requests.exceptions.HTTPError as e: - print(f"Discord webhook error: {e}\nPayload: {payload}") + content = format_batch(pub_events, "Pub-aktiviteter") + send_in_batches(content, webhook_url) def main():