diff --git a/extracao_vendashora_rgb.py b/extracao_vendashora_rgb.py index 536e1d8..c834c74 100644 --- a/extracao_vendashora_rgb.py +++ b/extracao_vendashora_rgb.py @@ -412,17 +412,35 @@ def main(): end_time = time.time() + 180 # até 3 minutos downloaded_file = None while time.time() < end_time: - # Esperar terminar downloads parciais - if any(name.endswith('.crdownload') for name in os.listdir(download_dir)): + # Esperar terminar downloads parciais (.crdownload ou arquivos temporários do Chrome) + current_dir_files = os.listdir(download_dir) + if any(name.endswith('.crdownload') for name in current_dir_files): time.sleep(1) continue - current_files = set(f for f in os.listdir(download_dir) if not f.endswith('.crdownload')) + + # Filtrar arquivos válidos: + # - Não termina com .crdownload + # - Não começa com . (arquivos temporários/ocultos do Chrome) + # - Não contém .com.google.Chrome (arquivos temporários específicos) + def is_valid_file(filename): + if filename.endswith('.crdownload'): + return False + if filename.startswith('.'): + return False + if '.com.google.Chrome' in filename: + return False + return True + + current_files = set(f for f in current_dir_files if is_valid_file(f)) new_files = [f for f in current_files - before_files] + if new_files: # Escolher o mais recente candidate = max((os.path.join(download_dir, f) for f in new_files), key=os.path.getmtime) - downloaded_file = candidate - break + # Verificar se o arquivo realmente existe e tem tamanho > 0 + if os.path.exists(candidate) and os.path.getsize(candidate) > 0: + downloaded_file = candidate + break time.sleep(1) def _normalize_label(s: str) -> str: @@ -685,14 +703,14 @@ def main(): print("Script finalizado.") def run_with_retry(max_retries=3, retry_delay=120): - """Executa main() com retentativas em caso de erro de timeout/conexão.""" + """Executa main() com retentativas em caso de erro de timeout/conexão/arquivo.""" for attempt in range(1, max_retries + 1): try: main() return True # Sucesso except Exception as e: error_str = str(e).lower() - # Verificar se é erro de timeout/conexão que vale retry + # Verificar se é erro de timeout/conexão/arquivo temporário que vale retry is_retryable = any(keyword in error_str for keyword in [ 'read timed out', 'timed out', @@ -700,7 +718,10 @@ def run_with_retry(max_retries=3, retry_delay=120): 'connectionpool', 'connection refused', 'connection reset', - 'httpconnectionpool' + 'httpconnectionpool', + 'no such file or directory', + '.com.google.chrome', + 'filenotfounderror' ]) if is_retryable and attempt < max_retries: