att
This commit is contained in:
+18
-48
@@ -16,7 +16,6 @@ from pathlib import Path
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
import pdfplumber
|
import pdfplumber
|
||||||
from minio import Minio
|
|
||||||
|
|
||||||
|
|
||||||
BASE_URL = "https://sistema.sgztrade.com.br"
|
BASE_URL = "https://sistema.sgztrade.com.br"
|
||||||
@@ -75,12 +74,8 @@ SQLSERVER_CONN = os.getenv(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
MINIO_ENDPOINT = os.getenv("MINIO_ENDPOINT", "10.77.77.29:31200")
|
AZURE_UPLOAD_URL = "https://api.grupoginseng.com.br/portal/boletosvitrine/boleto/arquivos"
|
||||||
MINIO_ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY", "admin")
|
AZURE_PREFIX = os.getenv("AZURE_PREFIX", "Boletos")
|
||||||
MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "admin123")
|
|
||||||
MINIO_BUCKET = os.getenv("MINIO_BUCKET", "boletosvitrine")
|
|
||||||
MINIO_PREFIX = os.getenv("MINIO_PREFIX", "Boletos")
|
|
||||||
MINIO_SECURE = os.getenv("MINIO_SECURE", "false").strip().lower() in ("1", "true", "yes")
|
|
||||||
MAX_PARALLEL_REQUESTS = 5
|
MAX_PARALLEL_REQUESTS = 5
|
||||||
MAX_PARALLEL_PDFS_PER_USER = 1
|
MAX_PARALLEL_PDFS_PER_USER = 1
|
||||||
DOWNLOAD_RETRY_ATTEMPTS = 4
|
DOWNLOAD_RETRY_ATTEMPTS = 4
|
||||||
@@ -371,31 +366,14 @@ def _guess_cte_nfe_keys_from_xml_names(xml_names: List[str]) -> Dict[str, Option
|
|||||||
return {"chave_cte": cte_key, "chave_nfe": nfe_key}
|
return {"chave_cte": cte_key, "chave_nfe": nfe_key}
|
||||||
|
|
||||||
|
|
||||||
def criar_minio_client():
|
def upload_azure(prefix: str, filename: str, data: bytes, content_type: str) -> str:
|
||||||
client = Minio(
|
response = requests.post(
|
||||||
MINIO_ENDPOINT,
|
AZURE_UPLOAD_URL,
|
||||||
access_key=MINIO_ACCESS_KEY,
|
files={"file": (filename, io.BytesIO(data), content_type)},
|
||||||
secret_key=MINIO_SECRET_KEY,
|
data={"prefix": prefix},
|
||||||
secure=MINIO_SECURE,
|
|
||||||
)
|
)
|
||||||
if not client.bucket_exists(MINIO_BUCKET):
|
response.raise_for_status()
|
||||||
client.make_bucket(MINIO_BUCKET)
|
return f"{prefix.rstrip('/')}/{filename}"
|
||||||
return client, MINIO_BUCKET, MINIO_PREFIX
|
|
||||||
|
|
||||||
|
|
||||||
def upload_bytes_minio(client: Minio, bucket: str, prefix: str, object_name: str, data: bytes, content_type: str) -> str:
|
|
||||||
object_name = object_name.lstrip("/")
|
|
||||||
if prefix:
|
|
||||||
object_name = f"{prefix.rstrip('/')}/{object_name}"
|
|
||||||
|
|
||||||
client.put_object(
|
|
||||||
bucket,
|
|
||||||
object_name,
|
|
||||||
io.BytesIO(data),
|
|
||||||
length=len(data),
|
|
||||||
content_type=content_type,
|
|
||||||
)
|
|
||||||
return object_name
|
|
||||||
|
|
||||||
|
|
||||||
def _safe_file_part(name: str) -> str:
|
def _safe_file_part(name: str) -> str:
|
||||||
@@ -529,11 +507,10 @@ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def process_boletos_to_minio_sql(boletos: List[Dict[str, Optional[str]]]) -> int:
|
def process_boletos_to_azure_sql(boletos: List[Dict[str, Optional[str]]]) -> int:
|
||||||
if not boletos:
|
if not boletos:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
client, bucket, prefix = criar_minio_client()
|
|
||||||
prepared_rows: List[Dict[str, Optional[str]]] = []
|
prepared_rows: List[Dict[str, Optional[str]]] = []
|
||||||
uploads = 0
|
uploads = 0
|
||||||
for b in boletos:
|
for b in boletos:
|
||||||
@@ -549,26 +526,19 @@ def process_boletos_to_minio_sql(boletos: List[Dict[str, Optional[str]]]) -> int
|
|||||||
base_name = arquivo.name if arquivo else Path(arquivo_str or "boleto.pdf").name
|
base_name = arquivo.name if arquivo else Path(arquivo_str or "boleto.pdf").name
|
||||||
nf = _extract_nf_from_name(base_name)
|
nf = _extract_nf_from_name(base_name)
|
||||||
if nf and b.get("chave_cte"):
|
if nf and b.get("chave_cte"):
|
||||||
obj = f"boleto/cte_{b.get('chave_cte')}_nf_{nf}_{_safe_file_part(base_name)}"
|
filename = f"cte_{b.get('chave_cte')}_nf_{nf}_{_safe_file_part(base_name)}"
|
||||||
elif nf and b.get("chave_nfe"):
|
elif nf and b.get("chave_nfe"):
|
||||||
obj = f"boleto/nfe_{b.get('chave_nfe')}_nf_{nf}_{_safe_file_part(base_name)}"
|
filename = f"nfe_{b.get('chave_nfe')}_nf_{nf}_{_safe_file_part(base_name)}"
|
||||||
elif b.get("chave_nfe"):
|
elif b.get("chave_nfe"):
|
||||||
obj = f"boleto/nfe_{b.get('chave_nfe')}_{_safe_file_part(base_name)}"
|
filename = f"nfe_{b.get('chave_nfe')}_{_safe_file_part(base_name)}"
|
||||||
elif nf:
|
elif nf:
|
||||||
obj = f"boleto/nf_{nf}_{_safe_file_part(base_name)}"
|
filename = f"nf_{nf}_{_safe_file_part(base_name)}"
|
||||||
else:
|
else:
|
||||||
obj = f"boleto/{_safe_file_part(base_name)}"
|
filename = _safe_file_part(base_name)
|
||||||
object_key = upload_bytes_minio(
|
object_key = upload_azure(AZURE_PREFIX, filename, pdf_bytes, "application/pdf")
|
||||||
client,
|
|
||||||
bucket,
|
|
||||||
prefix,
|
|
||||||
obj,
|
|
||||||
pdf_bytes,
|
|
||||||
"application/pdf",
|
|
||||||
)
|
|
||||||
uploads += 1
|
uploads += 1
|
||||||
|
|
||||||
b["minio_bucket"] = bucket
|
b["minio_bucket"] = "boletosvitrine"
|
||||||
b["minio_object_key"] = object_key
|
b["minio_object_key"] = object_key
|
||||||
row = dict(b)
|
row = dict(b)
|
||||||
row.pop("_pdf_bytes", None)
|
row.pop("_pdf_bytes", None)
|
||||||
@@ -1111,7 +1081,7 @@ if __name__ == "__main__":
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
print(f"{email_ativo}: falha ao processar PDF remoto '{rp}': {exc}")
|
print(f"{email_ativo}: falha ao processar PDF remoto '{rp}': {exc}")
|
||||||
|
|
||||||
gravados = process_boletos_to_minio_sql(boletos) if boletos else 0
|
gravados = process_boletos_to_azure_sql(boletos) if boletos else 0
|
||||||
return (email_ativo, len(boletos), gravados, None)
|
return (email_ativo, len(boletos), gravados, None)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return (email_ativo, 0, 0, str(exc))
|
return (email_ativo, 0, 0, str(exc))
|
||||||
|
|||||||
Reference in New Issue
Block a user