sudo-archive/src/views/Search.js

155 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-07-13 22:31:37 +00:00
import React from 'react';
import { InputBox } from '../components/InputBox'
import { Title } from '../components/Title'
import { Card } from '../components/Card'
2021-07-16 17:31:26 +00:00
import { ErrorBanner } from '../components/ErrorBanner'
2021-07-13 22:31:37 +00:00
import { MovieRow } from '../components/MovieRow'
2021-07-13 23:17:34 +00:00
import { Arrow } from '../components/Arrow'
2021-07-13 22:31:37 +00:00
import { Progress } from '../components/Progress'
2021-07-16 17:31:26 +00:00
import { findContent, getStreamUrl, getEpisodes } from '../lib/lookMovie'
2021-07-13 22:31:37 +00:00
import { useMovie } from '../hooks/useMovie';
2021-07-14 22:09:42 +00:00
import { TypeSelector } from '../components/TypeSelector'
2021-07-13 22:31:37 +00:00
import './Search.css'
export function SearchView() {
const { navigate, setStreamUrl, setStreamData } = useMovie();
2021-07-14 22:09:42 +00:00
const maxSteps = 4;
2021-07-13 22:31:37 +00:00
const [options, setOptions] = React.useState([]);
const [progress, setProgress] = React.useState(0);
const [text, setText] = React.useState("");
const [failed, setFailed] = React.useState(false);
const [showingOptions, setShowingOptions] = React.useState(false);
2021-07-16 17:31:26 +00:00
const [offlineStatus, setOfflineStatus] = React.useState(false);
2021-07-14 22:09:42 +00:00
const [type, setType] = React.useState("movie");
2021-07-13 22:31:37 +00:00
const fail = (str) => {
setProgress(maxSteps);
setText(str)
setFailed(true)
}
2021-07-14 22:09:42 +00:00
async function getStream(title, slug, type) {
2021-07-13 22:31:37 +00:00
setStreamUrl("");
2021-07-14 22:09:42 +00:00
2021-07-13 22:31:37 +00:00
try {
setProgress(2);
setText(`Getting stream for "${title}"`)
2021-07-14 17:03:10 +00:00
2021-07-14 22:09:42 +00:00
let seasons = [];
let episodes = [];
if (type === "show") {
const episodeData = await getEpisodes(slug);
episodeData.forEach((e) => {
if (!seasons.includes(e.season))
seasons.push(e.season);
if (!episodes[e.season])
episodes[e.season] = []
episodes[e.season].push(e.episode)
})
}
let realUrl = '';
if (type === "movie") {
const { url } = await getStreamUrl(slug, type);
if (url === '') {
return fail(`Not found: ${title}`)
}
realUrl = url;
2021-07-14 17:03:10 +00:00
}
2021-07-13 22:31:37 +00:00
setProgress(maxSteps);
2021-07-14 22:09:42 +00:00
setStreamUrl(realUrl);
2021-07-13 22:31:37 +00:00
setStreamData({
title,
type,
2021-07-14 22:09:42 +00:00
seasons,
episodes,
slug
2021-07-13 22:31:37 +00:00
})
setText(`Streaming...`)
navigate("movie")
} catch (err) {
fail("Failed to get stream")
}
}
2021-07-14 22:09:42 +00:00
async function searchMovie(query, contentType) {
2021-07-13 22:31:37 +00:00
setFailed(false);
2021-07-14 22:09:42 +00:00
setText(`Searching for ${contentType} "${query}"`);
2021-07-13 22:31:37 +00:00
setProgress(1)
setShowingOptions(false)
try {
2021-07-14 15:15:25 +00:00
const { options } = await findContent(query, contentType)
2021-07-13 22:31:37 +00:00
if (options.length === 0) {
2021-07-14 15:15:25 +00:00
return fail(`Could not find that ${contentType}`)
2021-07-13 22:31:37 +00:00
} else if (options.length > 1) {
setProgress(2);
2021-07-14 15:15:25 +00:00
setText(`Choose your ${contentType}`);
setOptions(options);
setShowingOptions(true);
2021-07-13 22:31:37 +00:00
return;
}
const { title, slug, type } = options[0];
2021-07-14 22:09:42 +00:00
getStream(title, slug, type);
2021-07-13 22:31:37 +00:00
} catch (err) {
2021-07-14 15:15:25 +00:00
fail(`Failed to watch ${contentType}`)
2021-07-13 22:31:37 +00:00
}
}
2021-07-16 17:31:26 +00:00
React.useEffect(() => {
async function fetchHealth() {
const HOME_URL = "https://hidden-inlet-27205.herokuapp.com/https://lookmovie.io/"
await fetch(HOME_URL).catch(() => {
// Request failed; source likely offline
setOfflineStatus(`Our content provider is currently offline, apologies.`)
})
}
fetchHealth()
}, [])
2021-07-13 22:31:37 +00:00
return (
<div className="cardView">
<Card>
2021-07-16 17:31:26 +00:00
{offlineStatus ? <ErrorBanner>{offlineStatus}</ErrorBanner> : ''}
2021-07-14 15:15:25 +00:00
<Title accent="Because watching content legally is boring">
What do you wanna watch?
2021-07-13 22:31:37 +00:00
</Title>
2021-07-14 22:09:42 +00:00
<TypeSelector
setType={(type) => setType(type)}
choices={[
{ label: "Movie", value: "movie" },
{ label: "TV Show", value: "show" }
]}
noWrap={true}
2021-07-14 22:09:42 +00:00
selected={type}
/>
<InputBox placeholder={ type === "movie" ? "Hamilton" : "Atypical" } onSubmit={(str) => searchMovie(str, type)} />
2021-07-13 22:31:37 +00:00
<Progress show={progress > 0} failed={failed} progress={progress} steps={maxSteps} text={text} />
</Card>
<Card show={showingOptions} doTransition>
<Title size="medium">
2021-07-14 22:09:42 +00:00
Whoops, there are a few {type}s like that
2021-07-13 22:31:37 +00:00
</Title>
{options?.map((v, i) => (
<MovieRow key={i} title={v.title} slug={v.slug} type={v.type} year={v.year} season={v.season} episode={v.episode} onClick={() => {
2021-07-13 22:31:37 +00:00
setShowingOptions(false)
2021-07-14 17:03:10 +00:00
getStream(v.title, v.slug, v.type, v.season, v.episode)
2021-07-13 22:31:37 +00:00
}}/>
))}
</Card>
2021-07-13 23:17:34 +00:00
<div className="topRightCredits">
<a href="https://github.com/JamesHawkinss/movie-web" target="_blank" rel="noreferrer">Check it out on GitHub <Arrow/></a>
</div>
2021-07-13 22:31:37 +00:00
</div>
)
}