rewritten crap

This commit is contained in:
2025-05-11 08:00:23 +03:00
parent b5110ec69a
commit 2e3245c8f9
8 changed files with 72 additions and 60 deletions

View File

@@ -16,26 +16,28 @@ class OzonStocksSender(BaseStocksSender):
return 100
async def _process_chunk(self, chunk: list[dict]) -> SendStockStatus:
response = await self.api.update_stocks(data=chunk)
status_code = response.status
if status_code == 200:
return SendStockStatus.SUCCESS
if response.content_type != JSONResponse.media_type:
session ,response = await self.api.update_stocks(data=chunk)
try:
status_code = response.status
if status_code == 200:
return SendStockStatus.SUCCESS
if response.content_type != JSONResponse.media_type:
return SendStockStatus.ERROR
json_data = await response.json()
error_code = json_data.get('code')
error_message = json_data.get('message')
if error_code == 8:
await asyncio.sleep(1)
return SendStockStatus.SHOULD_RETRY
logging.error(f'[{self.updater.marketplace.id}]: {error_message}')
if status_code in [
404,
500,
]:
return SendStockStatus.SHOULD_RETRY
return SendStockStatus.ERROR
json_data = await response.json()
error_code = json_data.get('code')
error_message = json_data.get('message')
if error_code == 8:
await asyncio.sleep(1)
return SendStockStatus.SHOULD_RETRY
logging.error(f'[{self.updater.marketplace.id}]: {error_message}')
if status_code in [
404,
500,
]:
return SendStockStatus.SHOULD_RETRY
return SendStockStatus.ERROR
finally:
await session.close()
async def after_chunk_processed(self):
return await asyncio.sleep(80 / 100)

View File

@@ -19,27 +19,29 @@ class WildberriesStocksSender(BaseStocksSender):
return 5
async def _process_chunk(self, chunk: list[dict]) -> SendStockStatus:
response = await self.api.update_stocks(chunk)
headers = response.headers
status_code = response.status
session,response = await self.api.update_stocks(chunk)
try:
headers = response.headers
status_code = response.status
if status_code in [
401, # Invalid token
403, # Access denied
404, # Not found
400, # Other
]:
return SendStockStatus.ERROR
# If there is rate limit
if status_code == 429:
delay_time = float(headers.get('X-Ratelimit-Reset', self.sleep_time))
await asyncio.sleep(delay_time)
self.remaining = int(headers.get('X-Ratelimit-Limit', 1))
return SendStockStatus.SHOULD_RETRY
self.remaining = int(headers.get('X-Ratelimit-Remaining', 0))
return SendStockStatus.SUCCESS
if status_code in [
401, # Invalid token
403, # Access denied
404, # Not found
400, # Other
]:
return SendStockStatus.ERROR
# If there is rate limit
if status_code == 429:
delay_time = float(headers.get('X-Ratelimit-Reset', self.sleep_time))
await asyncio.sleep(delay_time)
self.remaining = int(headers.get('X-Ratelimit-Limit', 1))
return SendStockStatus.SHOULD_RETRY
self.remaining = int(headers.get('X-Ratelimit-Remaining', 0))
return SendStockStatus.SUCCESS
finally:
await session.close()
async def after_chunk_processed(self):
if self.remaining <= 0:
await asyncio.sleep(self.sleep_time)

View File

@@ -26,18 +26,21 @@ class YandexmarketStocksSender(BaseStocksSender):
return 2000
async def _process_chunk(self, chunk: list[dict]) -> SendStockStatus:
response = await self.api.update_stocks(chunk)
status_code = response.status
if status_code == 200:
self.total_stocks_sent += len(chunk)
return SendStockStatus.SUCCESS
if status_code == 420:
time_to_sleep = 60 - (time.time() - self.start_time)
await asyncio.sleep(time_to_sleep)
return SendStockStatus.SHOULD_RETRY
response_text = await response.text()
logging.error(f'[{self.updater.marketplace.id}]: {response_text}')
return SendStockStatus.ERROR
session, response = await self.api.update_stocks(chunk)
try:
status_code = response.status
if status_code == 200:
self.total_stocks_sent += len(chunk)
return SendStockStatus.SUCCESS
if status_code == 420:
time_to_sleep = 60 - (time.time() - self.start_time)
await asyncio.sleep(time_to_sleep)
return SendStockStatus.SHOULD_RETRY
response_text = await response.text()
logging.error(f'[{self.updater.marketplace.id}]: {response_text}')
return SendStockStatus.ERROR
finally:
await session.close()
async def after_chunk_processed(self):
time_delta = time.time() - self.start_time