diff --git a/env_example b/env_example index 4601ce2..c26f221 100644 --- a/env_example +++ b/env_example @@ -5,6 +5,7 @@ USERNAME = "..." PASSWORD = "..." URL_REPORT = "..." URL_REALISATION = "..." +URL_CONTRAGENTS = "..." BUYER = "..." #Физические лица-розница COMPANY="..." #Организация_Key diff --git a/requirements.txt b/requirements.txt index 764e833..f5d0f46 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ pandas==2.3.3 openpyxl==3.1.5 pydantic==2.12.3 pydantic_settings == 2.12.0 +requests==2.32.5 dotenv==0.9.9 \ No newline at end of file diff --git a/server/backend/api/contragents.py b/server/backend/api/contragents.py new file mode 100644 index 0000000..0167148 --- /dev/null +++ b/server/backend/api/contragents.py @@ -0,0 +1,56 @@ +import requests +from requests.auth import HTTPBasicAuth +import xml.etree.ElementTree as ET +from base64 import b64encode +from server.backend.schemas.pydantic import settings + + +auth_str = f"{setting.USERNAME}:{settings.PASSWORD}" +b64_auth_str = b64encode(auth_str.encode("utf-8")).decode("utf-8") + +session = requests.Session() +session.headers.update({ + "Authorization": f"Basic {b64_auth_str}", + "Accept": "application/xml", +}) + +def fetch_contragents(): + response = session.get(settings.URL_CONTRAGENTS, timeout=10) + response.raise_for_status() + return response.text + +def parse_contragents(xml: str): + NS = { + "atom": "http://www.w3.org/2005/Atom", + "m": "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", + "d": "http://schemas.microsoft.com/ado/2007/08/dataservices", + } + try: + # Парсинг XML-ответа + root = ET.fromstring(xml) + # Извлечение конкретных данных + for entry in root.findall('atom:entry', NS): + properties = entry.find('atom:content',NS).find( + 'm:properties', NS) + + ref_key = properties.find('d:Ref_Key', NS).text + description = properties.find('d:Description', NS).text + full_name = properties.find( + 'd:НаименованиеПолное', NS).text + inn = properties.find('d:ИНН', NS).text + kpp = properties.find('d:КПП', NS).text + creation_date = properties.find('d:ДатаСоздания', NS).text + + print(f"Ref_Key: {ref_key}") + print(f"Description: {description}") + print(f"Full Name: {full_name}") + print(f"ИНН: {inn}") + print(f"КПП: {kpp}") + print(f"Дата Создания: {creation_date}") + print("----") + + except ET.ParseError: + print("Ошибка при парсинге XML.") +xml_data = fetch_contragents() +root = parse_contragents(xml_data) +print(root) \ No newline at end of file