2021-07-14 00:31:37 +02:00
|
|
|
import React from 'react'
|
|
|
|
import { Title } from '../components/Title'
|
|
|
|
import { Card } from '../components/Card'
|
|
|
|
import { useMovie } from '../hooks/useMovie'
|
|
|
|
import { VideoElement } from '../components/VideoElement'
|
2021-07-15 00:09:42 +02:00
|
|
|
import { EpisodeSelector } from '../components/EpisodeSelector'
|
2021-07-20 12:20:56 +02:00
|
|
|
import { getStreamUrl } from '../lib/index'
|
|
|
|
|
2021-07-15 00:09:42 +02:00
|
|
|
import './Movie.css'
|
2021-07-14 00:31:37 +02:00
|
|
|
|
|
|
|
export function MovieView(props) {
|
2021-07-15 00:09:42 +02:00
|
|
|
const { streamUrl, streamData, setStreamUrl } = useMovie();
|
|
|
|
const [season, setSeason] = React.useState("1");
|
|
|
|
const [seasonList, setSeasonList] = React.useState([]);
|
|
|
|
const [episodeLists, setEpisodeList] = React.useState([]);
|
|
|
|
const [episode, setEpisode] = React.useState({ episode: null, season: null });
|
|
|
|
const [loading, setLoading] = React.useState(false);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
setEpisodeList(streamData.episodes[season]);
|
|
|
|
}, [season, streamData.episodes])
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (streamData.type === "show") {
|
|
|
|
setSeasonList(streamData.seasons);
|
|
|
|
setSeason(streamData.seasons[0])
|
|
|
|
// TODO load from localstorage last watched
|
|
|
|
setEpisode({ episode: streamData.episodes[streamData.seasons[0]][0], season: streamData.seasons[0] })
|
|
|
|
setEpisodeList(streamData.episodes[streamData.seasons[0]]);
|
|
|
|
}
|
|
|
|
}, [streamData])
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
let cancel = false;
|
|
|
|
// ignore if not a show
|
|
|
|
if (streamData.type !== "show") return () => {
|
|
|
|
cancel = true;
|
|
|
|
};
|
|
|
|
if (!episode.episode) {
|
|
|
|
setLoading(false);
|
|
|
|
setStreamUrl('');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setLoading(true);
|
|
|
|
|
2021-07-21 00:49:33 +02:00
|
|
|
getStreamUrl(streamData.slug, streamData.type, streamData.source, episode.season, episode.episode)
|
2021-07-15 00:09:42 +02:00
|
|
|
.then(({url}) => {
|
|
|
|
if (cancel) return;
|
|
|
|
setStreamUrl(url)
|
|
|
|
setLoading(false);
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
if (cancel) return;
|
|
|
|
console.error(e)
|
|
|
|
})
|
|
|
|
return () => {
|
|
|
|
cancel = true;
|
2021-07-15 21:04:47 +02:00
|
|
|
}
|
2021-07-15 00:09:42 +02:00
|
|
|
}, [episode, streamData, setStreamUrl])
|
2021-07-14 00:31:37 +02:00
|
|
|
|
2021-07-15 20:34:25 +02:00
|
|
|
const setProgress = (evt) => {
|
|
|
|
let ls = JSON.parse(localStorage.getItem("video-progress") || "{}")
|
|
|
|
|
|
|
|
// We're just checking lookmovie for now since there is only one scraper
|
2021-07-21 12:29:18 +02:00
|
|
|
if(!ls[streamData.source]) ls[streamData.source] = {}
|
|
|
|
if(!ls[streamData.source][streamData.type]) ls[streamData.source][streamData.type] = {}
|
|
|
|
if(!ls[streamData.source][streamData.type][streamData.slug]) {
|
|
|
|
ls[streamData.source][streamData.type][streamData.slug] = {}
|
2021-07-15 20:34:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store real data
|
|
|
|
let key = streamData.type === "show" ? `${season}-${episode.episode}` : "full"
|
2021-07-21 12:29:18 +02:00
|
|
|
ls[streamData.source][streamData.type][streamData.slug][key] = {
|
2021-07-15 20:34:25 +02:00
|
|
|
currentlyAt: Math.floor(evt.currentTarget.currentTime),
|
2021-07-15 21:04:47 +02:00
|
|
|
totalDuration: Math.floor(evt.currentTarget.duration),
|
|
|
|
updatedAt: Date.now()
|
2021-07-15 20:34:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(streamData.type === "show") {
|
2021-07-21 12:29:18 +02:00
|
|
|
ls[streamData.source][streamData.type][streamData.slug][key].show = {
|
2021-07-15 20:34:25 +02:00
|
|
|
season,
|
|
|
|
episode: episode.episode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem("video-progress", JSON.stringify(ls))
|
|
|
|
}
|
|
|
|
|
2021-07-14 00:31:37 +02:00
|
|
|
return (
|
2021-07-15 00:09:42 +02:00
|
|
|
<div className={`cardView showType-${streamData.type}`}>
|
2021-07-14 00:31:37 +02:00
|
|
|
<Card fullWidth>
|
|
|
|
<Title accent="Return to home" accentLink="search">
|
2021-07-15 00:09:42 +02:00
|
|
|
{streamData.title}
|
2021-07-14 00:31:37 +02:00
|
|
|
</Title>
|
2021-07-15 00:09:42 +02:00
|
|
|
{streamData.type === "show" ? <Title size="small">
|
|
|
|
Season {episode.season}: Episode {episode.episode}
|
|
|
|
</Title> : undefined}
|
2021-07-15 20:34:25 +02:00
|
|
|
<VideoElement streamUrl={streamUrl} loading={loading} setProgress={setProgress} />
|
2021-07-15 00:09:42 +02:00
|
|
|
{streamData.type === "show" ?
|
|
|
|
<EpisodeSelector
|
|
|
|
setSeason={setSeason}
|
|
|
|
setEpisode={setEpisode}
|
2021-07-15 21:04:47 +02:00
|
|
|
season={season}
|
2021-07-15 00:09:42 +02:00
|
|
|
seasons={seasonList}
|
|
|
|
episodes={episodeLists}
|
2021-07-15 21:04:47 +02:00
|
|
|
slug={streamData.slug}
|
2021-07-15 00:09:42 +02:00
|
|
|
currentSeason={season}
|
|
|
|
currentEpisode={episode}
|
2021-07-21 00:49:33 +02:00
|
|
|
source={streamData.source}
|
2021-07-15 00:09:42 +02:00
|
|
|
/>
|
|
|
|
: ''}
|
2021-07-14 00:31:37 +02:00
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|