|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | + |
| 8 | +def download_backup(url_arquivo: str, caminho_salvar: str) -> None: |
| 9 | + """ |
| 10 | + Baixa um arquivo JSON grande usando chunks para economia de memória. |
| 11 | +
|
| 12 | + Args: |
| 13 | + url_arquivo: URL do arquivo JSON |
| 14 | + caminho_salvar: Caminho onde salvar o arquivo |
| 15 | + """ |
| 16 | + # Configurações para controle de memória |
| 17 | + tamanho_chunk = 1024 * 1024 # 1MB por chunk |
| 18 | + |
| 19 | + try: |
| 20 | + resposta = requests.get(url_arquivo, stream=True) |
| 21 | + |
| 22 | + tamanho_total = int(resposta.headers.get("content-length", 0)) |
| 23 | + |
| 24 | + caminho_arquivo = Path(caminho_salvar) |
| 25 | + caminho_arquivo.parent.mkdir(parents=True, exist_ok=True) |
| 26 | + |
| 27 | + with open(caminho_salvar, "wb") as arquivo: |
| 28 | + bytes_baixados = 0 |
| 29 | + |
| 30 | + for chunk in resposta.iter_content(chunk_size=tamanho_chunk): |
| 31 | + if chunk: |
| 32 | + arquivo.write(chunk) |
| 33 | + bytes_baixados += len(chunk) |
| 34 | + |
| 35 | + print( |
| 36 | + f"\rBaixando: {bytes_baixados/tamanho_total*100:.2f}%", end="", flush=True |
| 37 | + ) |
| 38 | + |
| 39 | + print("\n\nDownload concluído!") |
| 40 | + |
| 41 | + except Exception as e: |
| 42 | + print(f"Erro ao baixar o arquivo: {str(e)}") |
| 43 | + |
| 44 | + |
| 45 | +def remove_connection_accounts(path: str) -> None: |
| 46 | + """ |
| 47 | + Remove qualquer conexão com que o JSON gerado do APP v1 tenha com account |
| 48 | +
|
| 49 | + Args: |
| 50 | + path: Caminho para o path JSON |
| 51 | + """ |
| 52 | + |
| 53 | + try: |
| 54 | + # Ler o path |
| 55 | + with open(path, "r", encoding="utf-8") as json_file: |
| 56 | + dados = json.load(json_file) |
| 57 | + |
| 58 | + # Processar cada registro |
| 59 | + for registro in dados: |
| 60 | + if registro.get("model") == "v1.table": |
| 61 | + registro.setdefault("fields", {}).update( |
| 62 | + {"published_by": [], "data_cleaned_by": []} |
| 63 | + ) |
| 64 | + elif registro.get("model") == "v1.informationrequest": |
| 65 | + registro.setdefault("fields", {})["started_by"] = None |
| 66 | + |
| 67 | + # Salvar alterações |
| 68 | + with open(path, "w", encoding="utf-8") as f: |
| 69 | + json.dump(dados, f, indent=2, ensure_ascii=False) |
| 70 | + |
| 71 | + except FileNotFoundError: |
| 72 | + print(f"Erro: path '{path}' não encontrado") |
| 73 | + except json.JSONDecodeError: |
| 74 | + print(f"Erro: path '{path}' contém JSON inválido") |
| 75 | + except Exception as e: |
| 76 | + print(f"Erro inesperado ao processar '{path}': {str(e)}") |
0 commit comments