diff --git a/server/backend/api/contragents.py b/server/backend/api/contragents.py index 0167148..aab6421 100644 --- a/server/backend/api/contragents.py +++ b/server/backend/api/contragents.py @@ -3,9 +3,9 @@ from requests.auth import HTTPBasicAuth import xml.etree.ElementTree as ET from base64 import b64encode from server.backend.schemas.pydantic import settings +import pandas as pd - -auth_str = f"{setting.USERNAME}:{settings.PASSWORD}" +auth_str = f"{settings.USERNAME}:{settings.PASSWORD}" b64_auth_str = b64encode(auth_str.encode("utf-8")).decode("utf-8") session = requests.Session() @@ -20,37 +20,31 @@ def fetch_contragents(): 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-ответа + 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", + } + rows = [] + # Парсинг 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("----") - + rows.append({ + 'Ref_Key': properties.findtext('d:Ref_Key', default=None, namespaces=NS), + 'Description': properties.findtext('d:Description', default=None, namespaces=NS), + 'FullName': properties.findtext('d:НаименованиеПолное', default=None, namespaces=NS), + 'INN': properties.findtext('d:ИНН', default=None, namespaces=NS), + 'KPP': properties.findtext('d:КПП', default=None, namespaces=NS), + 'DoC': properties.findtext('d:ДатаСоздания', default=None, namespaces=NS), + }) + df = pd.DataFrame(rows) + return df except ET.ParseError: - print("Ошибка при парсинге XML.") + return "Error during the parsing process" xml_data = fetch_contragents() root = parse_contragents(xml_data) print(root) \ No newline at end of file diff --git a/server/backend/schemas/pydantic.py b/server/backend/schemas/pydantic.py index 4e62389..1ac7043 100644 --- a/server/backend/schemas/pydantic.py +++ b/server/backend/schemas/pydantic.py @@ -17,6 +17,7 @@ class Settings(BaseSettings): PASSWORD: str URL_REPORT:str URL_REALISATION:str + URL_CONTRAGENTS:str BUYER: str COMPANY: str STORE: str