Skip to content

Commit

Permalink
FIX selection of shipping options
Browse files Browse the repository at this point in the history
Depending on the input, one shipping size and all their options are preselected. If we want to keep that shipping size, we have to unselect the unwanted options instead of selecting the wanted
  • Loading branch information
provinzio committed May 30, 2024
1 parent ba73ebb commit 9deeca7
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions src/kleinanzeigen_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,38 +686,58 @@ async def __set_category(self, ad_file:str, ad_cfg: dict[str, Any]) -> None:
LOG.debug("Successfully set attribute field [%s] to [%s]...", special_attribute_key, special_attribute_value)

async def __set_shipping_options(self, ad_cfg: dict[str, Any]) -> None:
shipping_options_mapping = {
"DHL_2": ("Klein", "Paket 2 kg"),
"Hermes_Päckchen": ("Klein", "Päckchen"),
"Hermes_S": ("Klein", "S-Paket"),
"DHL_5": ("Mittel", "Paket 5 kg"),
"Hermes_M": ("Mittel", "M-Paket"),
"DHL_10": ("Mittel", "Paket 10 kg"),
"DHL_31,5": ("Groß", "Paket 31,5 kg"),
"Hermes_L": ("Groß", "L-Paket"),
}
try:
shipping_option_mapping = {
"DHL_2": ("Klein", "Paket 2 kg"),
"Hermes_Päckchen": ("Klein", "Päckchen"),
"Hermes_S": ("Klein", "S-Paket"),
"DHL_5": ("Mittel", "Paket 5 kg"),
"Hermes_M": ("Mittel", "M-Paket"),
"DHL_10": ("Mittel", "Paket 10 kg"),
"DHL_31,5": ("Groß", "Paket 31,5 kg"),
"Hermes_L": ("Groß", "L-Paket"),
}
try:
mapped_shipping_options = [shipping_option_mapping[option] for option in ad_cfg["shipping_options"]]
shipping_sizes, shipping_packages = zip(*mapped_shipping_options)
except KeyError as ex:
raise KeyError(f"Unknown shipping option(s), please refer to the documentation/README: {ad_cfg['shipping_options']}") from ex
mapped_shipping_options = [
shipping_options_mapping[option]
for option in set(ad_cfg["shipping_options"])
]
except KeyError as ex:
raise KeyError(f"Unknown shipping option(s), please refer to the documentation/README: {ad_cfg['shipping_options']}") from ex

unique_shipping_sizes = set(shipping_sizes)
if len(unique_shipping_sizes) > 1:
raise ValueError("You can only specify shipping options for one package size!")
shipping_sizes, shipping_packages = zip(*mapped_shipping_options)

shipping_size, = unique_shipping_sizes
await self.web_click(By.CSS_SELECTOR, f'.SingleSelectionItem--Main input[type=radio][data-testid="{shipping_size}"]')
try:
shipping_size, = set(shipping_sizes)
except ValueError as ex:
raise ValueError("You can only specify shipping options for one package size!") from ex

for shipping_package in shipping_packages:
try:
shipping_size_radio = await self.web_find(By.CSS_SELECTOR, f'.SingleSelectionItem--Main input[type=radio][data-testid="{shipping_size}"]')
assert isinstance(shipping_size_radio.attributes, list)
shipping_size_radio_is_checked = "checked" in shipping_size_radio.attributes

if shipping_size_radio_is_checked:
await self.web_click(
By.XPATH,
'//*[contains(@class, "ModalDialog--Actions")]'
'//*[contains(@class, "Button-primary") and .//*[text()[contains(.,"Weiter")]]]')

unwanted_shipping_packages = [
package for size, package in shipping_options_mapping.values()
if size == shipping_size and package not in shipping_packages
]
to_be_clicked_shipping_packages = unwanted_shipping_packages
else:
await self.web_click(By.CSS_SELECTOR, f'.SingleSelectionItem--Main input[type=radio][data-testid="{shipping_size}"]')
to_be_clicked_shipping_packages = list(shipping_packages)

for shipping_package in to_be_clicked_shipping_packages:
await self.web_click(
By.XPATH,
'//*[contains(@class, "CarrierSelectionModal")]'
'//*[contains(@class, "CarrierOption")]'
f'//*[contains(@class, "CarrierOption--Main") and @data-testid="{shipping_package}"]'
)

await self.web_click(By.XPATH, '//*[contains(@class, "ModalDialog--Actions")]//button[.//*[text()[contains(.,"Fertig")]]]')
except TimeoutError as ex:
LOG.debug(ex, exc_info = True)
Expand Down

0 comments on commit 9deeca7

Please sign in to comment.