wat223
This commit is contained in:
48
crawler.py
48
crawler.py
@ -67,21 +67,33 @@ def fetch_nationer_open_times(url):
|
||||
def send_to_discord_webhook(data, webhook_url):
|
||||
if not webhook_url:
|
||||
raise ValueError("DISCORD_WEBHOOK_URL not set")
|
||||
content = "Nationer Öppna Idag:\n"
|
||||
for item in data:
|
||||
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()
|
||||
return response.status_code
|
||||
except requests.exceptions.HTTPError as e:
|
||||
print(f"Discord webhook error: {e}\nPayload: {payload}")
|
||||
return None
|
||||
if not data:
|
||||
payload = {"content": "Inga öppettider hittades."}
|
||||
try:
|
||||
response = requests.post(webhook_url, json=payload)
|
||||
response.raise_for_status()
|
||||
return response.status_code
|
||||
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:
|
||||
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():
|
||||
@ -95,5 +107,9 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
import time
|
||||
while True:
|
||||
main()
|
||||
print("Sleeping for 24 hours...")
|
||||
time.sleep(86400)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user