From a330626ceb859207668225972dd56648be3e004e Mon Sep 17 00:00:00 2001 From: Noettore Date: Thu, 20 Jan 2022 22:08:11 +0100 Subject: [PATCH] Added try except block to check for connectivity errors --- fatture_ccsr/downloader.py | 57 +++++++++++++++++++----------- fatture_ccsr/fatture_ccsr.py | 19 ++++++---- fatture_ccsr/traf2000_converter.py | 12 +++++-- 3 files changed, 58 insertions(+), 30 deletions(-) diff --git a/fatture_ccsr/downloader.py b/fatture_ccsr/downloader.py index efc5d9c..04be233 100644 --- a/fatture_ccsr/downloader.py +++ b/fatture_ccsr/downloader.py @@ -6,13 +6,21 @@ import tempfile import openpyxl import PyPDF2 import wx +import requests def download_input_file(parent): """download input file""" start_date = parent.start_date_picker.GetValue().Format("%d/%m/%Y") end_date = parent.end_date_picker.GetValue().Format("%d/%m/%Y") input_file_url = 'https://report.casadicurasanrossore.it:8443/reportserver?/STAT_FATTURATO_CTERZI&dataI='+start_date+'&dataF='+end_date+'&rs:Format=EXCELOPENXML' - downloaded_input_file = parent.session.get(input_file_url) + try: + downloaded_input_file = parent.session.get(input_file_url) + except requests.exceptions.RequestException: + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) + parent.log_dialog.log_text.AppendText("ERRORE: impossibile connettersi al portale CCSR.") + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) + wx.Yield() + return if downloaded_input_file.status_code != 200: parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) parent.log_dialog.log_text.AppendText("ERRORE: impossibile scaricare il file di input.\nControllare la connessione ad internet e l'operatività del portale CCSR. Code %d\n" % downloaded_input_file.status_code) @@ -73,28 +81,35 @@ def download_invoices(parent): downloaded_count = 0 for invoice_id, invoice in invoices.items(): - resp = parent.session.get(invoice["url"]) - if resp.status_code == 200: - with open(tmp_dir+"/"+invoice_id+".pdf", "wb") as output_file: - output_file.write(resp.content) - invoice["path"] = output_file.name - try: - PyPDF2.PdfFileReader(open(invoice["path"], "rb")) - except (PyPDF2.utils.PdfReadError, OSError): - parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) - parent.log_dialog.log_text.AppendText("Errore: fattura %s corrotta!\n" % invoice_id) - parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) - wx.Yield() - invoice["good"] = False - else: - downloaded_count += 1 - if parent.verbose: - parent.log_dialog.log_text.AppendText("%d/%d scaricata fattura %s in %s\n" % (downloaded_count, invoices_count, invoice_id, invoice["path"])) + try: + resp = parent.session.get(invoice["url"]) + if resp.status_code == 200: + with open(tmp_dir+"/"+invoice_id+".pdf", "wb") as output_file: + output_file.write(resp.content) + invoice["path"] = output_file.name + try: + PyPDF2.PdfFileReader(open(invoice["path"], "rb")) + except (PyPDF2.utils.PdfReadError, OSError): + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) + parent.log_dialog.log_text.AppendText("Errore: fattura %s corrotta!\n" % invoice_id) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) wx.Yield() - invoice["good"] = True - else: + invoice["good"] = False + else: + downloaded_count += 1 + if parent.verbose: + parent.log_dialog.log_text.AppendText("%d/%d scaricata fattura %s in %s\n" % (downloaded_count, invoices_count, invoice_id, invoice["path"])) + wx.Yield() + invoice["good"] = True + else: + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) + parent.log_dialog.log_text.AppendText("Errore: impossibile scaricare fattura %s: %d\n" % (invoice_id, resp.status_code)) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) + wx.Yield() + invoice["good"] = False + except requests.exceptions.RequestException: parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) - parent.log_dialog.log_text.AppendText("Errore: impossibile scaricare fattura %s: %d\n" % (invoice_id, resp.status_code)) + parent.log_dialog.log_text.AppendText("Errore: impossibile scaricare fattura %s: errore di connessione\n" % invoice_id) parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) wx.Yield() invoice["good"] = False diff --git a/fatture_ccsr/fatture_ccsr.py b/fatture_ccsr/fatture_ccsr.py index 7594b2a..451c259 100644 --- a/fatture_ccsr/fatture_ccsr.py +++ b/fatture_ccsr/fatture_ccsr.py @@ -5,6 +5,7 @@ import sys import getopt import subprocess import atexit +from requests.models import RequestField import wx import wx.adv import requests @@ -284,13 +285,17 @@ class LoginDialog(wx.Dialog): if self.username.GetValue() not in ("", None) and self.password.GetValue() not in ("", None): session = self.GetParent().session session.auth = requests_ntlm.HttpNtlmAuth("sr\\"+self.username.GetValue(), self.password.GetValue()) - if session.get('https://report.casadicurasanrossore.it:8443/Reports/browse/').status_code == 200: - self.logged_in = True - self.username.SetValue('') - self.password.SetValue('') - self.Close() - else: - self.error_lbl.SetLabel("Errore: credenziali errate") + try: + login = session.get('https://report.casadicurasanrossore.it:8443/Reports/browse/') + if login.status_code == 200: + self.logged_in = True + self.username.SetValue('') + self.password.SetValue('') + self.Close() + else: + self.error_lbl.SetLabel("Errore: credenziali errate") + except requests.exceptions.RequestException: + self.error_lbl.SetLabel("Errore: impossibile connettersi") else: self.error_lbl.SetLabel("Errore: credenziali mancanti") diff --git a/fatture_ccsr/traf2000_converter.py b/fatture_ccsr/traf2000_converter.py index d713711..ae1d776 100755 --- a/fatture_ccsr/traf2000_converter.py +++ b/fatture_ccsr/traf2000_converter.py @@ -7,16 +7,24 @@ import tempfile import lxml.etree import unidecode import wx +import requests def download_input_file(parent): """download input file from CCSR SSRS web service""" start_date = parent.start_date_picker.GetValue().Format("%d/%m/%Y") end_date = parent.end_date_picker.GetValue().Format("%d/%m/%Y") input_file_url = 'https://report.casadicurasanrossore.it:8443/reportserver?/STAT_FATTURATO_CTERZI&dataI='+start_date+'&dataF='+end_date+'&rs:Format=XML' - downloaded_input_file = parent.session.get(input_file_url) + try: + downloaded_input_file = parent.session.get(input_file_url) + except requests.exceptions.RequestException: + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) + parent.log_dialog.log_text.AppendText("ERRORE: impossibile connettersi al portale CCSR.") + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) + wx.Yield() + return None if downloaded_input_file.status_code != 200: parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) - parent.log_dialog.log_text.AppendText("ERRORE: impossibile scaricare il file di input.\nControllare la connessione ad internet e l'operatività del portale CCSR. Code %d\n" % downloaded_input_file.status_code) + parent.log_dialog.log_text.AppendText("ERRORE: impossibile scaricare il file di input.\nCode %d\n" % downloaded_input_file.status_code) parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) wx.Yield() return None