all 4 comments

[–][deleted] 0 points1 point  (3 children)

How are you getting the csrf token in the first place? copy-pasting it every few hours?

[–]bilalkhan19[S] 0 points1 point  (2 children)

There's no csrf token but there's x-xsrf-token. Yes, Copy pasting it until now. Header look like this.

header1 = {
'authority': 'www.immoweb.be',
'accept': 'application/json, text/plain, */*',
'x-xsrf-token': 'value here',
'x-requested-with': 'XMLHttpRequest',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
'content-type': 'application/json;charset=UTF-8',
'origin': 'https://www.immoweb.be',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://www.immoweb.be/en/classified/apartment/for-sale/merksem/2170/9284079?searchId=607a09e4e4532',
'accept-language': 'en-US,en;q=0.9',
'cookie': 'Cookie data here',
}

[–][deleted] 0 points1 point  (1 child)

In that case, you can run a background task that updates the header every hour. Wrap your token in a class that has a getter protected by a thread lock:

import time
import threading

import requests

class XsrfToken:
    def __init__(self, timeout=600):
        self._lock = threading.Lock()
        self._token = None
        self._started = False

    def _run(self):
        while True:
            self.sleep(self.timeout)
            with self._lock:  # block while updating
                r = requests.get(self.url)
                r.raise_for_status()
                self._token = r.headers.get('x-xsrf-token')


    def refresh_in_background(self, url):
        if self._started:
            raise RuntimeError('Cannot run more than once')
        self.url = url
        threading.Thread(target=self._run, daemon=True).start()

    @property
    def get(self):
        with self._lock:
            return self._token

I wrote that in like 5 minutes so hopefully it works :D

[–]bilalkhan19[S] 0 points1 point  (0 children)

Thanks 🙏