From c20d979e1049e8f715ea94dd29b5a12699ac16df Mon Sep 17 00:00:00 2001 From: el Mau Date: Tue, 17 Jan 2023 01:07:39 -0600 Subject: [PATCH] Initial clean portal_sat --- .gitignore | 1 + source/main.py | 12 +++--------- source/sat/portal_sat.py | 31 +++++++++++++++++++++++++++++++ source/sat/util.py | 22 ++++++++++++++++++++++ 4 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 source/sat/portal_sat.py create mode 100644 source/sat/util.py diff --git a/.gitignore b/.gitignore index 5d381cc..b8c2fe7 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +portal_sat.bk.py diff --git a/source/main.py b/source/main.py index e82de33..72f88a3 100644 --- a/source/main.py +++ b/source/main.py @@ -2,26 +2,20 @@ import falcon import json - - -XML = """ - - - -""" +from sat.util import get_uuid class JSONTranslator(): def process_response(self, req, resp, resource, req_succeeded): if not hasattr(resp.context, 'result'): return - resp.body = json.dumps(resp.context.result) + resp.text = json.dumps(resp.context.result) class AppApi(object): def on_get(self, req, resp, rfc, cfdi): - resp.context['result'] = {'error': '', 'xml': XML} + resp.context['result'] = get_uuid(rfc, cfdi) resp.status = falcon.HTTP_200 diff --git a/source/sat/portal_sat.py b/source/sat/portal_sat.py new file mode 100644 index 0000000..51a4e07 --- /dev/null +++ b/source/sat/portal_sat.py @@ -0,0 +1,31 @@ + +import requests +from requests import Session, exceptions, adapters +requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL' + + +class PortalSAT(object): + URL_MAIN = 'https://portalcfdi.facturaelectronica.sat.gob.mx/' + HOST = 'cfdiau.sat.gob.mx' + BROWSER = 'Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0' + REFERER = 'https://cfdiau.sat.gob.mx/nidp/app/login?id=SATUPCFDiCon&sid=0&option=credential&sid=0' + + PORTAL = 'portalcfdi.facturaelectronica.sat.gob.mx' + URL_LOGIN = 'https://{}/nidp/app/login'.format(HOST) + URL_FORM = 'https://{}/nidp/app/login?sid=0&sid=0'.format(HOST) + URL_PORTAL = 'https://portalcfdi.facturaelectronica.sat.gob.mx/' + URL_CONTROL = 'https://cfdicontribuyentes.accesscontrol.windows.net/v2/wsfederation' + URL_CONSULTA = URL_PORTAL + 'Consulta.aspx' + URL_RECEPTOR = URL_PORTAL + 'ConsultaReceptor.aspx' + URL_EMISOR = URL_PORTAL + 'ConsultaEmisor.aspx' + URL_LOGOUT = URL_PORTAL + 'logout.aspx?salir=y' + + def __init__(self, cert, cfdi_uuid): + self._cert = cert + self._uuid = cfdi_uuid + self.error = '' + self.is_connect = False + self._session = Session() + a = adapters.HTTPAdapter(pool_connections=512, pool_maxsize=512, max_retries=5) + self._session.mount('https://', a) + diff --git a/source/sat/util.py b/source/sat/util.py new file mode 100644 index 0000000..0592311 --- /dev/null +++ b/source/sat/util.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +from uuid import UUID +from .portal_sat import PortalSAT + + +def validate_uuid(value): + try: + UUID(value) + return True + except ValueError: + return False + + +def get_uuid(rfc, cfdi): + data = {'error': '', 'xml': ''} + + if not validate_uuid(cfdi): + data['error'] = 'UUID inválido' + return data + + return data