From 4794aee88bb1b5e800acd439efee2a829eac4308 Mon Sep 17 00:00:00 2001 From: sebthom Date: Sat, 11 Jun 2022 17:14:40 +0200 Subject: [PATCH] Add "delete" command. Fixes #56 --- kleinanzeigen_bot/__init__.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/kleinanzeigen_bot/__init__.py b/kleinanzeigen_bot/__init__.py index 29dd6b5..3ec81e8 100644 --- a/kleinanzeigen_bot/__init__.py +++ b/kleinanzeigen_bot/__init__.py @@ -79,7 +79,17 @@ class KleinanzeigenBot(SeleniumMixin): LOG.info("############################################") LOG.info("DONE: No new/outdated ads found.") LOG.info("############################################") - + case "delete": + self.configure_file_logging() + self.load_config() + if ads := self.load_ads(): + self.create_webdriver_session() + self.login() + self.delete_ads(ads) + else: + LOG.info("############################################") + LOG.info("DONE: No ads to delete found.") + LOG.info("############################################") case _: LOG.error("Unknown command: %s", self.command) sys.exit(2) @@ -98,6 +108,7 @@ class KleinanzeigenBot(SeleniumMixin): Commands: publish - (re-)publishes ads verify - verifies the configuration files + delete - deletes ads -- help - displays this help (default command) version - displays the application version @@ -340,6 +351,19 @@ class KleinanzeigenBot(SeleniumMixin): self.web_await(lambda _: self.webdriver.find_element(By.ID, "recaptcha-anchor").get_attribute("aria-checked") == "true", timeout = 5 * 60) self.webdriver.switch_to.default_content() + def delete_ads(self, ad_cfgs:list[tuple[str, dict[str, Any], dict[str, Any]]]) -> None: + count = 0 + + for (ad_file, ad_cfg, _) in ad_cfgs: + count += 1 + LOG.info("Processing %s/%s: '%s' from [%s]...", count, len(ad_cfgs), ad_cfg["title"], ad_file) + self.delete_ad(ad_cfg) + pause(2000, 4000) + + LOG.info("############################################") + LOG.info("DONE: Deleting %s", pluralize("ad", count)) + LOG.info("############################################") + def delete_ad(self, ad_cfg: dict[str, Any]) -> bool: LOG.info("Deleting ad '%s' if already present...", ad_cfg["title"])