diff --git a/apps/expo/src/app/videoPlayer.tsx b/apps/expo/src/app/videoPlayer.tsx index 618dd1d..2d00d3c 100644 --- a/apps/expo/src/app/videoPlayer.tsx +++ b/apps/expo/src/app/videoPlayer.tsx @@ -17,6 +17,7 @@ export default function VideoPlayerWrapper() { const data = params.data ? (JSON.parse(params.data as string) as ItemData) : null; + const download = params.download === "true"; if (!data) return router.back(); @@ -26,6 +27,10 @@ export default function VideoPlayerWrapper() { return ; } + if (download) { + return ; + } + if (playerStatus === PlayerStatus.SCRAPING) { return ; } diff --git a/apps/expo/src/components/item/item.tsx b/apps/expo/src/components/item/item.tsx index 0b1cd53..0c32056 100644 --- a/apps/expo/src/components/item/item.tsx +++ b/apps/expo/src/components/item/item.tsx @@ -5,7 +5,6 @@ import ContextMenu from "react-native-context-menu-view"; import { useRouter } from "expo-router"; import { Image, Text, View } from "tamagui"; -import { useDownloadManager } from "~/hooks/DownloadManagerContext"; import { usePlayerStore } from "~/stores/player/store"; export interface ItemData { @@ -19,7 +18,6 @@ export interface ItemData { export default function Item({ data }: { data: ItemData }) { const resetVideo = usePlayerStore((state) => state.resetVideo); const router = useRouter(); - const { startDownload } = useDownloadManager(); const { title, type, year, posterUrl } = data; @@ -40,11 +38,12 @@ export default function Item({ data }: { data: ItemData }) { const onContextMenuPress = ( e: NativeSyntheticEvent, ) => { - console.log(e.nativeEvent.name); - startDownload( - "https://samplelib.com/lib/preview/mp4/sample-5s.mp4", - "mp4", - ).catch(console.error); + if (e.nativeEvent.name === "Download") { + router.push({ + pathname: "/videoPlayer", + params: { data: JSON.stringify(data), download: "true" }, + }); + } }; return ( diff --git a/apps/expo/src/components/player/ScraperProcess.tsx b/apps/expo/src/components/player/ScraperProcess.tsx index 39375bf..277754c 100644 --- a/apps/expo/src/components/player/ScraperProcess.tsx +++ b/apps/expo/src/components/player/ScraperProcess.tsx @@ -5,10 +5,14 @@ import { useRouter } from "expo-router"; import { View } from "tamagui"; import type { HlsBasedStream } from "@movie-web/provider-utils"; -import { extractTracksFromHLS } from "@movie-web/provider-utils"; +import { + extractTracksFromHLS, + findHighestQuality, +} from "@movie-web/provider-utils"; import type { ItemData } from "../item/item"; import type { AudioTrack } from "./AudioTrackSelector"; +import { useDownloadManager } from "~/hooks/DownloadManagerContext"; import { useMeta } from "~/hooks/player/useMeta"; import { useScrape } from "~/hooks/player/useSourceScrape"; import { constructFullUrl } from "~/lib/url"; @@ -19,10 +23,12 @@ import { ScrapeCard, ScrapeItem } from "./ScrapeCard"; interface ScraperProcessProps { data: ItemData; + download?: boolean; } -export const ScraperProcess = ({ data }: ScraperProcessProps) => { +export const ScraperProcess = ({ data, download }: ScraperProcessProps) => { const router = useRouter(); + const { startDownload } = useDownloadManager(); const scrollViewRef = useRef(null); @@ -43,6 +49,17 @@ export const ScraperProcess = ({ data }: ScraperProcessProps) => { const streamResult = await startScraping(convertMetaToScrapeMedia(meta)); if (!streamResult) return router.back(); + if (download) { + if (streamResult.stream.type === "file") { + const highestQuality = findHighestQuality(streamResult.stream); + const url = highestQuality + ? streamResult.stream.qualities[highestQuality]?.url + : null; + if (!url) return; + startDownload(url, "mp4").catch(console.error); + } + return router.back(); + } setStream(streamResult.stream); if (streamResult.stream.type === "hls") { @@ -88,12 +105,14 @@ export const ScraperProcess = ({ data }: ScraperProcessProps) => { }, [ convertMovieIdToMeta, data, + download, router, setAudioTracks, setHlsTracks, setPlayerStatus, setSourceId, setStream, + startDownload, startScraping, ]);