wat223
This commit is contained in:
32
crawler.py
32
crawler.py
@ -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"
|
|
||||||
# Discord message limit is 2000 characters
|
|
||||||
if len(content) > 1900:
|
|
||||||
content = content[:1900] + "... (truncated)"
|
|
||||||
payload = {"content": content}
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
response = requests.post(webhook_url, json=payload)
|
response = requests.post(webhook_url, json=payload)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.status_code
|
return response.status_code
|
||||||
except requests.exceptions.HTTPError as e:
|
except requests.exceptions.HTTPError as e:
|
||||||
print(f"Discord webhook error: {e}\nPayload: {payload}")
|
print(f"Discord webhook error: {e}\nPayload: {payload}")
|
||||||
return None
|
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():
|
def main():
|
||||||
@ -95,5 +107,9 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import time
|
||||||
|
while True:
|
||||||
main()
|
main()
|
||||||
|
print("Sleeping for 24 hours...")
|
||||||
|
time.sleep(86400)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user