aboutsummaryrefslogtreecommitdiff
path: root/components/videoPlayer.js
blob: c680430efc93a8682480150062161751a0676106 (plain) (blame)
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
import Player from "../lib/Artplayer";
import { useEffect, useState } from "react";

export default function VideoPlayer({ data, seek, titles, id }) {
  const [url, setUrl] = useState();
  const [source, setSource] = useState([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    async function compiler() {
      try {
        const dataEpi = data.sources;
        const referer = data.headers.Referer;
        let sumber = dataEpi.find((source) => source.quality === "default");

        const source = data.sources
          .map((items) => ({
            html: items.quality,
            url: `https://cors.moopa.my.id/?url=${encodeURIComponent(
              items.url
            )}&referer=${encodeURIComponent(referer)}`,
          }))
          .sort((a, b) => {
            if (a.html === "default") return -1;
            if (b.html === "default") return 1;
            return 0;
          });

        const defUrl = `https://cors.moopa.my.id/?url=${encodeURIComponent(
          sumber.url
        )}&referer=${encodeURIComponent(referer)}`;

        setUrl(defUrl);
        setSource(source);
        setLoading(false);
      } catch (error) {
        console.error(error);
      }
    }
    compiler();
  }, [data]);

  return (
    <>
      {loading ? (
        ""
      ) : (
        <Player
          option={{
            url: `${url}`,
            quality: [source],
            // autoplay: true,
            screenshot: true,
            type: "m3u8",
          }}
          style={{ width: "100%", height: "100%", margin: "0 auto 0" }}
          getInstance={(art) => {
            art.on("ready", () => {
              const seekTime = seek;
              const duration = art.duration;
              const percentage = seekTime / duration;

              if (percentage >= 0.9) {
                // use >= instead of >
                art.currentTime = 0;
                console.log("Video restarted from the beginning");
              } else {
                art.currentTime = seek;
              }
            });

            art.on("destroy", () => {
              const lastPlayed = {
                id: id,
                time: art.currentTime,
              };

              const title = titles;
              const prevDataStr = localStorage.getItem("lastPlayed") || "[]";
              const prevData = JSON.parse(prevDataStr);
              let titleExists = false;

              prevData.forEach((item) => {
                if (item.title === title) {
                  const foundIndex = item.data.findIndex((e) => e.id === id);
                  if (foundIndex !== -1) {
                    item.data[foundIndex] = lastPlayed;
                  } else {
                    item.data.push(lastPlayed);
                  }
                  titleExists = true;
                }
              });

              if (!titleExists) {
                prevData.push({
                  title: title,
                  data: [lastPlayed],
                });
              }

              localStorage.setItem("lastPlayed", JSON.stringify(prevData));
            });
          }}
        />
      )}
    </>
  );
}