import gradio as gr import json import os import shutil # Caminhos MOVIES_FILE = "movies.json" POSTER_DIR = "assets/posters" VIDEO_DIR = "assets/videos" ADMIN_PIN = "1234" # Criar diretórios os.makedirs(POSTER_DIR, exist_ok=True) os.makedirs(VIDEO_DIR, exist_ok=True) # Carregar filmes já salvos if os.path.exists(MOVIES_FILE): with open(MOVIES_FILE, "r", encoding="utf-8") as f: movie_data = json.load(f) else: movie_data = [] # Mostrar filme selecionado def show_movie(title): movie = next((m for m in movie_data if m["title"] == title), None) if movie: return movie["poster"], movie["description"], movie["video"] return None, "Filme não encontrado", None # Adicionar filme novo def add_movie(title, description, video_file, poster_file): if not all([title, description, video_file, poster_file]): return "❌ Preencha todos os campos." try: # Corrigir caminhos video_filename = os.path.basename(video_file.name) poster_filename = os.path.basename(poster_file.name) video_path = os.path.join(VIDEO_DIR, video_filename) poster_path = os.path.join(POSTER_DIR, poster_filename) shutil.move(video_file.name, video_path) shutil.move(poster_file.name, poster_path) new_movie = { "title": title, "description": description, "poster": poster_path, "video": video_path } movie_data.append(new_movie) with open(MOVIES_FILE, "w", encoding="utf-8") as f: json.dump(movie_data, f, indent=2, ensure_ascii=False) return f"✅ Filme '{title}' adicionado com sucesso!" except Exception as e: return f"❌ Erro ao salvar o filme: {e}" # Verificar PIN def check_pin(pin): if pin == ADMIN_PIN: return ( gr.update(visible=True), # título gr.update(visible=True), # descrição gr.update(visible=True), # vídeo gr.update(visible=True), # pôster gr.update(visible=True), # botão gr.update(visible=True), # status "✅ Acesso liberado" ) else: return ( gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), "❌ PIN incorreto" ) # Lista de títulos titles = [m["title"] for m in movie_data] # Interface with gr.Blocks() as demo: gr.Markdown("# 🎬 App de Streaming de Filmes") with gr.Tab("🎥 Ver Filmes"): dropdown = gr.Dropdown(choices=titles, label="Escolha um filme") poster = gr.Image(label="Capa") desc = gr.Textbox(label="Descrição", lines=3) player = gr.Video(label="Assistir") dropdown.change(show_movie, dropdown, [poster, desc, player]) with gr.Tab("🛠️ Admin: Adicionar Filme"): gr.Markdown("### 🔐 Área do Administrador") pin_input = gr.Textbox(label="PIN", type="password") unlock = gr.Button("Desbloquear") title = gr.Textbox(label="Título", visible=False) description = gr.Textbox(label="Descrição", visible=False) video = gr.File(label="Vídeo (.mp4)", file_types=[".mp4"], visible=False) poster_file = gr.File(label="Pôster (.jpg/.png)", file_types=[".jpg", ".png"], visible=False) add_button = gr.Button("Adicionar ao Catálogo", visible=False) status = gr.Textbox(label="Status", visible=False) unlock.click( fn=check_pin, inputs=[pin_input], outputs=[title, description, video, poster_file, add_button, status, status] ) add_button.click( fn=add_movie, inputs=[title, description, video, poster_file], outputs=status ) demo.launch()