brantsch / wg-gesucht-updater

Bring Deine Anzeigen automatisch in den Suchergebnissen nach oben durch regelmäßige Aktualisierung. Automatically push your ads to the top of the list by periodically updating them.
14 stars 6 forks source link

Deactivation not working #5

Open blendaddict opened 1 year ago

blendaddict commented 1 year ago

I tried running the script on my ad with only the deactivation part ("deactivated" : 1) like this


import getpass
import time

import requests
from bs4 import BeautifulSoup

class WGGesuchtSession(requests.Session):
    def login(self, user, password):

        url = "https://www.wg-gesucht.de/ajax/sessions.php?action=login"
        data = {"login_email_username": user,
                "login_password": password,
                "login_form_autologin": "1",
                "display_language": "en"}
        self.post(url, json=data)
        response = self.get("https://www.wg-gesucht.de/meine-anzeigen.html")
        soup = BeautifulSoup(response.text, features="html.parser")
        nodes = soup.select("a.logout_button")

        self.csrf_token = nodes[0]['data-csrf_token']
        nodes = soup.select("a.logout_button")
        self.user_id = nodes[0]['data-user_id']

    def toggle_activation(self, ad_id):

        api_url = "https://www.wg-gesucht.de/api/offers/{}/users/{}".format(ad_id, self.user_id)
        headers = {"X-User-ID": self.user_id,
                   "X-Client-ID": "wg_desktop_website",
                   "X-Authorization": "Bearer " + self.cookies.get("X-Access-Token"),
                   "X-Dev-Ref-No": self.cookies.get("X-Dev-Ref-No")}
        data = {"deactivated": "1", "csrf_token": self.csrf_token}
        r = self.patch(api_url, json=data, headers=headers)
        print("deactivated ad")
        data["deactivated"] = "0"
        # r = self.patch(api_url, json=data, headers=headers)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='Keep WG-Gesucht.de ads on top of the listing by regularly toggling their activation status.')
    parser.add_argument("--interval", nargs=1, type=int, default=3600, help="How often to update the ads. Interval in seconds, default 3600 (1h).")
    parser.add_argument("ad_id", nargs="+", help="The IDs of the ads.")
    args = parser.parse_args()
    username = input("username:")
    password = getpass.getpass("password:")
    while True:
        session = WGGesuchtSession()
        session.login(username, password)
        for ad_id in args.ad_id:
            print("toggling activation")
            session.toggle_activation(ad_id)
        time.sleep(args.interval)```
 But it did not deactivate the ad. Anyone know whether that is still possible?
SebastKl commented 1 year ago

The deactivating and reactivating definitely works like a charm. I think, there is probably a problem with the X-Access-Token and the X-Dev-Ref-No Token.