Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mevljas committed Feb 15, 2025
1 parent 9bb5274 commit 6c23a12
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 9 additions & 2 deletions services/extract_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ async def parse_result(

logger.debug("Extracting result data...")

image_url = await item.locator(
image_locator = item.locator(
'xpath=div/div[contains(@class, "property-image")]/a[2]/img'
).first.get_attribute("data-src")
)

image_url = None

if (await image_locator.count()) > 0:
image_url = await item.locator(
'xpath=div/div[contains(@class, "property-image")]/a[2]/img'
).first.get_attribute("data-src")

# Replace the url domain, so it will work on Discord.
if image_url and image_url.startswith("http"):
Expand Down
10 changes: 7 additions & 3 deletions spider/spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ async def run_spider(database_manager: DatabaseManager) -> tuple[dict, bool]:

saved_results = await database_manager.get_listings()

error = False

# For each url, send the results to a different channel.
for channel, page_url in config:
logger.debug("Processing channel %s with URL %s", channel, page_url)
Expand All @@ -48,8 +50,6 @@ async def run_spider(database_manager: DatabaseManager) -> tuple[dict, bool]:

more_pages = True

error = False

index = 1

results = {}
Expand Down Expand Up @@ -115,7 +115,11 @@ async def run_spider(database_manager: DatabaseManager) -> tuple[dict, bool]:
await browser_page.close()

await browser.close()
logger.info("Spider finished. Found %d new listings.", len(discord_listings))

# Count all listings in discord_listings.
total_listings = sum(len(listings) for listings in discord_listings.values())

logger.info("Spider finished. Found %d new listings.", total_listings)

return discord_listings, error

Expand Down

0 comments on commit 6c23a12

Please sign in to comment.