Compare commits

..

No commits in common. "32c1617f7dd3686f3109042edb34732495a0eac8" and "c19c91bb8169378e13852eb7b13807e504035268" have entirely different histories.

4 changed files with 27 additions and 46 deletions

View File

@ -125,22 +125,21 @@ class StolichkiDriver(webdriver.Chrome):
self.add_cookie(new_cookie)
def get_network_response(self, url_mask):
logs = self.get_log("performance")
logs = self.get_log("performance")[self.last_resp_index:]
url_mask = re.compile(url_mask)
body = None
for log in filter(lambda log: self.__filter_logs(log), logs):
message = json.loads(log.get("message"))['message']
request_id = message["params"]["requestId"]
resp_url = message["params"]["response"]["url"]
if re.fullmatch(url_mask, resp_url):
body_ = self.execute_cdp_cmd("Network.getResponseBody", {"requestId": request_id})
body = json.loads(body_['body'])
if re.fullmatch(url_mask, resp_url) and self.last_resp_url != resp_url:
self.last_resp_index = logs.index(log)
self.last_resp_url = resp_url
body = self.execute_cdp_cmd("Network.getResponseBody", {"requestId": request_id})
return json.loads(body['body'])
return body
return None
def __filter_logs(self, log):
message = json.loads(log.get("message"))['message']

View File

@ -1,12 +1,11 @@
import logging
import dataclasses
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from .browser import StolichkiDriver
from .types import Store
class Product:
id: int = 0
@ -49,6 +48,11 @@ class Product:
def __parse_stores(self):
try:
self.driver.find_element(By.CSS_SELECTOR, "p.badge-class.product-not-found")
return []
except:
pass
try:
self.driver.find_element(By.CSS_SELECTOR, "a.stores-stock.stores-order.package")
return []
except:
@ -59,31 +63,19 @@ class Product:
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "tr-start-store")))
reg_stores = r"https://stolichki\.ru/drugs/\d{1,}/stores\?cityId=\d{1,}&no-captcha-token=.{1,}"
response = self.driver.get_network_response(reg_stores)
stores = self.driver.find_elements(By.CLASS_NAME, "tr-start-store")
stores_list = []
for store in stores:
try:
store_name = store.find_element(By.CLASS_NAME, "store-link").text
number_of_product = int(store.find_element(By.CLASS_NAME, "part-quantity").text)
for store in response.get("stores"):
if store.get("parts"):
prices = store.get("parts")[0]
stores_list.append({
"name": store_name,
"quantity": number_of_product
}.copy())
store_normal = Store(
id = store.get("id"),
name=store.get("name"),
address=store.get("address"),
price=prices.get("priceStore"),
price_order=prices.get("priceOnline")
)
except:
continue
self.stores.append(store_normal)
if bool(prices.get("bad")):
store_special = dataclasses.replace(store_normal)
store_special.name += " СП"
discounts = prices.get("discounts")
discount = discounts[0].get("value")
store_special.price = store_normal.price - (store_normal.price * (discount / 100))
store_special.price_order = store_normal.price_order - (store_normal.price_order * (discount / 100))
self.stores.append(store_special)
return stores_list

View File

@ -1 +0,0 @@
from .store import *

View File

@ -1,9 +0,0 @@
from dataclasses import dataclass
@dataclass
class Store():
id: int
name: str
address: str
price: float = 0.0
price_order: float = 0.0