56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
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) |