@danhachuel/thunderbolt 0.2.40 → 0.2.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MANUAL-INSTALACAO.md +13 -5
- package/README.md +7 -7
- package/app/main.py +170 -1
- package/hermes_ui/domain.py +3 -0
- package/hermes_ui/storage.py +2 -0
- package/integrations/youtube_batch.py +185 -0
- package/package.json +1 -1
package/MANUAL-INSTALACAO.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Este manual descreve a instalação local da UI Thunderbolt, baseada no MoneyPrinterTurbo, utilizando o pacote npm `@danhachuel/thunderbolt`. O fluxo recomendado instala automaticamente o ambiente Python, as dependências da aplicação, as dependências do MoneyPrinterTurbo, o Streamlit e o suporte FFmpeg através de `imageio-ffmpeg`.
|
|
4
4
|
|
|
5
|
-
> **Versão deste manual:** 0.2.
|
|
5
|
+
> **Versão deste manual:** 0.2.41
|
|
6
6
|
> **Pacote npm:** `@danhachuel/thunderbolt`
|
|
7
7
|
> **Porta padrão da UI:** `localhost:3030`
|
|
8
8
|
> **Repositório:** [github.com/DanHachuel/thunderbolt](https://github.com/DanHachuel/thunderbolt)
|
|
@@ -100,13 +100,13 @@ Execute:
|
|
|
100
100
|
Windows PowerShell ou MobaXterm:
|
|
101
101
|
|
|
102
102
|
```powershell
|
|
103
|
-
npx.cmd --yes @danhachuel/thunderbolt@0.2.
|
|
103
|
+
npx.cmd --yes @danhachuel/thunderbolt@0.2.41 install
|
|
104
104
|
```
|
|
105
105
|
|
|
106
106
|
Linux/macOS:
|
|
107
107
|
|
|
108
108
|
```bash
|
|
109
|
-
npx --yes @danhachuel/thunderbolt@0.2.
|
|
109
|
+
npx --yes @danhachuel/thunderbolt@0.2.41 install
|
|
110
110
|
```
|
|
111
111
|
|
|
112
112
|
A instalação normal é **segura para actualizações**: preserva `storage`, Blueprints, Brandings, configurações e artefactos do utilizador. Remove apenas `.venv`, o clone técnico do MoneyPrinterTurbo e dependências que serão recriadas. Uma pasta antiga sem dados do utilizador, como `C:\Users\<utilizador>\AppData\Local\hermes` da tentativa incompleta, pode ser removida; uma pasta antiga que contenha Blueprints, Brandings ou storage é preservada e apenas avisada no terminal. Feche processos Python, Node, Streamlit e MobaXterm que estejam a usar as pastas antes de executar.
|
|
@@ -379,7 +379,7 @@ Após iniciar a aplicação, valide o seguinte percurso:
|
|
|
379
379
|
7. **Criação de Vídeos > Vídeos:** verifique o estado `to_do` e os botões **Iniciar** e **Parar** dentro da subaba. Confirme também que **Criação de Músicas** mostra o mesmo fluxo com título próprio.
|
|
380
380
|
8. **Upload:** configure o OAuth Client ID e Secret, autorize primeiro o **youtube-automation-agent** na própria aba, confirme o estado **pronto para publicar**, preencha título/descrição/tags e publique um MP4 real. O botão **Autorizar fallback OAuth** existe apenas para redundância; a Data API Key, se configurada, é exclusivamente para consultas oficiais públicas e nunca substitui OAuth.
|
|
381
381
|
9. **Edição > Limpador de Metadados:** suba um vídeo de terceiro, preencha título, preview, links, timestamps e tags, aplique a limpeza e descarregue a cópia limpa e o manifesto JSON. O original fica preservado e vídeos das páginas de criação não são aceites nesta área.
|
|
382
|
-
10. **Configurações > Configurações Técnicas:** confirme que os caminhos e credenciais estão locais e não aparecem no Git.
|
|
382
|
+
10. **Configurações > Configurações Técnicas:** confirme que os caminhos e credenciais estão locais e não aparecem no Git. Na secção **Contas Google/YouTube — canais em lote**, adicione cada conta com o seu e-mail/Gmail, OAuth Client ID e OAuth Client Secret próprios; o e-mail identifica a conta e não é usado para ler a caixa Gmail.
|
|
383
383
|
11. **Criação de Vídeos:** confirme que `Pexels/Pixabay` substitui o label antigo, que `Estilo IA` aparece apenas em `full_ia` e que `Apenas Música` exige áudio e guarda `background_mode=none`.
|
|
384
384
|
12. **Automação:** no cartão de cada canal, escolha o **Blueprint padrão** e a **Voz padrão**, clique em `Guardar` e confirme que o resumo do cartão se actualiza. Configure também **Automação ON** e um horário `HH:MM`; confirme a lista de vídeos cadastrados. Os mesmos defaults aparecem em **Canais** e são usados em novas tarefas. Inicie o Thunderbolt pelo launcher, confirme o aviso verde **Worker activo** e verifique que o relógio apresentado corresponde ao computador. O worker cria no máximo um lote por canal por dia quando o horário local coincide.
|
|
385
385
|
13. **Configurações > Configurações Técnicas > Teste de vozes:** teste Edge/Azure ou provider configurado e confirme reprodução/download sem criação de tarefa.
|
|
@@ -388,6 +388,14 @@ Após iniciar a aplicação, valide o seguinte percurso:
|
|
|
388
388
|
16. **Configurações > MCP > Servidor MCP:** abra a subaba, mantenha o host `127.0.0.1`, active **Servidor MCP ON** e clique em **Guardar e iniciar Servidor MCP**. Confirme o endpoint `/mcp` e o health endpoint `/health`. Mantenha **Permitir ferramentas de escrita** desactivado até precisar que um agente crie lotes.
|
|
389
389
|
17. **Configurações > MCP > Skill:** clique em **Guardar skill localmente** e confirme o ficheiro em `storage/skills/moneyprinterturbo-video.md`; opcionalmente use **Descarregar skill .md** para obter a cópia através do navegador.
|
|
390
390
|
|
|
391
|
+
## Canais em lote por conta Google/YouTube
|
|
392
|
+
|
|
393
|
+
A subaba **Canais em lote gmail**, dentro de **Canais**, não lê e-mails, mensagens, contactos ou a caixa Gmail. O nome identifica a conta Google/Gmail que gere os canais YouTube. Configure cada conta em **Configurações > Configurações Técnicas > Contas Google/YouTube — canais em lote**, preenchendo e-mail, OAuth Client ID e OAuth Client Secret.
|
|
394
|
+
|
|
395
|
+
Na subaba, seleccione a conta e clique em **Autorizar conta Google**. A autorização abre o browser do sistema e guarda um refresh token separado para essa conta. Depois clique em **Listar canais desta conta**. O Thunderbolt chama a YouTube Data API com `channels.list`, `mine=true`, `part=snippet,contentDetails,statistics`, suporta páginas sucessivas e apresenta os canais encontrados para selecção.
|
|
396
|
+
|
|
397
|
+
Escolha os canais e os defaults de Blueprint, voz, idioma e estilo wide, depois clique em **Cadastrar canais seleccionados**. A importação usa `youtube_channel_id` para não duplicar canais já cadastrados, preserva os registos existentes e identifica a origem como `youtube_data_api_oauth_mine`. A API Key global não substitui OAuth neste fluxo; permanece reservada para consultas públicas/métricas.
|
|
398
|
+
|
|
391
399
|
## Niche Finder Kaggle e Niche Finder Apify
|
|
392
400
|
|
|
393
401
|
A página **Niche Finder Kaggle** integra a lógica adaptada do projecto open source [johanfortus/Niche-Finder](https://github.com/johanfortus/Niche-Finder), cujo projecto original usa K-Means e FP-Growth sobre o dataset público [Trending Youtube Video Statistics (113 Countries)](https://www.kaggle.com/datasets/asaniczka/trending-youtube-videos-113-countries). No Thunderbolt não existe Flask, rota HTTP adicional, template HTML, JavaScript D3 ou segundo processo; toda a análise é síncrona no Streamlit.
|
|
@@ -396,7 +404,7 @@ Ao abrir a página, o Thunderbolt não prepara dados públicos, não descarrega
|
|
|
396
404
|
|
|
397
405
|
Os parâmetros da UI são número de clusters entre 2 e 10, suporte mínimo entre 0,01 e 0,50, país, engagement, intervalo de datas e tags, todos dentro da área principal da aba. O núcleo normaliza os dados, calcula engagement, aplica filtros, faz transformação logarítmica e standardização, executa K-Means e calcula itemsets/regras com FP-Growth. Não são apresentados resultados até ao primeiro clique em **Analisar Nichos**; o mesmo botão aplica alterações posteriores aos filtros. Os resultados são DataFrames de clusters, itemsets frequentes, regras de associação e dados analisados; o gráfico de dispersão é criado nativamente com Plotly.
|
|
398
406
|
|
|
399
|
-
As dependências adicionais — `scikit-learn`, `mlxtend`, `plotly`, `seaborn`, `matplotlib` e `kagglehub` — são instaladas pelo procedimento normal de `npx`. Em instalações existentes, execute novamente `npx.cmd --yes @danhachuel/thunderbolt@0.2.
|
|
407
|
+
As dependências adicionais — `scikit-learn`, `mlxtend`, `plotly`, `seaborn`, `matplotlib` e `kagglehub` — são instaladas pelo procedimento normal de `npx`. Em instalações existentes, execute novamente `npx.cmd --yes @danhachuel/thunderbolt@0.2.41 install`; o instalador detecta e reutiliza o que já estiver válido.
|
|
400
408
|
|
|
401
409
|
### Niche Finder Apify
|
|
402
410
|
|
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ A primeira versão implementa a camada UI independente com:
|
|
|
14
14
|
| Pipeline | Menu expansível com Criação de Vídeos, Criação de Músicas e Upload |
|
|
15
15
|
| Blueprints | Leitura da pasta `storage/blueprints/`, upload/validação de JSON e criação a partir de link YouTube |
|
|
16
16
|
| Brandings | Subaba própria dentro de Blueprints, upload/listagem de Brandings e criação conjunta com Blueprint |
|
|
17
|
-
| Canais | Subabas de importação pública sem API Key,
|
|
17
|
+
| Canais | Subabas de importação pública sem API Key, Canais em lote gmail por conta Google/YouTube via OAuth e cadastro manual independente |
|
|
18
18
|
| Criação de Vídeos / Criação de Músicas | Subabas Criar vídeo e Vídeos; lotes; 51 rótulos de idioma; Pexels/Pixabay, full IA com Estilo IA e Apenas Música com agente musical; a segunda página reutiliza o mesmo fluxo com título próprio |
|
|
19
19
|
| Automação | Lista de vídeos e canais, selectores editáveis de Blueprint/voz padrão, Automação ON, horário diário HH:MM e worker local baseado no relógio do computador |
|
|
20
20
|
| Models AI | Menu expansível com Personagens e Redes Sociais reservados para desenvolvimento futuro |
|
|
@@ -22,12 +22,12 @@ A primeira versão implementa a camada UI independente com:
|
|
|
22
22
|
| Edição | Menu expansível abaixo de Automação com Limpador de Metadados funcional, Cortes reservado e Editor Python inspirado no PYEdit para vídeos e scripts locais |
|
|
23
23
|
| Upload | YouTube via `youtube-automation-agent` adaptado internamente, OAuth directo de redundância, Upload directo experimental, TikTok, Instagram e Facebook Pages no front end |
|
|
24
24
|
| MCP | Catálogo local opcional de Short Video Maker, AutoVio, OpenMontage e OpenCut, com portas editáveis e activação |
|
|
25
|
-
| Configurações Técnicas | Provedores LLM, TTS/voz, preview de vozes, Suno, materiais, Whisper, FFmpeg, OAuth YouTube, Data API Key opcional, Kaggle Username/API Key, Apify API Token/Actor ID/limites, Upload directo, TikTok Client ID/Secret e Upload-Post |
|
|
25
|
+
| Configurações Técnicas | Provedores LLM, TTS/voz, preview de vozes, Suno, materiais, Whisper, FFmpeg, OAuth YouTube, contas Google/YouTube para canais em lote com e-mail, Client ID e Client Secret por conta, Data API Key opcional, Kaggle Username/API Key, Apify API Token/Actor ID/limites, Upload directo, TikTok Client ID/Secret e Upload-Post |
|
|
26
26
|
| Launcher | Execução via `npx`, instalação assistida, diagnóstico e preparação para distribuição |
|
|
27
27
|
|
|
28
28
|
Os adaptadores do MoneyPrinterTurbo e de publicação nas plataformas são ligados pelas configurações locais e pelos pontos de integração em `integrations/`. A UI não inventa dados quando um serviço externo ou credencial não está disponível.
|
|
29
29
|
|
|
30
|
-
## Navegação da UI 0.2.
|
|
30
|
+
## Navegação da UI 0.2.41
|
|
31
31
|
|
|
32
32
|
A barra lateral mantém os níveis principais, nesta ordem: **Início**, **Niche Finder**, **Pipeline**, **Automação**, **Edição**, **Models AI** e **Configurações**. **Pipeline** é expansível e contém **Criação de Vídeos**, **Criação de Músicas** e **Upload**. **Edição** é expansível e contém **Limpador de Metadados**, **Cortes** e **Editor Python**, nessa ordem. **Models AI** é expansível e contém **Personagens** e **Redes Sociais**, nessa ordem. **Niche Finder** é expansível e contém **Niche Finder Kaggle** e **Niche Finder Apify**. **Configurações** é expansível e contém **Canais**, **Blueprints**, **MCP** e **Configurações Técnicas**. O Início reúne o dashboard e as filas do Pipeline, sem botões de acções rápidas.
|
|
33
33
|
|
|
@@ -116,9 +116,9 @@ thunderbolt
|
|
|
116
116
|
No Windows PowerShell, se `npx` for bloqueado por `npx.ps1`, use directamente `npx.cmd`:
|
|
117
117
|
|
|
118
118
|
```powershell
|
|
119
|
-
npx.cmd --yes @danhachuel/thunderbolt@0.2.
|
|
120
|
-
npx.cmd --yes @danhachuel/thunderbolt@0.2.
|
|
121
|
-
npx.cmd --yes @danhachuel/thunderbolt@0.2.
|
|
119
|
+
npx.cmd --yes @danhachuel/thunderbolt@0.2.41 install
|
|
120
|
+
npx.cmd --yes @danhachuel/thunderbolt@0.2.41 doctor
|
|
121
|
+
npx.cmd --yes @danhachuel/thunderbolt@0.2.41
|
|
122
122
|
```
|
|
123
123
|
|
|
124
124
|
Como alternativa, pode permitir scripts para o seu utilizador:
|
|
@@ -142,7 +142,7 @@ O menu expansível **Niche Finder** contém duas alternativas independentes. **N
|
|
|
142
142
|
|
|
143
143
|
A interface apresenta dentro da aba os parâmetros da busca: número de clusters entre 2 e 10, suporte mínimo entre 0,01 e 0,50, país, categoria de engagement, intervalo de datas e tags. A página permanece sem resultados até o primeiro clique em **Analisar Nichos**; depois, se os parâmetros forem alterados, mostra os resultados anteriores e pede novo clique para aplicar os filtros actuais. O núcleo aplica normalização, filtros, `log1p`, `StandardScaler`, K-Means e FP-Growth. Os resultados aparecem em DataFrames para clusters, itemsets frequentes, regras de associação e dados analisados, acompanhados por uma visualização Plotly nativa e pesquisa de palavras nos clusters.
|
|
144
144
|
|
|
145
|
-
**Niche Finder Apify** é a segunda alternativa e não usa o dataset, filtros, execução ou resultados Kaggle. Define três palavras-chave, período, limite de resultados, Shorts, duração, idioma de legendas e ordenação; depois de clicar em **Pesquisar no Apify**, inicia o actor `streamers~youtube-scraper`, acompanha o run, carrega o dataset, normaliza vídeos, limpa SRT, calcula VSC Ratio e tenta resumir as transcrições com o provider LLM configurado. Os resultados ficam na sessão própria `niche_apify_results`, o histórico pequeno fica em `storage/state/niche_apify_runs.json` e existem exportações JSON/CSV. Configure o **Apify API Token** em Configurações Técnicas. As dependências adicionais são instaladas pelo fluxo normal do pacote: `requests`, `pandas` e os componentes já existentes da análise. Em instalações existentes, execute novamente `npx.cmd --yes @danhachuel/thunderbolt@0.2.
|
|
145
|
+
**Niche Finder Apify** é a segunda alternativa e não usa o dataset, filtros, execução ou resultados Kaggle. Define três palavras-chave, período, limite de resultados, Shorts, duração, idioma de legendas e ordenação; depois de clicar em **Pesquisar no Apify**, inicia o actor `streamers~youtube-scraper`, acompanha o run, carrega o dataset, normaliza vídeos, limpa SRT, calcula VSC Ratio e tenta resumir as transcrições com o provider LLM configurado. Os resultados ficam na sessão própria `niche_apify_results`, o histórico pequeno fica em `storage/state/niche_apify_runs.json` e existem exportações JSON/CSV. Configure o **Apify API Token** em Configurações Técnicas. As dependências adicionais são instaladas pelo fluxo normal do pacote: `requests`, `pandas` e os componentes já existentes da análise. Em instalações existentes, execute novamente `npx.cmd --yes @danhachuel/thunderbolt@0.2.41 install`; o instalador detecta e reutiliza componentes já válidos.
|
|
146
146
|
|
|
147
147
|
## Editor Python baseado no PYEdit
|
|
148
148
|
|
package/app/main.py
CHANGED
|
@@ -4,6 +4,7 @@ import json
|
|
|
4
4
|
import re
|
|
5
5
|
from datetime import date, datetime
|
|
6
6
|
import sys
|
|
7
|
+
import uuid
|
|
7
8
|
from pathlib import Path
|
|
8
9
|
|
|
9
10
|
import requests
|
|
@@ -33,6 +34,7 @@ from hermes_ui.music import list_music_files, materialize_suno_audio, request_su
|
|
|
33
34
|
from hermes_ui.voice_preview import DEFAULT_SAMPLE, load_preview_file, synthesize_preview
|
|
34
35
|
from integrations.platforms import IntegrationResult, TikTokAdapter, YouTubeAdapter
|
|
35
36
|
from integrations.youtube_direct_upload import YouTubeDirectUploader
|
|
37
|
+
from integrations.youtube_batch import account_key as youtube_batch_account_key, account_status as youtube_batch_account_status, authorize_account as authorize_youtube_batch_account, delete_account_token as delete_youtube_batch_token, list_my_channels as list_youtube_batch_channels
|
|
36
38
|
from integrations.local_runtime import MoneyPrinterRuntime
|
|
37
39
|
from integrations.moneyprinter_config import sync_moneyprinter_config
|
|
38
40
|
|
|
@@ -339,7 +341,7 @@ def render_channels():
|
|
|
339
341
|
st.caption("Escolha entre importar dados públicos do YouTube ou preencher o canal manualmente.")
|
|
340
342
|
settings = read_json("settings.json", {})
|
|
341
343
|
youtube = YouTubeAdapter(settings=settings)
|
|
342
|
-
import_tab, manual_tab = st.tabs(["Importar do YouTube", "Cadastro manual"])
|
|
344
|
+
import_tab, batch_tab, manual_tab = st.tabs(["Importar do YouTube", "Canais em lote gmail", "Cadastro manual"])
|
|
343
345
|
|
|
344
346
|
with import_tab:
|
|
345
347
|
st.caption("A pesquisa pública funciona sem API Key. A Data API é opcional e fica disponível numa opção separada para métricas oficiais.")
|
|
@@ -423,6 +425,96 @@ def render_channels():
|
|
|
423
425
|
else:
|
|
424
426
|
st.info("Introduza um URL, handle ou ID e clique em Buscar no YouTube. Não é necessária API Key na opção pública.")
|
|
425
427
|
|
|
428
|
+
with batch_tab:
|
|
429
|
+
st.caption("Esta subaba usa a conta Google/YouTube seleccionada para listar os canais que ela gere. Não lê a caixa Gmail e não usa e-mails como pesquisa pública.")
|
|
430
|
+
accounts = [account for account in settings.get("youtube_batch_accounts", []) if isinstance(account, dict) and account.get("id")]
|
|
431
|
+
if not accounts:
|
|
432
|
+
st.info("Configure primeiro uma conta em Configurações > Configurações Técnicas > Contas Google/YouTube para canais em lote.")
|
|
433
|
+
else:
|
|
434
|
+
account_ids = [str(account["id"]) for account in accounts]
|
|
435
|
+
account_labels = {str(account["id"]): f"{account.get('email', 'Conta Google')} — {account.get('label', 'Canais YouTube')}" for account in accounts}
|
|
436
|
+
default_account_id = str(settings.get("youtube_batch_selected_account_id") or account_ids[0])
|
|
437
|
+
if default_account_id not in account_ids:
|
|
438
|
+
default_account_id = account_ids[0]
|
|
439
|
+
selected_account_id = st.selectbox("Conta Google/YouTube", account_ids, index=account_ids.index(default_account_id), format_func=lambda item: account_labels.get(item, item), key="batch_selected_account_id")
|
|
440
|
+
selected_account = next(account for account in accounts if str(account["id"]) == selected_account_id)
|
|
441
|
+
account_status = youtube_batch_account_status(selected_account, STORAGE)
|
|
442
|
+
(st.success if account_status.ok else st.warning)(account_status.message)
|
|
443
|
+
auth_cols = st.columns([1, 2])
|
|
444
|
+
with auth_cols[0]:
|
|
445
|
+
if st.button("Autorizar conta Google", type="secondary", use_container_width=True, key="batch_authorize_account"):
|
|
446
|
+
result = authorize_youtube_batch_account(selected_account, STORAGE)
|
|
447
|
+
(st.success if result.ok else st.error)(result.message)
|
|
448
|
+
if result.ok:
|
|
449
|
+
st.rerun()
|
|
450
|
+
with auth_cols[1]:
|
|
451
|
+
st.caption("A autorização é feita no browser do sistema e fica guardada separadamente para esta conta.")
|
|
452
|
+
batch_key = f"youtube_batch_channels_{selected_account_id}"
|
|
453
|
+
if st.button("Listar canais desta conta", type="primary", use_container_width=True, key="batch_list_channels"):
|
|
454
|
+
result = list_youtube_batch_channels(selected_account, STORAGE)
|
|
455
|
+
st.session_state[batch_key] = result.data.get("channels", []) if result.ok else []
|
|
456
|
+
st.session_state[f"{batch_key}_message"] = result.message
|
|
457
|
+
st.session_state[f"{batch_key}_ok"] = result.ok
|
|
458
|
+
if st.session_state.get(f"{batch_key}_message"):
|
|
459
|
+
(st.success if st.session_state.get(f"{batch_key}_ok") else st.error)(st.session_state[f"{batch_key}_message"])
|
|
460
|
+
listed_channels = st.session_state.get(batch_key, [])
|
|
461
|
+
if listed_channels:
|
|
462
|
+
channel_by_id = {str(channel.get("youtube_channel_id")): channel for channel in listed_channels if channel.get("youtube_channel_id")}
|
|
463
|
+
channel_labels = {channel_id: f"{channel.get('name', channel_id)} — {channel.get('handle') or channel_id}" for channel_id, channel in channel_by_id.items()}
|
|
464
|
+
existing_channels = read_json("channels.json", [])
|
|
465
|
+
existing_ids = {str(channel.get("youtube_channel_id")) for channel in existing_channels if channel.get("youtube_channel_id")}
|
|
466
|
+
existing_count = sum(1 for channel_id in channel_by_id if channel_id in existing_ids)
|
|
467
|
+
st.caption(f"{len(listed_channels)} canal(is) listado(s); {existing_count} já cadastrado(s). A API devolveu a lista da conta autenticada, não uma caixa Gmail.")
|
|
468
|
+
blueprint_items = blueprint_catalog()
|
|
469
|
+
blueprint_ids = [item[0] for item in blueprint_items]
|
|
470
|
+
blueprint_labels = {item[0]: item[1] for item in blueprint_items}
|
|
471
|
+
voice_options = voice_catalog()
|
|
472
|
+
with st.form("batch_channel_import_form"):
|
|
473
|
+
selected_channel_ids = st.multiselect("Canais a cadastrar", list(channel_by_id), default=list(channel_by_id), format_func=lambda item: channel_labels.get(item, item), key="batch_channel_ids")
|
|
474
|
+
defaults_cols = st.columns(4)
|
|
475
|
+
with defaults_cols[0]:
|
|
476
|
+
batch_blueprint = st.selectbox("Blueprint padrão", blueprint_ids, format_func=lambda item: blueprint_labels.get(item, item or "Sem Blueprint padrão"), key="batch_channel_blueprint")
|
|
477
|
+
with defaults_cols[1]:
|
|
478
|
+
batch_voice = st.selectbox("Voz padrão", voice_options, format_func=lambda item: item or "Sem voz padrão", key="batch_channel_voice")
|
|
479
|
+
with defaults_cols[2]:
|
|
480
|
+
batch_language = st.selectbox("Idioma", ["Português", "English", "Español", "Français", "Deutsch"], key="batch_channel_language")
|
|
481
|
+
with defaults_cols[3]:
|
|
482
|
+
batch_style = st.selectbox("Estilo wide", ["Pexels/Pixabay", "full_ia", "Apenas Música"], key="batch_channel_style")
|
|
483
|
+
import_selected = st.form_submit_button("Cadastrar canais seleccionados", type="primary", use_container_width=True)
|
|
484
|
+
if import_selected:
|
|
485
|
+
created_names = []
|
|
486
|
+
skipped_names = []
|
|
487
|
+
failed_names = []
|
|
488
|
+
style_value = {"Pexels/Pixabay": "pexels", "full_ia": "full_ia", "Apenas Música": "music"}.get(batch_style, batch_style)
|
|
489
|
+
for channel_id in selected_channel_ids:
|
|
490
|
+
data = channel_by_id[channel_id]
|
|
491
|
+
if channel_id in existing_ids:
|
|
492
|
+
skipped_names.append(data.get("name", channel_id))
|
|
493
|
+
continue
|
|
494
|
+
try:
|
|
495
|
+
create_channel(data.get("name", channel_id), data.get("url", ""), {
|
|
496
|
+
**data,
|
|
497
|
+
"youtube_channel_id": channel_id,
|
|
498
|
+
"google_account_id": str(selected_account.get("id", "")),
|
|
499
|
+
"google_account_email": str(selected_account.get("email", "")),
|
|
500
|
+
"blueprint_id": batch_blueprint.strip(),
|
|
501
|
+
"default_blueprint_id": batch_blueprint.strip(),
|
|
502
|
+
"default_voice": batch_voice.strip(),
|
|
503
|
+
"voice": batch_voice.strip(),
|
|
504
|
+
"language": batch_language,
|
|
505
|
+
"style_wide": style_value,
|
|
506
|
+
"last_youtube_sync": now(),
|
|
507
|
+
})
|
|
508
|
+
created_names.append(data.get("name", channel_id))
|
|
509
|
+
except Exception as exc:
|
|
510
|
+
failed_names.append(f"{data.get('name', channel_id)}: {exc}")
|
|
511
|
+
if created_names:
|
|
512
|
+
st.success(f"Canais cadastrados: {', '.join(created_names)}")
|
|
513
|
+
if skipped_names:
|
|
514
|
+
st.info(f"Já existentes e não duplicados: {', '.join(skipped_names)}")
|
|
515
|
+
if failed_names:
|
|
516
|
+
st.error(f"Falhas: {'; '.join(failed_names)}")
|
|
517
|
+
|
|
426
518
|
with manual_tab:
|
|
427
519
|
st.caption("Este fluxo não consulta o YouTube e não exige API Key. Preencha os dados que quiser e guarde o canal localmente.")
|
|
428
520
|
with st.form("channel_manual_form"):
|
|
@@ -1287,6 +1379,83 @@ def render_settings():
|
|
|
1287
1379
|
key=f"settings_{key}",
|
|
1288
1380
|
)
|
|
1289
1381
|
|
|
1382
|
+
st.subheader("Contas Google/YouTube — canais em lote")
|
|
1383
|
+
st.caption("Cada registo representa uma conta Google que pode gerir vários canais do YouTube. O e-mail identifica a conta; esta área não lê a caixa Gmail. Cada conta tem Client ID, Client Secret e token OAuth próprios.")
|
|
1384
|
+
batch_accounts = [account for account in settings.get("youtube_batch_accounts", []) if isinstance(account, dict) and account.get("id")]
|
|
1385
|
+
for batch_account in batch_accounts:
|
|
1386
|
+
account_id = str(batch_account["id"])
|
|
1387
|
+
with st.container(border=True):
|
|
1388
|
+
with st.form(f"batch_account_form_{account_id}"):
|
|
1389
|
+
account_cols = st.columns(4)
|
|
1390
|
+
with account_cols[0]:
|
|
1391
|
+
account_label = st.text_input("Nome da conta", value=str(batch_account.get("label", "Canais YouTube")), key=f"batch_label_{account_id}")
|
|
1392
|
+
with account_cols[1]:
|
|
1393
|
+
account_email = st.text_input("E-mail/Gmail da conta", value=str(batch_account.get("email", "")), key=f"batch_email_{account_id}")
|
|
1394
|
+
with account_cols[2]:
|
|
1395
|
+
account_client_id = st.text_input("OAuth Client ID", value=str(batch_account.get("client_id", "")), key=f"batch_client_id_{account_id}")
|
|
1396
|
+
with account_cols[3]:
|
|
1397
|
+
account_client_secret = st.text_input("OAuth Client Secret", value=str(batch_account.get("client_secret", "")), type="password", key=f"batch_client_secret_{account_id}")
|
|
1398
|
+
save_account = st.form_submit_button("Guardar conta", type="primary", use_container_width=True)
|
|
1399
|
+
account_status = youtube_batch_account_status(batch_account, STORAGE)
|
|
1400
|
+
status_cols = st.columns([2, 1, 1])
|
|
1401
|
+
with status_cols[0]:
|
|
1402
|
+
(st.success if account_status.ok else st.warning)(account_status.message)
|
|
1403
|
+
with status_cols[1]:
|
|
1404
|
+
if st.button("Autorizar/Reautorizar", key=f"batch_authorize_settings_{account_id}", use_container_width=True):
|
|
1405
|
+
result = authorize_youtube_batch_account(batch_account, STORAGE)
|
|
1406
|
+
(st.success if result.ok else st.error)(result.message)
|
|
1407
|
+
if result.ok:
|
|
1408
|
+
st.rerun()
|
|
1409
|
+
with status_cols[2]:
|
|
1410
|
+
if st.button("Remover conta", key=f"batch_remove_settings_{account_id}", use_container_width=True):
|
|
1411
|
+
delete_youtube_batch_token(batch_account, STORAGE)
|
|
1412
|
+
remaining_accounts = [account for account in batch_accounts if str(account.get("id")) != account_id]
|
|
1413
|
+
settings["youtube_batch_accounts"] = remaining_accounts
|
|
1414
|
+
if settings.get("youtube_batch_selected_account_id") == account_id:
|
|
1415
|
+
settings["youtube_batch_selected_account_id"] = str(remaining_accounts[0].get("id")) if remaining_accounts else ""
|
|
1416
|
+
write_json("settings.json", settings)
|
|
1417
|
+
st.rerun()
|
|
1418
|
+
if save_account:
|
|
1419
|
+
if "@" not in account_email.strip():
|
|
1420
|
+
st.error("Informe um e-mail Google válido.")
|
|
1421
|
+
elif not account_client_id.strip() or not account_client_secret.strip():
|
|
1422
|
+
st.error("Informe o Client ID e o Client Secret desta conta.")
|
|
1423
|
+
else:
|
|
1424
|
+
for existing in batch_accounts:
|
|
1425
|
+
if str(existing.get("id")) == account_id:
|
|
1426
|
+
credentials_changed = any(existing.get(field, "") != value for field, value in (("email", account_email.strip()), ("client_id", account_client_id.strip()), ("client_secret", account_client_secret.strip())))
|
|
1427
|
+
if credentials_changed:
|
|
1428
|
+
delete_youtube_batch_token(existing, STORAGE)
|
|
1429
|
+
existing.update({"label": account_label.strip() or "Canais YouTube", "email": account_email.strip(), "client_id": account_client_id.strip(), "client_secret": account_client_secret.strip()})
|
|
1430
|
+
settings["youtube_batch_accounts"] = batch_accounts
|
|
1431
|
+
write_json("settings.json", settings)
|
|
1432
|
+
st.success("Conta Google/YouTube guardada.")
|
|
1433
|
+
st.rerun()
|
|
1434
|
+
with st.form("add_batch_account_form"):
|
|
1435
|
+
add_cols = st.columns(4)
|
|
1436
|
+
with add_cols[0]:
|
|
1437
|
+
new_account_label = st.text_input("Nome da nova conta", value="Canais YouTube", key="new_batch_account_label")
|
|
1438
|
+
with add_cols[1]:
|
|
1439
|
+
new_account_email = st.text_input("E-mail/Gmail", key="new_batch_account_email")
|
|
1440
|
+
with add_cols[2]:
|
|
1441
|
+
new_account_client_id = st.text_input("OAuth Client ID", key="new_batch_account_client_id")
|
|
1442
|
+
with add_cols[3]:
|
|
1443
|
+
new_account_client_secret = st.text_input("OAuth Client Secret", type="password", key="new_batch_account_client_secret")
|
|
1444
|
+
add_account = st.form_submit_button("Adicionar conta Google/YouTube", use_container_width=True)
|
|
1445
|
+
if add_account:
|
|
1446
|
+
if "@" not in new_account_email.strip():
|
|
1447
|
+
st.error("Informe um e-mail Google válido.")
|
|
1448
|
+
elif not new_account_client_id.strip() or not new_account_client_secret.strip():
|
|
1449
|
+
st.error("Informe o Client ID e o Client Secret da nova conta.")
|
|
1450
|
+
else:
|
|
1451
|
+
new_account = {"id": f"google_batch_{uuid.uuid4().hex[:12]}", "label": new_account_label.strip() or "Canais YouTube", "email": new_account_email.strip(), "client_id": new_account_client_id.strip(), "client_secret": new_account_client_secret.strip()}
|
|
1452
|
+
batch_accounts.append(new_account)
|
|
1453
|
+
settings["youtube_batch_accounts"] = batch_accounts
|
|
1454
|
+
settings["youtube_batch_selected_account_id"] = new_account["id"]
|
|
1455
|
+
write_json("settings.json", settings)
|
|
1456
|
+
st.success(f"Conta {new_account['email']} adicionada.")
|
|
1457
|
+
st.rerun()
|
|
1458
|
+
|
|
1290
1459
|
with st.form("settings_form"):
|
|
1291
1460
|
st.subheader("Execução local")
|
|
1292
1461
|
port = st.number_input("Porta Streamlit", 1, 65535, int(settings.get("port", 3030)))
|
package/hermes_ui/domain.py
CHANGED
|
@@ -22,6 +22,9 @@ def make_id(prefix: str) -> str:
|
|
|
22
22
|
def create_channel(name: str, url: str = "", metadata: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
23
23
|
channel = {
|
|
24
24
|
"id": make_id("channel"),
|
|
25
|
+
"youtube_channel_id": "",
|
|
26
|
+
"google_account_id": "",
|
|
27
|
+
"google_account_email": "",
|
|
25
28
|
"name": name.strip(),
|
|
26
29
|
"url": url.strip(),
|
|
27
30
|
"handle": "",
|
package/hermes_ui/storage.py
CHANGED
|
@@ -83,6 +83,8 @@ DEFAULTS: dict[str, Any] = {
|
|
|
83
83
|
"youtube_api_key": "",
|
|
84
84
|
"youtube_client_id": "",
|
|
85
85
|
"youtube_client_secret": "",
|
|
86
|
+
"youtube_batch_accounts": [],
|
|
87
|
+
"youtube_batch_selected_account_id": "",
|
|
86
88
|
"kaggle_username": "",
|
|
87
89
|
"kaggle_api_key": "",
|
|
88
90
|
"kaggle_kernel_slug": "thunderbolt-niche-finder",
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
import secrets
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from integrations.platforms import IntegrationResult
|
|
12
|
+
|
|
13
|
+
BATCH_SCOPES = ["https://www.googleapis.com/auth/youtube.readonly"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _text(value: Any) -> str:
|
|
17
|
+
return str(value or "").strip()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def account_key(account: dict[str, Any]) -> str:
|
|
21
|
+
raw = _text(account.get("id") or account.get("email"))
|
|
22
|
+
digest = hashlib.sha256(raw.lower().encode("utf-8")).hexdigest()[:16]
|
|
23
|
+
slug = re.sub(r"[^a-z0-9]+", "-", raw.lower()).strip("-")[:36] or "google-account"
|
|
24
|
+
return f"{slug}-{digest}"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def token_path(storage_root: Path, account: dict[str, Any]) -> Path:
|
|
28
|
+
return Path(storage_root) / "state" / "youtube_batch_tokens" / f"{account_key(account)}.json"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _client_config(account: dict[str, Any]) -> dict[str, Any]:
|
|
32
|
+
return {
|
|
33
|
+
"installed": {
|
|
34
|
+
"client_id": _text(account.get("client_id")),
|
|
35
|
+
"client_secret": _text(account.get("client_secret")),
|
|
36
|
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
|
37
|
+
"token_uri": "https://oauth2.googleapis.com/token",
|
|
38
|
+
"redirect_uris": ["http://localhost"],
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _save_credentials(path: Path, credentials: Any) -> None:
|
|
44
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
45
|
+
temporary = path.with_name(f".{path.name}.{secrets.token_hex(6)}.tmp")
|
|
46
|
+
temporary.write_text(credentials.to_json(), encoding="utf-8")
|
|
47
|
+
try:
|
|
48
|
+
os.chmod(temporary, 0o600)
|
|
49
|
+
except OSError:
|
|
50
|
+
pass
|
|
51
|
+
temporary.replace(path)
|
|
52
|
+
try:
|
|
53
|
+
os.chmod(path, 0o600)
|
|
54
|
+
except OSError:
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _load_credentials(path: Path) -> Any | None:
|
|
59
|
+
if not path.exists() or path.stat().st_size == 0:
|
|
60
|
+
return None
|
|
61
|
+
try:
|
|
62
|
+
from google.auth.transport.requests import Request
|
|
63
|
+
from google.oauth2.credentials import Credentials
|
|
64
|
+
except ImportError as exc:
|
|
65
|
+
raise RuntimeError("As dependências Google OAuth ainda não estão instaladas. Execute a instalação do Thunderbolt novamente.") from exc
|
|
66
|
+
raw = json.loads(path.read_text(encoding="utf-8"))
|
|
67
|
+
credentials = Credentials.from_authorized_user_info(raw, BATCH_SCOPES)
|
|
68
|
+
if credentials.expired and credentials.refresh_token:
|
|
69
|
+
credentials.refresh(Request())
|
|
70
|
+
_save_credentials(path, credentials)
|
|
71
|
+
return credentials
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def authorize_account(account: dict[str, Any], storage_root: Path, *, open_browser: bool = True) -> IntegrationResult:
|
|
75
|
+
email = _text(account.get("email"))
|
|
76
|
+
if not email or "@" not in email:
|
|
77
|
+
return IntegrationResult(False, "Informe um e-mail Google válido para esta conta.", {"status": "email_not_configured"})
|
|
78
|
+
if not _text(account.get("client_id")) or not _text(account.get("client_secret")):
|
|
79
|
+
return IntegrationResult(False, "Preencha o OAuth Client ID e o OAuth Client Secret desta conta.", {"status": "oauth_not_configured", "email": email})
|
|
80
|
+
try:
|
|
81
|
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
82
|
+
except ImportError as exc:
|
|
83
|
+
return IntegrationResult(False, "As dependências Google OAuth ainda não estão instaladas. Execute a instalação do Thunderbolt novamente.", {"status": "missing_dependencies", "error": str(exc)})
|
|
84
|
+
try:
|
|
85
|
+
flow = InstalledAppFlow.from_client_config(_client_config(account), BATCH_SCOPES)
|
|
86
|
+
credentials = flow.run_local_server(
|
|
87
|
+
host="localhost",
|
|
88
|
+
port=0,
|
|
89
|
+
open_browser=open_browser,
|
|
90
|
+
access_type="offline",
|
|
91
|
+
prompt="consent",
|
|
92
|
+
include_granted_scopes="true",
|
|
93
|
+
)
|
|
94
|
+
path = token_path(storage_root, account)
|
|
95
|
+
_save_credentials(path, credentials)
|
|
96
|
+
return IntegrationResult(True, f"Conta Google autorizada para listagem de canais: {email}.", {"status": "authorized", "email": email, "token_path": str(path)})
|
|
97
|
+
except Exception as exc:
|
|
98
|
+
return IntegrationResult(False, f"A autorização da conta {email} falhou: {exc}", {"status": "authorization_failed", "email": email})
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def delete_account_token(account: dict[str, Any], storage_root: Path) -> None:
|
|
102
|
+
path = token_path(storage_root, account)
|
|
103
|
+
try:
|
|
104
|
+
path.unlink(missing_ok=True)
|
|
105
|
+
except OSError:
|
|
106
|
+
pass
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def account_status(account: dict[str, Any], storage_root: Path) -> IntegrationResult:
|
|
110
|
+
email = _text(account.get("email"))
|
|
111
|
+
if not email or not _text(account.get("client_id")) or not _text(account.get("client_secret")):
|
|
112
|
+
return IntegrationResult(False, "Conta incompleta: informe e-mail, Client ID e Client Secret.", {"status": "not_configured", "email": email})
|
|
113
|
+
path = token_path(storage_root, account)
|
|
114
|
+
if not path.exists():
|
|
115
|
+
return IntegrationResult(False, "Conta configurada, mas ainda não autorizada.", {"status": "requires_authorization", "email": email})
|
|
116
|
+
try:
|
|
117
|
+
raw = json.loads(path.read_text(encoding="utf-8"))
|
|
118
|
+
if isinstance(raw, dict) and (raw.get("refresh_token") or raw.get("access_token")):
|
|
119
|
+
return IntegrationResult(True, f"Conta autorizada: {email}", {"status": "ready", "email": email, "token_path": str(path), "refresh_on_use": bool(raw.get("refresh_token"))})
|
|
120
|
+
except (OSError, ValueError) as exc:
|
|
121
|
+
return IntegrationResult(False, f"Token da conta {email} inválido: {exc}", {"status": "invalid_token", "email": email})
|
|
122
|
+
return IntegrationResult(False, f"Token da conta {email} não está pronto.", {"status": "requires_authorization", "email": email})
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _channel_record(item: dict[str, Any]) -> dict[str, Any]:
|
|
126
|
+
channel_id = _text(item.get("id"))
|
|
127
|
+
snippet = item.get("snippet") or {}
|
|
128
|
+
statistics = item.get("statistics") or {}
|
|
129
|
+
custom_url = _text(snippet.get("customUrl"))
|
|
130
|
+
url = f"https://www.youtube.com/{custom_url.lstrip('/')}" if custom_url else f"https://www.youtube.com/channel/{channel_id}"
|
|
131
|
+
thumbnails = snippet.get("thumbnails") or {}
|
|
132
|
+
thumbnail = (thumbnails.get("high") or thumbnails.get("medium") or thumbnails.get("default") or {}).get("url", "")
|
|
133
|
+
|
|
134
|
+
def integer(value: Any) -> int | None:
|
|
135
|
+
try:
|
|
136
|
+
return int(value) if value is not None else None
|
|
137
|
+
except (TypeError, ValueError):
|
|
138
|
+
return None
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
"youtube_channel_id": channel_id,
|
|
142
|
+
"name": _text(snippet.get("title")) or channel_id,
|
|
143
|
+
"url": url,
|
|
144
|
+
"handle": custom_url,
|
|
145
|
+
"description": _text(snippet.get("description")),
|
|
146
|
+
"thumbnail_url": _text(thumbnail),
|
|
147
|
+
"subscriber_count": integer(statistics.get("subscriberCount")),
|
|
148
|
+
"video_count": integer(statistics.get("videoCount")),
|
|
149
|
+
"view_count": integer(statistics.get("viewCount")),
|
|
150
|
+
"metrics_source": "youtube_data_api_oauth_mine",
|
|
151
|
+
"published_at": _text(snippet.get("publishedAt")),
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def list_my_channels(account: dict[str, Any], storage_root: Path) -> IntegrationResult:
|
|
156
|
+
status = account_status(account, storage_root)
|
|
157
|
+
if not status.ok:
|
|
158
|
+
return status
|
|
159
|
+
try:
|
|
160
|
+
credentials = _load_credentials(token_path(storage_root, account))
|
|
161
|
+
if credentials is None:
|
|
162
|
+
return IntegrationResult(False, "Autorize primeiro esta conta Google.", {"status": "requires_authorization"})
|
|
163
|
+
from googleapiclient.discovery import build
|
|
164
|
+
|
|
165
|
+
youtube = build("youtube", "v3", credentials=credentials, cache_discovery=False)
|
|
166
|
+
channels: list[dict[str, Any]] = []
|
|
167
|
+
page_token = None
|
|
168
|
+
while True:
|
|
169
|
+
kwargs: dict[str, Any] = {
|
|
170
|
+
"part": "snippet,contentDetails,statistics",
|
|
171
|
+
"mine": True,
|
|
172
|
+
"maxResults": 50,
|
|
173
|
+
}
|
|
174
|
+
if page_token:
|
|
175
|
+
kwargs["pageToken"] = page_token
|
|
176
|
+
response = youtube.channels().list(**kwargs).execute()
|
|
177
|
+
channels.extend(_channel_record(item) for item in response.get("items", []))
|
|
178
|
+
page_token = response.get("nextPageToken")
|
|
179
|
+
if not page_token:
|
|
180
|
+
break
|
|
181
|
+
return IntegrationResult(True, f"{len(channels)} canal(is) encontrado(s) na conta {account.get('email')}.", {"status": "listed", "email": account.get("email", ""), "channels": channels, "count": len(channels)})
|
|
182
|
+
except ImportError as exc:
|
|
183
|
+
return IntegrationResult(False, "A biblioteca Google API Client ainda não está instalada. Execute a instalação do Thunderbolt novamente.", {"status": "missing_dependencies", "error": str(exc)})
|
|
184
|
+
except Exception as exc:
|
|
185
|
+
return IntegrationResult(False, f"Não foi possível listar os canais da conta {account.get('email', '')}: {exc}", {"status": "list_failed", "email": account.get("email", ""), "error": str(exc)})
|
package/package.json
CHANGED