movie-web/src/views/Search.js

188 lines
7.1 KiB
JavaScript
Raw Normal View History

2021-07-14 00:31:37 +02:00
import React from 'react';
2021-08-02 14:15:18 +02:00
import { Redirect, useRouteMatch, useHistory } from 'react-router-dom';
2021-08-02 15:15:24 +02:00
import { Helmet } from 'react-helmet';
2021-07-20 12:20:56 +02:00
import { InputBox } from '../components/InputBox';
import { Title } from '../components/Title';
import { Card } from '../components/Card';
import { ErrorBanner } from '../components/ErrorBanner';
import { MovieRow } from '../components/MovieRow';
import { Arrow } from '../components/Arrow';
import { Progress } from '../components/Progress';
import { findContent, getStreamUrl, getEpisodes } from '../lib/index';
2021-07-14 00:31:37 +02:00
import { useMovie } from '../hooks/useMovie';
2021-07-20 12:20:56 +02:00
import { TypeSelector } from '../components/TypeSelector';
2021-07-14 00:31:37 +02:00
2021-07-20 12:20:56 +02:00
import './Search.css';
2021-08-02 13:45:22 +02:00
import { DiscordBanner } from '../components/DiscordBanner';
2021-07-14 00:31:37 +02:00
export function SearchView() {
const { navigate, setStreamUrl, setStreamData } = useMovie();
2021-08-02 14:15:18 +02:00
const history = useHistory();
const routeMatch = useRouteMatch('/:type');
const type = routeMatch?.params?.type;
const streamRouteMatch = useRouteMatch('/:type/:source/:title/:slug');
2021-07-15 00:09:42 +02:00
const maxSteps = 4;
2021-07-14 00:31:37 +02: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-08-02 14:15:18 +02:00
const [errorStatus, setErrorStatus] = React.useState(false);
2021-07-14 00:31:37 +02:00
const fail = (str) => {
setProgress(maxSteps);
setText(str)
setFailed(true)
}
2021-07-21 00:49:33 +02:00
async function getStream(title, slug, type, source) {
2021-07-14 00:31:37 +02:00
setStreamUrl("");
2021-07-15 00:09:42 +02:00
2021-07-14 00:31:37 +02:00
try {
setProgress(2);
2021-08-02 14:15:18 +02:00
setText(`Getting stream for "${title}"`);
2021-07-14 19:03:10 +02:00
2021-07-15 00:09:42 +02:00
let seasons = [];
let episodes = [];
if (type === "show") {
2021-07-21 00:49:33 +02:00
const data = await getEpisodes(slug, source);
2021-07-19 16:32:40 +02:00
seasons = data.seasons;
episodes = data.episodes;
2021-07-15 00:09:42 +02:00
}
let realUrl = '';
if (type === "movie") {
2021-07-21 00:49:33 +02:00
// getStreamUrl(slug, type, source, season, episode)
const { url } = await getStreamUrl(slug, type, source);
2021-07-22 22:11:53 +02:00
2021-07-15 00:09:42 +02:00
if (url === '') {
return fail(`Not found: ${title}`)
}
realUrl = url;
2021-07-14 19:03:10 +02:00
}
2021-07-14 00:31:37 +02:00
setProgress(maxSteps);
2021-07-15 00:09:42 +02:00
setStreamUrl(realUrl);
2021-07-14 00:31:37 +02:00
setStreamData({
title,
type,
2021-07-15 00:09:42 +02:00
seasons,
episodes,
2021-07-21 00:49:33 +02:00
slug,
source
2021-07-14 00:31:37 +02:00
})
setText(`Streaming...`)
navigate("movie")
} catch (err) {
2021-07-20 12:20:56 +02:00
console.error(err);
2021-07-14 00:31:37 +02:00
fail("Failed to get stream")
}
}
2021-07-15 00:09:42 +02:00
async function searchMovie(query, contentType) {
2021-07-14 00:31:37 +02:00
setFailed(false);
2021-07-15 00:09:42 +02:00
setText(`Searching for ${contentType} "${query}"`);
2021-07-14 00:31:37 +02:00
setProgress(1)
setShowingOptions(false)
try {
2021-07-21 00:49:33 +02:00
const { options } = await findContent(query, contentType);
2021-07-14 00:31:37 +02:00
if (options.length === 0) {
2021-07-14 17:15:25 +02:00
return fail(`Could not find that ${contentType}`)
2021-07-14 00:31:37 +02:00
} else if (options.length > 1) {
setProgress(2);
2021-07-14 17:15:25 +02:00
setText(`Choose your ${contentType}`);
setOptions(options);
setShowingOptions(true);
2021-07-14 00:31:37 +02:00
return;
}
2021-07-21 00:49:33 +02:00
const { title, slug, type, source } = options[0];
2021-08-02 14:15:18 +02:00
history.push(`${routeMatch.url}/${source}/${title}/${slug}`);
2021-07-21 00:49:33 +02:00
getStream(title, slug, type, source);
2021-07-14 00:31:37 +02:00
} catch (err) {
2021-07-21 00:49:33 +02:00
console.error(err);
2021-07-14 17:15:25 +02:00
fail(`Failed to watch ${contentType}`)
2021-07-14 00:31:37 +02:00
}
}
2021-07-16 19:31:26 +02:00
React.useEffect(() => {
async function fetchHealth() {
2021-07-22 22:30:08 +02:00
const HOME_URL = "https://movie-web-proxy.herokuapp.com"
2021-07-16 19:31:26 +02:00
await fetch(HOME_URL).catch(() => {
// Request failed; source likely offline
2021-08-02 14:15:18 +02:00
setErrorStatus(`Our content provider is currently offline, apologies.`)
2021-07-16 19:31:26 +02:00
})
}
fetchHealth()
2021-08-02 14:15:18 +02:00
}, []);
React.useEffect(() => {
if (streamRouteMatch) {
2021-08-02 15:45:10 +02:00
if (streamRouteMatch?.params.type === 'movie' || streamRouteMatch.params.type === 'show') getStream(streamRouteMatch.params.title, streamRouteMatch.params.slug, streamRouteMatch.params.type, streamRouteMatch.params.source);
else return setErrorStatus("Failed to find movie. Please try searching below.");
2021-08-02 14:15:18 +02:00
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
if (!type || (type !== 'movie' && type !== 'show')) return <Redirect to="/movie" />
2021-07-16 19:31:26 +02:00
2021-07-14 00:31:37 +02:00
return (
<div className="cardView">
2021-08-02 15:15:24 +02:00
<Helmet>
<title>{type === 'movie' ? 'Movies' : 'TV Shows'} | movie-web</title>
</Helmet>
2021-08-02 15:22:13 +02:00
2021-07-14 00:31:37 +02:00
<Card>
2021-08-02 13:45:22 +02:00
<DiscordBanner />
2021-08-02 14:15:18 +02:00
{errorStatus ? <ErrorBanner>{errorStatus}</ErrorBanner> : ''}
2021-07-14 17:15:25 +02:00
<Title accent="Because watching content legally is boring">
What do you wanna watch?
2021-07-14 00:31:37 +02:00
</Title>
2021-07-15 00:09:42 +02:00
<TypeSelector
2021-08-02 14:15:18 +02:00
setType={(type) => history.push(`/${type}`)}
2021-07-15 00:09:42 +02:00
choices={[
{ label: "Movie", value: "movie" },
{ label: "TV Show", value: "show" }
]}
noWrap={true}
2021-07-15 00:09:42 +02:00
selected={type}
/>
2021-07-22 22:11:53 +02:00
<InputBox placeholder={type === "movie" ? "Hamilton" : "Atypical"} onSubmit={(str) => searchMovie(str, type)} />
2021-07-14 00:31:37 +02:00
<Progress show={progress > 0} failed={failed} progress={progress} steps={maxSteps} text={text} />
</Card>
<Card show={showingOptions} doTransition>
<Title size="medium">
2021-07-15 00:09:42 +02:00
Whoops, there are a few {type}s like that
2021-07-14 00:31:37 +02:00
</Title>
2021-07-22 22:11:53 +02:00
{ Object.entries(options.reduce((a, v) => {
if (!a[v.source]) a[v.source] = []
a[v.source].push(v)
return a;
}, {})).map(v => (
<div key={v[0]}>
<p className="source">{v[0]}</p>
{v[1].map((v, i) => (
<MovieRow key={i} title={v.title} slug={v.slug} type={v.type} year={v.year} source={v.source} onClick={() => {
2021-08-02 14:15:18 +02:00
history.push(`${routeMatch.url}/${v.source}/${v.title}/${v.slug}`);
2021-07-22 22:11:53 +02:00
setShowingOptions(false)
getStream(v.title, v.slug, v.type, v.source)
}} />
))}
</div>
))
}
2021-07-14 00:31:37 +02:00
</Card>
2021-07-14 01:17:34 +02:00
<div className="topRightCredits">
2021-08-02 14:44:54 +02:00
<a href="https://github.com/JamesHawkinss/movie-web" target="_blank" rel="noreferrer">Check it out on GitHub <Arrow /></a>
<br />
<a href="https://discord.gg/vXsRvye8BS" target="_blank" rel="noreferrer">Join the Discord <Arrow /></a>
2021-07-14 01:17:34 +02:00
</div>
2021-07-14 00:31:37 +02:00
</div>
)
}