Delete ads only by id (and not by title). Fixes #55

This commit is contained in:
sebthom
2022-05-15 13:51:22 +02:00
parent 6db2b53bb6
commit 2c3515ec00

View File

@@ -45,6 +45,7 @@ class KleinanzeigenBot(SeleniumMixin):
self.command = "help" self.command = "help"
self.ads_selector = "due" self.ads_selector = "due"
self.delete_old_ads = True self.delete_old_ads = True
self.delete_ads_by_title = False
def __del__(self) -> None: def __del__(self) -> None:
if self.file_log: if self.file_log:
@@ -346,20 +347,28 @@ class KleinanzeigenBot(SeleniumMixin):
csrf_token_elem = self.web_find(By.XPATH, "//meta[@name='_csrf']") csrf_token_elem = self.web_find(By.XPATH, "//meta[@name='_csrf']")
csrf_token = csrf_token_elem.get_attribute("content") csrf_token = csrf_token_elem.get_attribute("content")
published_ads = json.loads(self.web_request(f"{self.root_url}/m-meine-anzeigen-verwalten.json?sort=DEFAULT")["content"])["ads"] if self.delete_ads_by_title:
published_ads = json.loads(self.web_request(f"{self.root_url}/m-meine-anzeigen-verwalten.json?sort=DEFAULT")["content"])["ads"]
for published_ad in published_ads: for published_ad in published_ads:
published_ad_id = int(published_ad.get("id", -1)) published_ad_id = int(published_ad.get("id", -1))
published_ad_title = published_ad.get("title", "") published_ad_title = published_ad.get("title", "")
if ad_cfg["id"] == published_ad_id or ad_cfg["title"] == published_ad_title: if ad_cfg["id"] == published_ad_id or ad_cfg["title"] == published_ad_title:
LOG.info(" -> deleting %s '%s'...", published_ad_id, published_ad_title) LOG.info(" -> deleting %s '%s'...", published_ad_id, published_ad_title)
self.web_request( self.web_request(
url = f"{self.root_url}/m-anzeigen-loeschen.json?ids={published_ad_id}", url = f"{self.root_url}/m-anzeigen-loeschen.json?ids={published_ad_id}",
method = "POST", method = "POST",
headers = {"x-csrf-token": csrf_token} headers = {"x-csrf-token": csrf_token}
) )
pause(1500, 3000) elif ad_cfg["id"]:
self.web_request(
url = f"{self.root_url}/m-anzeigen-loeschen.json?ids={ad_cfg['id']}",
method = "POST",
headers = {"x-csrf-token": csrf_token},
valid_response_codes = [200, 404]
)
pause(1500, 3000)
ad_cfg["id"] = None ad_cfg["id"] = None
return True return True