From 978eb19862d1a73a2bdf538b5ccadeecf936b73a Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Thu, 15 Jul 2021 13:14:14 -0500 Subject: [PATCH] Descarga por tipo, emitidas o recibidas --- README.md | 1 - source/sat/sat_web.py | 6 +--- source/sat/util.py | 64 ++++++++++++++++++++++++++++++------------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4d850b6..8e4d4ce 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ Descarga masiva del SAT ## Software libre, no software "gratis" -Esta librería tiene un costo simbólico de $100 MXN anuales En orden de preferencia diff --git a/source/sat/sat_web.py b/source/sat/sat_web.py index a032478..50e2208 100644 --- a/source/sat/sat_web.py +++ b/source/sat/sat_web.py @@ -176,13 +176,9 @@ class SATWebService(): 'FechaFinal': date_end.strftime(FORMAT), 'FechaInicial': date_start.strftime(FORMAT), 'TipoSolicitud': 'CFDI', - 'RfcEmisor': self._cert.rfc, + args['rfc']: self._cert.rfc, } request = ET.SubElement(request_down, node_name, attr) - # ~ if rfc_emisor is not None: - # ~ solicitud.set('RfcEmisor', rfc_emisor) - # ~ if rfc_receptor is not None: - # ~ solicitud.set('RfcReceptor', rfc_receptor) nsmap = {None: self.NS['xd']} signature = ET.SubElement(request, 'Signature', nsmap=nsmap) diff --git a/source/sat/util.py b/source/sat/util.py index d6dab57..1dd2511 100644 --- a/source/sat/util.py +++ b/source/sat/util.py @@ -227,19 +227,31 @@ def _validate_download_args(args): return True, data +def _request_download(sat, data, key): + data['rfc'] = key + result = sat.request_download(data) + # ~ Usando un simple print, permite capturarlo desde cualquier lenguaje + print(result) + return result + + def solicitar_descarga(args): result, data = _validate_requests_args(args) if not result: return sat = SATWebService(data['cert']) - if not sat.is_authenticate: log.error(sat.error) return - result = sat.request_download(data) - print(result) + if data['type'] == 'e': + _request_download(sat, data, 'RfcEmisor') + elif data['type'] == 'r': + _request_download(sat, data, 'RfcReceptor') + else: + _request_download(sat, data, 'RfcEmisor') + _request_download(sat, data, 'RfcReceptor') return @@ -256,6 +268,7 @@ def verificar_descarga(args): return result = sat.verify(data) + # ~ Usando un simple print, permite capturarlo desde cualquier lenguaje print(result) return @@ -289,8 +302,8 @@ def descargar_archivos(args): log.info(msg) with open(path_zip, 'wb') as f: f.write(file_data) - msg = f'\tArchivo guardado correctamente' - log.info(msg) + msg = f's\tArchivo guardado correctamente' + log.info(msg) return @@ -309,29 +322,20 @@ def _validate_args(args): return True, data -def descargar(args): +def _download(sat, data, key): OK = '5000' - result, data = _validate_args(args) - if not result: - return - - sat = SATWebService(data['cert']) - - if not sat.is_authenticate: - log.error(sat.error) - return - - result = sat.request_download(data) + result = _request_download(sat, data, key) if result['CodEstatus'] != OK: log.error(result) return - msg = 'Esperando un minuto para verificar la descarga...' + data['id'] = result['IdSolicitud'] + + msg = f"Descarga aceptada con el ID: {data['id']}\nEsperando un minuto para verificar la descarga..." log.info(msg) sleep(60) - data['id'] = result['IdSolicitud'] while True: result = sat.verify(data) if result['EstadoSolicitud'] in ('1', '2'): @@ -361,3 +365,25 @@ def descargar(args): return + +def descargar(args): + result, data = _validate_args(args) + if not result: + return + + sat = SATWebService(data['cert']) + + if not sat.is_authenticate: + log.error(sat.error) + return + + if data['type'] == 'e': + _download(sat, data, 'RfcEmisor') + elif data['type'] == 'r': + _download(sat, data, 'RfcReceptor') + else: + _download(sat, data, 'RfcEmisor') + _download(sat, data, 'RfcReceptor') + + return +