Skip to content

Commit

Permalink
Using config.name instead of their keys
Browse files Browse the repository at this point in the history
  • Loading branch information
BoPeng committed Jan 31, 2025
1 parent 985b64d commit dc3df67
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
16 changes: 8 additions & 8 deletions src/ai_marketplace_monitor/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,24 @@ def validate_users(self: "Config") -> None:
# check for required fields in each user

# if user is specified in other section, they must exist
for marketplace_name, marketplace_config in self.marketplace.items():
for marketplace_config in self.marketplace.values():
for user in marketplace_config.notify or []:
if user not in self.user:
raise ValueError(
f"User [magenta]{user}[/magenta] specified in [magenta]{marketplace_name}[/magenta] does not exist."
f"User [magenta]{user}[/magenta] specified in [magenta]{marketplace_config.name}[/magenta] does not exist."
)

# if user is specified for any search item, they must exist
for item_name, item_config in self.item.items():
for item_config in self.item.values():
for user in item_config.notify or []:
if user not in self.user:
raise ValueError(
f"User [magenta]{user}[/magenta] specified in [magenta]{item_name}[/magenta] does not exist."
f"User [magenta]{user}[/magenta] specified in [magenta]{item_config.name}[/magenta] does not exist."
)

def expand_regions(self: "Config") -> None:
# if region is specified in other section, they must exist
for marketplace_name, marketplace_config in self.marketplace.items():
for marketplace_config in self.marketplace.values():
if marketplace_config.search_region is None:
continue

Expand All @@ -161,7 +161,7 @@ def expand_regions(self: "Config") -> None:
region_config: RegionConfig = self.region[region]
if region not in self.region:
raise ValueError(
f"Region [magenta]{region}[/magenta] specified in [magenta]{marketplace_name}[/magenta] does not exist."
f"Region [magenta]{region}[/magenta] specified in [magenta]{marketplace_config.name}[/magenta] does not exist."
)
# if region is specified, expand it into search_city
marketplace_config.search_city.extend(region_config.search_city)
Expand All @@ -172,7 +172,7 @@ def expand_regions(self: "Config") -> None:
marketplace_config.search_city.extend(list(set(marketplace_config.search_city)))

# if region is specified in any of the items, do the same
for item_name, item_config in self.item.items():
for item_config in self.item.values():
# expand region into item_config's search_city
if item_config.search_region is None:
continue
Expand All @@ -182,7 +182,7 @@ def expand_regions(self: "Config") -> None:
region_config = self.region[region]
if region not in self.region:
raise ValueError(
f"Region [magenta]{region}[/magenta] specified in [magenta]{item_name}[/magenta] does not exist."
f"Region [magenta]{region}[/magenta] specified in [magenta]{item_config.name}[/magenta] does not exist."
)
# if region is specified, expand it into search_city
item_config.search_city.extend(region_config.search_city)
Expand Down
67 changes: 36 additions & 31 deletions src/ai_marketplace_monitor/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,28 @@ def load_config_file(self: "MarketplaceMonitor") -> Config:
def load_ai_agents(self: "MarketplaceMonitor") -> None:
"""Load the AI agent."""
assert self.config is not None
for ai_name, ai_config in (self.config.ai or {}).items():
ai_class = supported_ai_backends[ai_name]
for ai_config in (self.config.ai or {}).values():
ai_class = supported_ai_backends[ai_config.name]
try:
self.ai_agents.append(ai_class(config=ai_config, logger=self.logger))
self.ai_agents[-1].connect()
self.logger.info(f"Connected to {ai_name}")
self.logger.info(f"Connected to {ai_config.name}")
# if one works, do not try to load another one
break
except Exception as e:
self.logger.error(f"Error connecting to {ai_name}: {e}")
self.logger.error(f"Error connecting to {ai_config.name}: {e}")
continue

def search_item(
self: "MarketplaceMonitor",
marketplace_name: str,
marketplace_config: TMarketplaceConfig,
marketplace: Marketplace,
item_name: str,
item_config: TItemConfig,
) -> None:
"""Search for an item on the marketplace."""
self.logger.info(f"Searching {marketplace_name} for [magenta]{item_name}[/magenta]")
self.logger.info(
f"Searching {marketplace_config.name} for [magenta]{item_config.name}[/magenta]"
)
new_items = []
# users to notify is determined from item, then marketplace, then all users
assert self.config is not None
Expand All @@ -115,12 +115,12 @@ def search_item(
)
continue
# for x in self.find_new_items(found_items)
if not self.confirmed_by_ai(item, item_name=item_name, item_config=item_config):
if not self.confirmed_by_ai(item, item_name=item_config.name, item_config=item_config):
continue
new_items.append(item)

