mirror of
https://gitlab.dit.htwk-leipzig.de/fsr-im/tools/flatscraper.git
synced 2025-07-26 00:09:14 +02:00
add initial scraping functionality and related utilities
This commit is contained in:
55
src/discord/webhook.py
Normal file
55
src/discord/webhook.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import json
|
||||
import requests
|
||||
|
||||
import src.lwb.scrape_image as scrape_image
|
||||
|
||||
# Webhook URL from Discord
|
||||
WEBHOOK_URL = "https://discord.com/api/webhooks/1327600813367099462/goqeWDyYwi13-6F0yopUzFkHVaZs01bCe-2SI8bPJLj3WNMhxLOlIYBRIGyTpSzGCSru"
|
||||
|
||||
|
||||
# Funktion: Nachricht an Discord senden
|
||||
def send_to_discord(property_data):
|
||||
|
||||
message = (
|
||||
f"**{property_data['title']}**\n"
|
||||
f"{property_data['subtitle']}\n"
|
||||
f"**Zimmer:** {property_data['rooms']}\n"
|
||||
f"**Wohnfläche:** {property_data['size']}\n"
|
||||
f"**Gesamtmiete:** {property_data['rent']}\n"
|
||||
f"**Warmmiete:** {property_data['warm_rent']}\n"
|
||||
f"**Verfügbar ab:** {property_data['availability']}\n"
|
||||
f"**Link:** {property_data['link']}\n"
|
||||
f"**Beschreibung:** {property_data['abstract']}"
|
||||
)
|
||||
|
||||
|
||||
# Set headers
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
# Check for optional image URL
|
||||
if "image_url" in property_data and property_data["image_url"]:
|
||||
try:
|
||||
|
||||
# Download the image
|
||||
image_response = scrape_image.scrape_image(property_data["image_url"])
|
||||
|
||||
# Send the message with an image attachment
|
||||
files = {"file": ("image.jpg", image_response)}
|
||||
payload = {"content": message}
|
||||
response = requests.post(WEBHOOK_URL, data=payload, files=files)
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Fehler beim Herunterladen des Bildes: {e}")
|
||||
payload = {"content": message}
|
||||
response = requests.post(WEBHOOK_URL, data=json.dumps(payload), headers=headers)
|
||||
else:
|
||||
# Send the message without an image
|
||||
payload = {"content": message}
|
||||
response = requests.post(WEBHOOK_URL, data=json.dumps(payload), headers=headers)
|
||||
|
||||
# Check if the message was sent successfully when the status code is >= 200 and < 300
|
||||
if response.status_code >= 200 and response.status_code < 300:
|
||||
print(f"Benachrichtigung gesendet: {property_data['title']} - response: {response.status_code}")
|
||||
else:
|
||||
print(f"Fehler beim Senden der Benachrichtigung: {response.status_code}")
|
||||
|
Reference in New Issue
Block a user