add scraping functionality for Lipsia and improve error handling for image downloads

This commit is contained in:
Elmar Kresse
2025-01-13 11:57:20 +01:00
parent 1093f49438
commit b2ef4eb5f2
13 changed files with 58 additions and 5 deletions

View File

@@ -33,6 +33,13 @@ def send_to_discord(property_data):
# Download the image
image_response = scrape_image.scrape_image(property_data["image_url"])
# Check if the image was downloaded successfully
if image_response == b"":
print("Fehler beim Herunterladen des Bildes: Leere Antwort")
payload = {"content": message}
response = requests.post(WEBHOOK_URL, data=json.dumps(payload), headers=headers)
return
# Send the message with an image attachment
files = {"file": ("image.jpg", image_response)}
payload = {"content": message}

Binary file not shown.

38
src/lipsia/lipsia.py Normal file
View File

@@ -0,0 +1,38 @@
import requests
from datetime import datetime
def scrape_lipsia():
url = "https://wg-lipsia.de/wp-admin/admin-ajax.php?action=emk_immosearch_api&endpoint=list"
response = requests.get(url)
if response.status_code != 200:
print(f"Failed to fetch data: {response.status_code}")
return []
data = response.json()
if data["status"] != "success":
print("Failed to fetch properties: Invalid response status")
return []
properties = []
for item in data["list"]:
lat = item.get("lat", "")
lon = item.get("lon", "")
google_maps_link = f"https://www.google.com/maps/search/?api=1&query={lat},{lon}"
properties.append({
"id": item.get("id"),
"title": "Lipsia - " + item.get("headline", ""),
"subtitle": item.get("adresse_strasse", "") + " " + item.get("adresse_plz_ort", ""),
"rooms": item.get("zimmer_anzahl", 0),
"size": str(item.get("wohnflaeche", 0)) + "",
"rent": "Kalt: " + str(item.get("miete_kalt_euro", 0)) + "",
"link": google_maps_link,
"abstract": item.get("highlight_1", ""),
"warm_rent": "", # Placeholder as warm rent is not provided
"availability": (item.get("highlight_3", "")), # Customize as needed
"image_url": item.get("image", ""),
})
return properties

View File

@@ -27,7 +27,8 @@ def scrape_image(url):
if response.status_code != 200:
print(f"Fehler beim Abrufen von Easysquare: {response.status_code}")
return []
# return empty image
return b''
# get image from response

View File

@@ -101,7 +101,7 @@ def scrape_easysquare():
properties.append({
"id": id,
"title": prop_title,
"title": "LWB - " + prop_title,
"subtitle": subtitle,
"rooms": format.format_room(rooms),
"size": format.format_roomSize(size),

View File

@@ -35,7 +35,7 @@ def scrape_wogetra():
# Add property to list
properties.append({
"id": property_id,
"title": title,
"title": "Wogetra - "+ title,
"subtitle": subtitle,
"rooms": rooms,
"size": size,