fix wgcompany fetch error
This commit is contained in:
parent
856f683ec3
commit
a628bdb9db
2 changed files with 75 additions and 4 deletions
|
|
@ -130,23 +130,28 @@ class WGCompanyNotifier:
|
|||
seen_ids.add(listing["id"])
|
||||
unique_listings.append(listing)
|
||||
await page.close()
|
||||
logger.info(f"[WGCOMPANY] Fetched {len(unique_listings)} unique listings")
|
||||
|
||||
if len(unique_listings) == 0:
|
||||
logger.warning("[WGCOMPANY] Fetched 0 listings - possible page load failure")
|
||||
else:
|
||||
logger.info(f"[WGCOMPANY] Fetched {len(unique_listings)} unique listings")
|
||||
return unique_listings
|
||||
except Exception as e:
|
||||
logger.error(f"[WGCOMPANY] Error fetching listings: {e}")
|
||||
logger.error(f"[WGCOMPANY] Error fetching listings: {e}", exc_info=True)
|
||||
return []
|
||||
|
||||
def load_previous_listings(self):
|
||||
if WGCOMPANY_LISTINGS_FILE.exists():
|
||||
with open(WGCOMPANY_LISTINGS_FILE, 'r') as f:
|
||||
data = json.load(f)
|
||||
logger.debug(f"[WG] Loaded {len(data)} previous listings")
|
||||
logger.info(f"[WGCOMPANY] Loaded {len(data)} previous listings from file")
|
||||
return data
|
||||
logger.info("[WGCOMPANY] No previous listings file found, starting fresh")
|
||||
return {}
|
||||
|
||||
def save_listings(self, listings: list[dict]) -> None:
|
||||
listings_dict = {l['id']: l for l in listings}
|
||||
logger.debug(f"[WG] Saving {len(listings_dict)} listings")
|
||||
logger.info(f"[WGCOMPANY] Saving {len(listings_dict)} listings to file")
|
||||
with open(WGCOMPANY_LISTINGS_FILE, 'w') as f:
|
||||
json.dump(listings_dict, f, indent=2, ensure_ascii=False)
|
||||
|
||||
|
|
@ -340,6 +345,18 @@ class WGCompanyNotifier:
|
|||
while True:
|
||||
listings = await self.fetch_listings()
|
||||
previous = self.load_previous_listings()
|
||||
|
||||
# Safety check: Don't overwrite state if fetch failed or returned suspiciously few results
|
||||
if len(listings) == 0:
|
||||
logger.warning("[WGCOMPANY] Fetched 0 listings - skipping save to prevent state loss")
|
||||
await asyncio.sleep(self.refresh_minutes * 60)
|
||||
continue
|
||||
elif len(previous) > 0 and len(listings) < len(previous) * 0.5:
|
||||
# If we fetched less than 50% of previous listings, something is likely wrong
|
||||
logger.warning(f"[WGCOMPANY] Fetched only {len(listings)} listings (previous: {len(previous)}) - skipping save")
|
||||
await asyncio.sleep(self.refresh_minutes * 60)
|
||||
continue
|
||||
|
||||
new_listings = self.find_new_listings(listings, previous)
|
||||
if new_listings:
|
||||
logger.info(f"[WGCOMPANY] Found {len(new_listings)} new listing(s)")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue