Spaces:
Running
Running
Update app.py
Browse filesModifiquei a função exportar_historico_csv para escrever o arquivo CSV diretamente com encoding='utf-8' e newline='', o que garante a formatação correta de todos os caracteres especiais e emojis
app.py
CHANGED
|
@@ -161,6 +161,10 @@ analytics = {}
|
|
| 161 |
CACHE_DIR = Path("post_cache")
|
| 162 |
CACHE_DIR.mkdir(exist_ok=True)
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# ============================================
|
| 166 |
# FUNÇÕES DE PERSISTÊNCIA (FIREBASE)
|
|
@@ -651,38 +655,37 @@ def exportar_historico_csv():
|
|
| 651 |
print("Nenhum histórico para exportar.")
|
| 652 |
return None
|
| 653 |
|
| 654 |
-
|
| 655 |
-
# Usar io.StringIO para criar um arquivo em memória
|
| 656 |
-
output = io.StringIO()
|
| 657 |
-
writer = csv.writer(output)
|
| 658 |
-
|
| 659 |
-
# Cabeçalhos
|
| 660 |
-
headers = ["DataHora", "Tema", "Nicho", "Estilo", "Formato", "Favorito", "Status", "Palavras", "Caracteres", "Hashtags", "Texto"]
|
| 661 |
-
writer.writerow(headers)
|
| 662 |
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
post.get("DataHora", ""),
|
| 668 |
-
post.get("Tema", ""),
|
| 669 |
-
post.get("Nicho", ""),
|
| 670 |
-
post.get("Estilo", ""),
|
| 671 |
-
post.get("Formato", ""),
|
| 672 |
-
post.get("Favorito", False),
|
| 673 |
-
post.get("Status", ""),
|
| 674 |
-
stats.get("palavras", 0),
|
| 675 |
-
stats.get("caracteres", 0),
|
| 676 |
-
stats.get("hashtags", 0),
|
| 677 |
-
post.get("Texto", "")
|
| 678 |
-
]
|
| 679 |
-
writer.writerow(row)
|
| 680 |
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 686 |
|
| 687 |
except Exception as e:
|
| 688 |
print(f"❌ Erro ao exportar CSV: {e}")
|
|
@@ -924,12 +927,11 @@ def preparar_download_zip(texto, imagem_pil):
|
|
| 924 |
if not texto and not imagem_pil:
|
| 925 |
print("Nada para baixar.")
|
| 926 |
return None
|
|
|
|
|
|
|
| 927 |
|
| 928 |
try:
|
| 929 |
-
|
| 930 |
-
zip_buffer = io.BytesIO()
|
| 931 |
-
|
| 932 |
-
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf:
|
| 933 |
# 1. Adicionar o texto (garantindo UTF-8)
|
| 934 |
if texto:
|
| 935 |
zf.writestr("post.txt", texto.encode('utf-8'))
|
|
@@ -943,14 +945,8 @@ def preparar_download_zip(texto, imagem_pil):
|
|
| 943 |
img_buffer.seek(0)
|
| 944 |
zf.writestr("imagem.png", img_buffer.getvalue())
|
| 945 |
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
# Salvar o ZIP em um arquivo temporário
|
| 950 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as f:
|
| 951 |
-
f.write(zip_buffer.getvalue())
|
| 952 |
-
print(f"Arquivo ZIP temporário salvo em: {f.name}")
|
| 953 |
-
return f.name
|
| 954 |
|
| 955 |
except Exception as e:
|
| 956 |
print(f"❌ Erro ao criar arquivo ZIP: {e}")
|
|
|
|
| 161 |
CACHE_DIR = Path("post_cache")
|
| 162 |
CACHE_DIR.mkdir(exist_ok=True)
|
| 163 |
|
| 164 |
+
# Nomes de arquivo padrão para download
|
| 165 |
+
CSV_FILENAME = "posthistpeacechatbot001.csv"
|
| 166 |
+
ZIP_FILENAME = "postpeacechatbot001.zip"
|
| 167 |
+
|
| 168 |
|
| 169 |
# ============================================
|
| 170 |
# FUNÇÕES DE PERSISTÊNCIA (FIREBASE)
|
|
|
|
| 655 |
print("Nenhum histórico para exportar.")
|
| 656 |
return None
|
| 657 |
|
| 658 |
+
filepath = CACHE_DIR / CSV_FILENAME
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 659 |
|
| 660 |
+
try:
|
| 661 |
+
# Escrever diretamente no arquivo com encoding UTF-8
|
| 662 |
+
with open(filepath, mode='w', encoding='utf-8', newline='') as f:
|
| 663 |
+
writer = csv.writer(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
|
| 665 |
+
# Cabeçalhos
|
| 666 |
+
headers = ["DataHora", "Tema", "Nicho", "Estilo", "Formato", "Favorito", "Status", "Palavras", "Caracteres", "Hashtags", "Texto"]
|
| 667 |
+
writer.writerow(headers)
|
| 668 |
+
|
| 669 |
+
# Escrever linhas
|
| 670 |
+
for post in post_history:
|
| 671 |
+
stats = post.get("Stats", {})
|
| 672 |
+
row = [
|
| 673 |
+
post.get("DataHora", ""),
|
| 674 |
+
post.get("Tema", ""),
|
| 675 |
+
post.get("Nicho", ""),
|
| 676 |
+
post.get("Estilo", ""),
|
| 677 |
+
post.get("Formato", ""),
|
| 678 |
+
post.get("Favorito", False),
|
| 679 |
+
post.get("Status", ""),
|
| 680 |
+
stats.get("palavras", 0),
|
| 681 |
+
stats.get("caracteres", 0),
|
| 682 |
+
stats.get("hashtags", 0),
|
| 683 |
+
post.get("Texto", "")
|
| 684 |
+
]
|
| 685 |
+
writer.writerow(row)
|
| 686 |
+
|
| 687 |
+
print(f"Arquivo CSV salvo em: {filepath}")
|
| 688 |
+
return str(filepath) # Retorna o caminho estático
|
| 689 |
|
| 690 |
except Exception as e:
|
| 691 |
print(f"❌ Erro ao exportar CSV: {e}")
|
|
|
|
| 927 |
if not texto and not imagem_pil:
|
| 928 |
print("Nada para baixar.")
|
| 929 |
return None
|
| 930 |
+
|
| 931 |
+
filepath = CACHE_DIR / ZIP_FILENAME
|
| 932 |
|
| 933 |
try:
|
| 934 |
+
with zipfile.ZipFile(filepath, 'w', zipfile.ZIP_DEFLATED) as zf:
|
|
|
|
|
|
|
|
|
|
| 935 |
# 1. Adicionar o texto (garantindo UTF-8)
|
| 936 |
if texto:
|
| 937 |
zf.writestr("post.txt", texto.encode('utf-8'))
|
|
|
|
| 945 |
img_buffer.seek(0)
|
| 946 |
zf.writestr("imagem.png", img_buffer.getvalue())
|
| 947 |
|
| 948 |
+
print(f"Arquivo ZIP salvo em: {filepath}")
|
| 949 |
+
return str(filepath) # Retorna o caminho estático
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 950 |
|
| 951 |
except Exception as e:
|
| 952 |
print(f"❌ Erro ao criar arquivo ZIP: {e}")
|