import time from sqlalchemy import select from external.marketplace.wildberries.core import WildberriesMarketplaceApi from marketplaces.base.core import BaseMarketplaceController from models import Product, ProductBarcode, ProductImage, WildberriesProduct class WildberriesController(BaseMarketplaceController): api: WildberriesMarketplaceApi async def synchronize_products(self): products = [] barcodes = [] images = [] wildberries_products = [] marketplace_id: int = self.marketplace.id synchronized_nm_uuids = set( ( await self.session.scalars( select( WildberriesProduct.nm_uuid ) .where( WildberriesProduct.marketplace_id == marketplace_id ) ) ).all() ) async for card in self.api.get_all_products(): nm_uuid = card['nmUUID'] if nm_uuid in synchronized_nm_uuids: continue sizes: list[dict] = card.get('sizes') or [] for size in sizes: tech_size = size.get('techSize') wb_size = size.get('wbSize') size_value = tech_size or wb_size product = Product( client_id=self.marketplace.client_id, name=card['title'], article=card['vendorCode'], size=size_value ) skus = size.get('skus') or [] for sku in skus: barcode = ProductBarcode( product=product, barcode=sku ) barcodes.append(barcode) photos = card.get('photos') or [] for photo in photos: image = ProductImage( product=product, image_url=photo['big'] ) images.append(image) break wildberries_product = WildberriesProduct( marketplace_id=self.marketplace.id, product=product, nm_uuid=nm_uuid ) wildberries_products.append( wildberries_product ) products.append(product) instances = products + wildberries_products + barcodes + images start = time.time() self.session.add_all(instances) await self.session.commit() print(f'Add and commit elapsed: {time.time() - start}')