From 54014ae01f9eb03e54c0054c8e2071cc6ed02bf5 Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Mon, 25 May 2026 02:09:40 +0100 Subject: [PATCH] feat(frontend): implement full in-app streaming system with 4 APIs - Add streaming types, interfaces, and normalized response structures - Create StreamingManager with URL building, validation, and security - Implement provider adapters for VidLink (2 servers), VidKing (1 server), AnyEmbed (5 servers) - Add Zustand streaming store with server selection, fallback, and retry logic - Build StreamingPlayer iframe component with fullscreen and keyboard shortcuts - Add ServerSelector dropdown with 8 servers and active state indicator - Create EpisodeNavigator with season tabs and episode grid - Implement AutoMarkWatchedPrompt for one-time completion opt-in - Add StreamingSection with Watch Now / Continue Watching entry point - Integrate StreamingSection into EntryDetailsPage --- .../streaming/AutoMarkWatchedPrompt.tsx | 41 ++ .../components/streaming/EpisodeNavigator.tsx | 114 +++ .../components/streaming/ServerSelector.tsx | 50 ++ .../components/streaming/StreamingPlayer.tsx | 386 ++++++++++ .../components/streaming/StreamingSection.tsx | 219 ++++++ .../src/components/streaming/index.ts | 5 + react_frontend/src/index.css | 22 + react_frontend/src/lib/streaming/index.ts | 26 + .../src/lib/streaming/providers/anyembed.ts | 78 +++ .../src/lib/streaming/providers/index.ts | 3 + .../src/lib/streaming/providers/vidking.ts | 45 ++ .../src/lib/streaming/providers/vidlink.ts | 58 ++ .../src/lib/streaming/streaming-manager.ts | 304 ++++++++ react_frontend/src/lib/streaming/types.ts | 110 +++ react_frontend/src/pages/EntryDetailsPage.tsx | 4 + react_frontend/src/pages/HomePage2.tsx | 293 ++++++++ react_frontend/src/pages/HomePage3.tsx | 661 ++++++++++++++++++ react_frontend/src/router.tsx | 22 + .../src/stores/useStreamingStore.ts | 176 +++++ 19 files changed, 2617 insertions(+) create mode 100644 react_frontend/src/components/streaming/AutoMarkWatchedPrompt.tsx create mode 100644 react_frontend/src/components/streaming/EpisodeNavigator.tsx create mode 100644 react_frontend/src/components/streaming/ServerSelector.tsx create mode 100644 react_frontend/src/components/streaming/StreamingPlayer.tsx create mode 100644 react_frontend/src/components/streaming/StreamingSection.tsx create mode 100644 react_frontend/src/components/streaming/index.ts create mode 100644 react_frontend/src/lib/streaming/index.ts create mode 100644 react_frontend/src/lib/streaming/providers/anyembed.ts create mode 100644 react_frontend/src/lib/streaming/providers/index.ts create mode 100644 react_frontend/src/lib/streaming/providers/vidking.ts create mode 100644 react_frontend/src/lib/streaming/providers/vidlink.ts create mode 100644 react_frontend/src/lib/streaming/streaming-manager.ts create mode 100644 react_frontend/src/lib/streaming/types.ts create mode 100644 react_frontend/src/pages/HomePage2.tsx create mode 100644 react_frontend/src/pages/HomePage3.tsx create mode 100644 react_frontend/src/stores/useStreamingStore.ts diff --git a/react_frontend/src/components/streaming/AutoMarkWatchedPrompt.tsx b/react_frontend/src/components/streaming/AutoMarkWatchedPrompt.tsx new file mode 100644 index 0000000..4590eff --- /dev/null +++ b/react_frontend/src/components/streaming/AutoMarkWatchedPrompt.tsx @@ -0,0 +1,41 @@ +import { Button } from '@/components/ui/button'; +import { CheckCircle2, X } from 'lucide-react'; + +interface AutoMarkWatchedPromptProps { + onAccept: () => void; + onDecline: () => void; +} + +export function AutoMarkWatchedPrompt({ onAccept, onDecline }: AutoMarkWatchedPromptProps) { + return ( +
+
+ +
+

Auto-mark as watched?

+

+ Automatically mark episodes/movies as watched when you finish them +

+
+
+ + +
+
+
+ ); +} diff --git a/react_frontend/src/components/streaming/EpisodeNavigator.tsx b/react_frontend/src/components/streaming/EpisodeNavigator.tsx new file mode 100644 index 0000000..35f2258 --- /dev/null +++ b/react_frontend/src/components/streaming/EpisodeNavigator.tsx @@ -0,0 +1,114 @@ +import { useMemo } from 'react'; +import { Button } from '@/components/ui/button'; +import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area'; +import { ChevronLeft, ChevronRight } from 'lucide-react'; +import type { Season } from '@/types'; + +interface EpisodeNavigatorProps { + seasons: Season[]; + currentSeason?: number; + currentEpisode?: number; + onEpisodeChange?: (season: number, episode: number) => void; +} + +export function EpisodeNavigator({ + seasons, + currentSeason, + currentEpisode, + onEpisodeChange, +}: EpisodeNavigatorProps) { + const activeSeason = useMemo( + () => seasons.find((s) => s.season_number === currentSeason), + [seasons, currentSeason] + ); + + const episodes = activeSeason?.episodes || []; + + const hasPrev = currentEpisode && currentEpisode > 1; + const hasNext = currentEpisode && activeSeason && currentEpisode < (activeSeason.episode_count || episodes.length); + + // Check if there's a next season + const hasNextSeason = useMemo(() => { + if (!currentSeason) return false; + return seasons.some((s) => s.season_number === currentSeason + 1); + }, [seasons, currentSeason]); + + return ( +
+ {/* Season selector */} +
+ +
+ {seasons + .filter((s) => s.season_number > 0) + .sort((a, b) => a.season_number - b.season_number) + .map((season) => ( + + ))} +
+ +
+
+ + {/* Episode navigation */} +
+ + + +
+ {episodes + .sort((a, b) => a.episode_number - b.episode_number) + .map((ep) => ( + + ))} +
+ +
+ + +
+
+ ); +} diff --git a/react_frontend/src/components/streaming/ServerSelector.tsx b/react_frontend/src/components/streaming/ServerSelector.tsx new file mode 100644 index 0000000..b43bb96 --- /dev/null +++ b/react_frontend/src/components/streaming/ServerSelector.tsx @@ -0,0 +1,50 @@ +import { useStreamingStore } from '@/stores/useStreamingStore'; +import { Button } from '@/components/ui/button'; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, +} from '@/components/ui/dropdown-menu'; +import { Monitor, Check, Loader2 } from 'lucide-react'; + +export function ServerSelector() { + const { servers, activeServer, isLoading, switchServer } = useStreamingStore(); + + return ( + + + + + + {servers.map((server) => ( + switchServer(server.id)} + className="flex items-center justify-between" + > + {server.name} + {server.id === activeServer?.id && ( + + )} + + ))} + + + ); +} diff --git a/react_frontend/src/components/streaming/StreamingPlayer.tsx b/react_frontend/src/components/streaming/StreamingPlayer.tsx new file mode 100644 index 0000000..bd9aff0 --- /dev/null +++ b/react_frontend/src/components/streaming/StreamingPlayer.tsx @@ -0,0 +1,386 @@ +import { useCallback, useEffect, useRef, useState } from 'react'; +import { useStreamingStore } from '@/stores/useStreamingStore'; +import { + isValidEventOrigin, + updateProgressFromEvent, + isConsideredWatched, + logWatchCompleted, + getServerSandboxPolicy, + getServerAllowedFeatures, + isValidEmbedUrl, +} from '@/lib/streaming'; +import type { PlayerEvent } from '@/lib/streaming'; +import { ServerSelector } from './ServerSelector'; +import { EpisodeNavigator } from './EpisodeNavigator'; +import { AutoMarkWatchedPrompt } from './AutoMarkWatchedPrompt'; +import { Skeleton } from '@/components/ui/skeleton'; +import { Button } from '@/components/ui/button'; +import { + X, + Maximize2, + Minimize2, + AlertTriangle, + RefreshCw, + SkipForward, +} from 'lucide-react'; +import type { Entry, Season } from '@/types'; + +interface StreamingPlayerProps { + entry: Entry; + seasons?: Season[]; + currentSeason?: number; + currentEpisode?: number; + onEpisodeChange?: (season: number, episode: number) => void; + onMarkAsWatched?: (season?: number, episode?: number) => void; +} + +export function StreamingPlayer({ + entry, + seasons, + currentSeason, + currentEpisode, + onEpisodeChange, + onMarkAsWatched, +}: StreamingPlayerProps) { + const { + currentStreamUrl, + isLoading, + error, + activeServer, + isPlayerOpen, + autoMarkWatched, + autoMarkPromptShown, + closePlayer, + tryNextServer, + play, + currentRequest, + } = useStreamingStore(); + + const iframeRef = useRef(null); + const [isFullscreen, setIsFullscreen] = useState(false); + const [iframeLoaded, setIframeLoaded] = useState(false); + const [showAutoMarkPrompt, setShowAutoMarkPrompt] = useState(false); + const [markedAsWatched, setMarkedAsWatched] = useState>(new Set()); + const containerRef = useRef(null); + + // Handle fullscreen + const toggleFullscreen = useCallback(() => { + if (!containerRef.current) return; + + if (!document.fullscreenElement) { + containerRef.current.requestFullscreen().then(() => setIsFullscreen(true)); + } else { + document.exitFullscreen().then(() => setIsFullscreen(false)); + } + }, []); + + // Listen for fullscreen change + useEffect(() => { + const handler = () => setIsFullscreen(!!document.fullscreenElement); + document.addEventListener('fullscreenchange', handler); + return () => document.removeEventListener('fullscreenchange', handler); + }, []); + + // Keyboard shortcuts + useEffect(() => { + const handler = (e: KeyboardEvent) => { + if (!isPlayerOpen) return; + + switch (e.key) { + case 'Escape': + if (isFullscreen) { + document.exitFullscreen(); + } else { + closePlayer(); + } + break; + case 'f': + case 'F': + if (!e.ctrlKey && !e.metaKey) { + toggleFullscreen(); + } + break; + } + }; + + window.addEventListener('keydown', handler); + return () => window.removeEventListener('keydown', handler); + }, [isPlayerOpen, isFullscreen, closePlayer, toggleFullscreen]); + + // Listen for player events from iframe + useEffect(() => { + const handler = (event: MessageEvent) => { + if (!isValidEventOrigin(event.origin)) return; + + // Handle VidLink progress data + if (event.data?.type === 'MEDIA_DATA') { + // VidLink specific progress tracking + return; + } + + // Handle player events from VidLink and VidKing + if (event.data?.type === 'PLAYER_EVENT') { + const data = event.data.data; + const playerEvent: PlayerEvent = { + event: data.event, + currentTime: data.currentTime || 0, + duration: data.duration || 0, + mediaType: entry.is_tvseries ? 'tv' : 'movie', + tmdbId: entry.entryIds?.tmdb_id || 0, + season: currentSeason, + episode: currentEpisode, + progress: data.progress, + }; + + // Update progress + updateProgressFromEvent( + playerEvent, + entry.title, + entry.poster_path || undefined + ); + + // Check for completion + if (playerEvent.event === 'ended' || isConsideredWatched( + playerEvent.currentTime, + playerEvent.duration, + autoMarkWatched.threshold + )) { + handleWatchCompleted(playerEvent); + } + } + + // Handle VidKing string events + if (typeof event.data === 'string') { + try { + const parsed = JSON.parse(event.data); + if (parsed?.type === 'PLAYER_EVENT') { + const data = parsed.data; + if (data.event === 'ended') { + handleWatchCompleted({ + event: 'ended', + currentTime: data.currentTime || data.timestamp || 0, + duration: data.duration || 0, + mediaType: entry.is_tvseries ? 'tv' : 'movie', + tmdbId: entry.entryIds?.tmdb_id || 0, + season: currentSeason, + episode: currentEpisode, + }); + } + } + } catch { + // Not JSON, ignore + } + } + }; + + window.addEventListener('message', handler); + return () => window.removeEventListener('message', handler); + }, [entry, currentSeason, currentEpisode, autoMarkWatched]); + + const handleWatchCompleted = useCallback( + (event: PlayerEvent) => { + const key = entry.is_tvseries + ? `${event.tmdbId}-s${currentSeason}e${currentEpisode}` + : `${event.tmdbId}`; + + // Don't mark the same thing twice in this session + if (markedAsWatched.has(key)) return; + + // Log the watch event + logWatchCompleted({ + tmdbId: event.tmdbId, + type: event.mediaType, + title: entry.title, + season: currentSeason, + episode: currentEpisode, + completedAt: Date.now(), + duration: event.duration, + }); + + if (autoMarkWatched.enabled) { + // Auto-mark as watched + onMarkAsWatched?.(currentSeason, currentEpisode); + setMarkedAsWatched((prev) => new Set(prev).add(key)); + } else if (!autoMarkPromptShown) { + // Show the prompt + setShowAutoMarkPrompt(true); + } + }, + [entry, currentSeason, currentEpisode, autoMarkWatched, autoMarkPromptShown, markedAsWatched, onMarkAsWatched] + ); + + // Handle next episode + const handleNextEpisode = useCallback(() => { + if (!entry.is_tvseries || !seasons || !currentSeason || !currentEpisode) return; + + const season = seasons.find((s) => s.season_number === currentSeason); + if (!season?.episodes) return; + + const nextEp = currentEpisode + 1; + const episodeExists = season.episodes.some((e) => e.episode_number === nextEp); + + if (episodeExists) { + onEpisodeChange?.(currentSeason, nextEp); + } else { + // Try next season + const nextSeason = seasons.find((s) => s.season_number === currentSeason + 1); + if (nextSeason?.episodes?.length) { + onEpisodeChange?.(nextSeason.season_number, 1); + } + } + }, [entry, seasons, currentSeason, currentEpisode, onEpisodeChange]); + + // Reset iframe loaded state when URL changes + useEffect(() => { + setIframeLoaded(false); + }, [currentStreamUrl]); + + if (!isPlayerOpen) return null; + + return ( +
+ {/* Top controls bar */} +
+
+ + {entry.is_tvseries && seasons && currentSeason && currentEpisode && ( + + S{currentSeason}:E{currentEpisode} + + )} +
+
+ {entry.is_tvseries && ( + + )} + + +
+
+ + {/* Loading state */} + {(isLoading || !iframeLoaded) && !error && ( +
+
+
+

Loading stream...

+
+
+ )} + + {/* Error state */} + {error && ( +
+
+
+ +
+
+

Stream Unavailable

+

{error.message}

+
+
+ + +
+
+
+ )} + + {/* Video iframe */} + {currentStreamUrl && isValidEmbedUrl(currentStreamUrl) && ( +