Вывел получение данных в единую функцию
This commit is contained in:
parent
4fc9b0fc64
commit
b7272a6e1e
|
@ -1,6 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import backoff
|
import backoff
|
||||||
|
@ -11,18 +12,28 @@ from settings import ApiConfig, EtlConfig
|
||||||
from state import State
|
from state import State
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
|
||||||
etl_config = EtlConfig()
|
etl_config = EtlConfig()
|
||||||
|
|
||||||
|
|
||||||
class ApiExtractor:
|
class ApiExtractor:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.api_config = ApiConfig()
|
self.api_config = ApiConfig()
|
||||||
self.headers = {"Authorization-Token": self.api_config.token}
|
self.headers = {
|
||||||
|
"Authorization-Token": self.api_config.token,
|
||||||
|
}
|
||||||
self.fields = ["ID", "IBLOCK_ID", "NAME", "CODE", "SECTION_ID"]
|
self.fields = ["ID", "IBLOCK_ID", "NAME", "CODE", "SECTION_ID"]
|
||||||
|
|
||||||
@backoff.on_exception(backoff.expo, (aiohttp.ClientResponseError, aiohttp.ClientConnectorError,
|
async def __get_data(self, url, data):
|
||||||
aiohttp.ServerDisconnectedError), base=2, factor=1,
|
async with aiohttp.ClientSession() as session:
|
||||||
max_value=etl_config.max_wait_size, max_tries=None)
|
async with session.get(url=url, headers=self.headers,
|
||||||
|
data=data) as resp:
|
||||||
|
return json.loads(await resp.text())
|
||||||
|
|
||||||
|
@backoff.on_exception(backoff.expo, (aiohttp.ClientResponseError, aiohttp.ClientConnectorError), base=2, factor=1,
|
||||||
|
max_value=int(etl_config.max_wait_size), max_tries=None)
|
||||||
async def get_extract_data(self, state: State, iblock_id: int, fields: list = None, **kwargs) -> list[Abitr]:
|
async def get_extract_data(self, state: State, iblock_id: int, fields: list = None, **kwargs) -> list[Abitr]:
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
|
@ -38,20 +49,17 @@ class ApiExtractor:
|
||||||
data['bitrFilter'] = json.dumps({'>ID': str(min_id)}, ensure_ascii=False)
|
data['bitrFilter'] = json.dumps({'>ID': str(min_id)}, ensure_ascii=False)
|
||||||
abitrs = []
|
abitrs = []
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
# Получаем всех новых абитуриентов
|
||||||
async with session.get(f'{self.api_config.host}/getnativeiblockelementslist', headers=self.headers,
|
results = await self.__get_data(f'{self.api_config.host}/getnativeiblockelementslist', data)
|
||||||
data=data) as resp:
|
|
||||||
|
|
||||||
results = json.loads(await resp.text())
|
|
||||||
if len(results) > 0:
|
if len(results) > 0:
|
||||||
logging.info(f'Получение абитуриентов ID > {min_id}. Iblock - {iblock_id}')
|
logging.info(f'Получение абитуриентов ID > {min_id}. Iblock - {iblock_id}')
|
||||||
for result in results:
|
for result in results:
|
||||||
data = {
|
data = {
|
||||||
'elementId': result,
|
'elementId': result,
|
||||||
}
|
}
|
||||||
async with session.get(f'{self.api_config.host}/getiblockelement/', headers=self.headers,
|
|
||||||
data=data) as resp:
|
# Получаем информацию об конкретном абитуриенте
|
||||||
res = json.loads(await resp.text())
|
res = await self.__get_data(f'{self.api_config.host}/getiblockelement', data)
|
||||||
res = res[str(result)]
|
res = res[str(result)]
|
||||||
class_attrs = vars(Abitr)['__annotations__']
|
class_attrs = vars(Abitr)['__annotations__']
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user