From b7ed25aeb9d73b785b0101460e3e49c5ec4f43b8 Mon Sep 17 00:00:00 2001 From: ehsang Date: Sun, 15 Sep 2024 23:40:53 +0330 Subject: [PATCH] celeray task --- say/crawler/__init__.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/say/crawler/__init__.py b/say/crawler/__init__.py index d7a38425..204ed871 100644 --- a/say/crawler/__init__.py +++ b/say/crawler/__init__.py @@ -119,29 +119,29 @@ def get_data(self, need_id, force=False): if self.dkp is None: return - # fresh products have different api / Digikala redirect to new link for fresh product url = self.API_URL_NOT_FRESH % self.dkp if force: r = requests.get(url) else: r = request_with_cache(url) if r.status_code != 200: - print(r) - if r.status_code == 302: - print("Checking if fresh product...") - url = self.API_URL_FRESH % self.dkp - if force: - r = requests.get(url) - else: - r = request_with_cache(url) - if r.status_code != 200: - print("Could not call the digikala api", need_id ) - return + return + + # fresh products have different api / Digikala redirect to new link for fresh product + # Typical respond: {'status': 302, 'redirect_url': {'base': None, 'uri': '/fresh/product/dkp-10269403/'}} + result = r.json + if result["status"] == 302 and "fresh" in result["redirect_url"]["uri"]: + url = self.API_URL_FRESH % self.dkp + if force: + r = requests.get(url) else: + r = request_with_cache(url) + if r.status_code != 200: return - print(url) + else: + return - data = r.json()['data'] + data = result['data'] if data['product'].get('is_inactive'): return dict(cost='unavailable', img=None, title=None)