From a876add5a784a924ea6233f811239be45659fc72 Mon Sep 17 00:00:00 2001 From: sebthom Date: Fri, 22 Nov 2024 12:41:34 +0100 Subject: [PATCH] feat: by default delete old ads after republishing #338 --- README.md | 7 ++++++- src/kleinanzeigen_bot/__init__.py | 12 +++++++----- src/kleinanzeigen_bot/resources/config_defaults.yaml | 12 +++++++++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2ce3ef5..f11eba6 100644 --- a/README.md +++ b/README.md @@ -224,7 +224,7 @@ Valid file extensions are `.json`, `.yaml` and `.yml` The following parameters can be configured: ```yaml -# wild card patterns to select ad configuration files +# glob (wildcard) patterns to select ad configuration files # if relative paths are specified, then they are relative to this configuration file ad_files: - "./**/ad_*.{json,yml,yaml}" @@ -254,6 +254,11 @@ categories: Verschenken & Tauschen > Verleihen: 272/274 Verschenken & Tauschen > Verschenken: 272/192 +# publishing configuration +publishing: + delete_old_ads: "AFTER_PUBLISH" # one of: AFTER_PUBLISH, BEFORE_PUBLISH, NEVER + delete_old_ads_by_title: true # only works if delete_old_ads is set to BEFORE_PUBLISH + # browser configuration browser: # https://peter.sh/experiments/chromium-command-line-switches/ diff --git a/src/kleinanzeigen_bot/__init__.py b/src/kleinanzeigen_bot/__init__.py index 294bcfc..c4e1a62 100644 --- a/src/kleinanzeigen_bot/__init__.py +++ b/src/kleinanzeigen_bot/__init__.py @@ -53,8 +53,7 @@ class KleinanzeigenBot(WebScrapingMixin): self.command = "help" self.ads_selector = "due" - self.delete_old_ads = True - self.delete_ads_by_title = False + self.keep_old_ads = False def __del__(self) -> None: if self.file_log: @@ -195,7 +194,7 @@ class KleinanzeigenBot(WebScrapingMixin): case "--force": self.ads_selector = "all" case "--keep-old": - self.delete_old_ads = False + self.keep_old_ads = True case "-v" | "--verbose": LOG.setLevel(logging.DEBUG) logging.getLogger("nodriver").setLevel(logging.INFO) @@ -462,7 +461,7 @@ class KleinanzeigenBot(WebScrapingMixin): if csrf_token is None: raise AssertionError("Expected CSRF Token not found in HTML content!") - if self.delete_ads_by_title: + if self.config["publishing"]["delete_old_ads_by_title"]: published_ads = json.loads((await self.web_request(f"{self.root_url}/m-meine-anzeigen-verwalten.json?sort=DEFAULT"))["content"])["ads"] for published_ad in published_ads: @@ -507,7 +506,7 @@ class KleinanzeigenBot(WebScrapingMixin): """ await self.assert_free_ad_limit_not_reached() - if self.delete_old_ads: + if self.config["publishing"]["delete_old_ads"] == "BEFORE_PUBLISH" and not self.keep_old_ads: await self.delete_ad(ad_cfg) LOG.info("Publishing ad '%s'...", ad_cfg["title"]) @@ -677,6 +676,9 @@ class KleinanzeigenBot(WebScrapingMixin): utils.save_dict(ad_file, ad_cfg_orig) + if self.config["publishing"]["delete_old_ads"] == "AFTER_PUBLISH" and not self.keep_old_ads: + await self.delete_ad(ad_cfg) + async def __set_condition(self, condition_value: str) -> None: condition_mapping = { "new_with_tag": "Neu mit Etikett", diff --git a/src/kleinanzeigen_bot/resources/config_defaults.yaml b/src/kleinanzeigen_bot/resources/config_defaults.yaml index 319ec5a..7094899 100644 --- a/src/kleinanzeigen_bot/resources/config_defaults.yaml +++ b/src/kleinanzeigen_bot/resources/config_defaults.yaml @@ -20,9 +20,15 @@ ad_defaults: # additional name to category ID mappings, see default list at # https://github.com/Second-Hand-Friends/kleinanzeigen-bot/blob/main/src/kleinanzeigen_bot/resources/categories.yaml -# Notebooks: 161/278 # Elektronik > Notebooks -# Autoteile: 210/223/sonstige_autoteile # Auto, Rad & Boot > Autoteile & Reifen > Weitere Autoteile -categories: [] +# +# categories: +# Elektronik > Notebooks: 161/278 +# Jobs > Praktika: 102/125 +categories: {} + +publishing: + delete_old_ads: "AFTER_PUBLISH" # one of: AFTER_PUBLISH, BEFORE_PUBLISH, NEVER + delete_old_ads_by_title: true # only works if delete_old_ads is set to BEFORE_PUBLISH # browser configuration browser: