File size: 4,213 Bytes
e787e40
 
 
25dca33
fa9497f
 
 
 
 
 
e787e40
 
 
 
 
 
 
 
 
 
 
fa9497f
 
 
 
 
 
 
 
 
 
 
 
 
 
e787e40
 
fa9497f
e787e40
fa9497f
 
e787e40
 
 
 
 
fa9497f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e787e40
 
fa9497f
e787e40
fa9497f
 
e787e40
 
 
 
 
fa9497f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e787e40
fa9497f
e787e40
fa9497f
 
e787e40
 
 
 
fa9497f
 
 
e787e40
fa9497f
 
e787e40
 
fa9497f
 
 
 
 
 
 
e787e40
 
 
fa9497f
 
 
 
e787e40
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"use client";
import apiClient from "@/api/apiClient";
import { useEffect, useState } from "react";
import MoviePlayer from "@/components/MoviePlayer";
import "./MoviePlayerPage.css";
import { Spinner } from "@/components/Spinner";
import { ProgressBar, Container, Row, Col, Alert } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";

const DEBUG = false; // Set to true to enable debug mode

export default function FilmPlayer({ params }) {
  const title = decodeURIComponent(params.title);
  const [videoUrl, setVideoUrl] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);
  const [downloadProgress, setDownloadProgress] = useState(null);
  const [downloadProgressUrl, setDownloadProgressUrl] = useState(null);
  const [metadata, setMetadata] = useState(null);

  useEffect(() => {
    if (DEBUG) {
      setMetadata({ title: "Debug Movie", year: "2024" });
    } else {
      const fetchMovieCard = async () => {
        try {
          const data = await apiClient.getMovieCard(title);
          if (data.title) {
            setMetadata(data);
          } else {
            setError("Movie card not found");
            setLoading(false);
          }
        } catch (err) {
          setError("Failed to fetch movie card");
          setLoading(false);
        }
      };

      fetchMovieCard();
    }
  }, [title]);

  useEffect(() => {
    if (!metadata) return;

    if (DEBUG) {
      setTimeout(() => {
        setDownloadProgressUrl("dummy_progress_url");
        setLoading(false);
      }, 1000);
    } else {
      const fetchVideoData = async () => {
        try {
          const data = await apiClient.getMovieByTitle(title);
          if (data.url) {
            setVideoUrl(data.url);
            setLoading(false);
          } else if (data.status === "Download started") {
            setDownloadProgressUrl(data.progress_url);
            setLoading(false);
          }
        } catch (err) {
          setError("Failed to fetch video data");
          setLoading(false);
        }
      };

      fetchVideoData();
    }
  }, [title, metadata]);

  useEffect(() => {
    if (!downloadProgressUrl) return;

    if (DEBUG) {
      const dummyProgressData = {
        progress: {
          status: "Downloading",
          progress: 45.5,
          eta: 120,
        },
      };
      setDownloadProgress(dummyProgressData.progress);
    } else {
      const intervalId = setInterval(async () => {
        try {
          const response = await fetch(downloadProgressUrl);
          const progressData = await response.json();
          if (progressData.progress.status === "Completed") {
            clearInterval(intervalId);
            setDownloadProgressUrl(null);
            setDownloadProgress(null);
            setLoading(true);
            setTimeout(async () => {
              const data = await apiClient.getMovieByTitle(title);
              if (data.url) {
                setVideoUrl(data.url);
                setLoading(false);
              }
            }, 5000); // Wait 5 seconds before fetching the video URL again
          } else {
            setDownloadProgress(progressData.progress);
          }
        } catch (err) {
          setError("Failed to fetch download progress");
        }
      }, 5000);

      return () => clearInterval(intervalId);
    }
  }, [downloadProgressUrl, title]);

  return (
    <div className="film-player-container">
      {loading && (
        <div className="loading">
          <Spinner />
        </div>
      )}
      {error && <Alert variant="danger">{error}</Alert>}
      {downloadProgressUrl && downloadProgress ? (
        <div className="download-status">
          <Alert variant="info">
            <strong>{downloadProgress.status}</strong> -{" "}
            {downloadProgress.progress.toFixed(2)}%{" "}
            <p>
              Wait for {downloadProgress.eta.toFixed(1)} Seconds
            </p>
          </Alert>
        </div>
      ) : (
        videoUrl && (
          <MoviePlayer
            videoUrl={videoUrl}
            title={`${metadata.title} (${metadata.year})`}
          />
        )
      )}
    </div>
  );
}