self.logger.info(
f"""[magenta]{len(new_items)}[/magenta] new listing{"" if len(new_items) == 1 else "s"} for {item_name} {"is" if len(new_items) == 1 else "are"} found."""
f"""[magenta]{len(new_items)}[/magenta] new listing{"" if len(new_items) == 1 else "s"} for {item_config.name} {"is" if len(new_items) == 1 else "are"} found."""
)
if new_items:
self.notify_users(users_to_notify, new_items)
Expand All @@ -140,19 +140,22 @@ def schedule_jobs(self: "MarketplaceMonitor") -> None:
self.load_ai_agents()

assert self.config is not None
for marketplace_name, marketplace_config in self.config.marketplace.items():
marketplace_class = supported_marketplaces[marketplace_name]
if marketplace_name in self.active_marketplaces:
marketplace = self.active_marketplaces[marketplace_name]
for marketplace_config in self.config.marketplace.values():
marketplace_class = supported_marketplaces[marketplace_config.name]
if marketplace_config.name in self.active_marketplaces:
marketplace = self.active_marketplaces[marketplace_config.name]
else:
marketplace = marketplace_class(marketplace_name, browser, self.logger)
self.active_marketplaces[marketplace_name] = marketplace
marketplace = marketplace_class(marketplace_config.name, browser, self.logger)
self.active_marketplaces[marketplace_config.name] = marketplace

# Configure might have been changed
marketplace.configure(marketplace_config)

for item_name, item_config in self.config.item.items():
if item_config.marketplace is None or item_config.marketplace == marketplace_name:
for item_config in self.config.item.values():
if (
item_config.marketplace is None
or item_config.marketplace == marketplace_config.name
):
if not (item_config.enabled or True):
continue
# wait for some time before next search
Expand All @@ -169,14 +172,12 @@ def schedule_jobs(self: "MarketplaceMonitor") -> None:
search_interval,
)
self.logger.info(
f"Scheduling to search for {item_name} every {search_interval} {'' if search_interval == max_search_interval else f'to {max_search_interval}'} minutes"
f"Scheduling to search for {item_config.name} every {search_interval} {'' if search_interval == max_search_interval else f'to {max_search_interval}'} minutes"
)
schedule.every(search_interval).to(max_search_interval).minutes.do(
self.search_item,
marketplace_name,
marketplace_config,
marketplace,
item_name,
item_config,
)

Expand Down Expand Up @@ -255,13 +256,13 @@ def check_items(
assert self.config is not None

# which marketplace to check it?
for marketplace_name, marketplace_config in self.config.marketplace.items():
marketplace_class = supported_marketplaces[marketplace_name]
if marketplace_name in self.active_marketplaces:
marketplace = self.active_marketplaces[marketplace_name]
for marketplace_config in self.config.marketplace.values():
marketplace_class = supported_marketplaces[marketplace_config.name]
if marketplace_config.name in self.active_marketplaces:
marketplace = self.active_marketplaces[marketplace_config.name]
else:
marketplace = marketplace_class(marketplace_name, None, self.logger)
self.active_marketplaces[marketplace_name] = marketplace
marketplace = marketplace_class(marketplace_config.name, None, self.logger)
self.active_marketplaces[marketplace_config.name] = marketplace

# Configure might have been changed
marketplace.configure(marketplace_config)
Expand All @@ -281,16 +282,20 @@ def check_items(

self.logger.info(f"Details of the item is found: {pretty_repr(listing)}")

for item_name, item_config in self.config.item.items():
if for_item is not None and item_name != for_item:
for item_config in self.config.item.values():
if for_item is not None and item_config.name != for_item:
continue
self.logger.info(
f"Checking {post_url} for item {item_name} with configuration {pretty_repr(item_config)}"
f"Checking {post_url} for item {item_config.name} with configuration {pretty_repr(item_config)}"
)
marketplace.filter_item(listing, item_config)
self.confirmed_by_ai(listing, item_name=item_name, item_config=item_config)
self.confirmed_by_ai(
listing, item_name=item_config.name, item_config=item_config
)
if ("notify_user", listing["id"]) in cache:
self.logger.info(f"Already sent notification for item {item_name}.")
self.logger.info(
f"Already sent notification for item {item_config.name}."
)

def confirmed_by_ai(
self: "MarketplaceMonitor", item: SearchedItem, item_name: str, item_config: TItemConfig
Expand Down

0 comments on commit dc3df67

Please sign in to comment.