all events after 18 are now pub events
This commit is contained in:
61
crawler.py
61
crawler.py
@ -76,40 +76,63 @@ def send_to_discord_webhook(data, webhook_url):
|
||||
except requests.exceptions.HTTPError as e:
|
||||
print(f"Discord webhook error: {e}\nPayload: {payload}")
|
||||
return None
|
||||
# Send data in batches of up to 40 events per message
|
||||
batch_size = 40
|
||||
total_batches = (len(data) + batch_size - 1) // batch_size
|
||||
for i in range(0, len(data), batch_size):
|
||||
batch = data[i:i+batch_size]
|
||||
content = f"Nationer Öppna Idag (batch {i//batch_size+1}/{total_batches}):\n"
|
||||
for item in batch:
|
||||
# Group events
|
||||
pub_keywords = ['pub', "wermlandskällar’n", 'orvars krog']
|
||||
def is_pub_event(e):
|
||||
# Keyword match
|
||||
if any(k in e['event'].lower() for k in pub_keywords):
|
||||
return True
|
||||
# Time match: look for start time after 18:00
|
||||
import re
|
||||
time_match = re.search(r'(\d{2}):(\d{2})', e.get('open_time', ''))
|
||||
if time_match:
|
||||
hour = int(time_match.group(1))
|
||||
if hour >= 18:
|
||||
return True
|
||||
return False
|
||||
pub_events = [e for e in data if is_pub_event(e)]
|
||||
other_events = [e for e in data if not is_pub_event(e)]
|
||||
# Format as plain grouped messages
|
||||
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)"
|
||||
payload = {"content": content}
|
||||
return content
|
||||
# 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(f"Sent batch {i//batch_size+1} to Discord.")
|
||||
print("Sent pub batch to Discord.")
|
||||
except requests.exceptions.HTTPError as e:
|
||||
print(f"Discord webhook error: {e}\nPayload: {payload}")
|
||||
|
||||
|
||||
def main():
|
||||
nationer_data = fetch_nationer_open_times(NATIONER_URL)
|
||||
if nationer_data:
|
||||
send_to_discord_webhook(nationer_data, DISCORD_WEBHOOK_URL)
|
||||
else:
|
||||
send_to_discord_webhook([
|
||||
{"nation": "Inga aktiviteter hittades.", "event": "", "open_time": ""}
|
||||
], DISCORD_WEBHOOK_URL)
|
||||
send_to_discord_webhook(nationer_data, DISCORD_WEBHOOK_URL)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import time
|
||||
while True:
|
||||
main()
|
||||
print("Sleeping for 24 hours...")
|
||||
time.sleep(86400)
|
||||
import datetime
|
||||
|
||||
# Run once immediately
|
||||
main()
|
||||
print("First run complete. Scheduling next runs at 00:05 daily.")
|
||||
while True:
|
||||
now = datetime.datetime.now()
|
||||
# Calculate next 00:05
|
||||
next_run = now.replace(hour=0, minute=5, second=0, microsecond=0)
|
||||
if now >= next_run:
|
||||
next_run += datetime.timedelta(days=1)
|
||||
sleep_seconds = (next_run - now).total_seconds()
|
||||
print(f"Sleeping until next run at {next_run.strftime('%Y-%m-%d %H:%M:%S')} ({int(sleep_seconds)} seconds)")
|
||||
time.sleep(sleep_seconds)
|
||||
main()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user