feat: add scrapemedia to downloaditem

This commit is contained in:
Adrian Castro
2024-03-27 10:40:16 +01:00
parent 4cfe7b6bfd
commit 42e6b1fe63
5 changed files with 50 additions and 11 deletions

View File

@@ -5,6 +5,8 @@ import { useRouter } from "expo-router";
import { MaterialCommunityIcons } from "@expo/vector-icons"; import { MaterialCommunityIcons } from "@expo/vector-icons";
import { useTheme, YStack } from "tamagui"; import { useTheme, YStack } from "tamagui";
import type { ScrapeMedia } from "@movie-web/provider-utils";
import { DownloadItem } from "~/components/DownloadItem"; import { DownloadItem } from "~/components/DownloadItem";
import ScreenLayout from "~/components/layout/ScreenLayout"; import ScreenLayout from "~/components/layout/ScreenLayout";
import { MWButton } from "~/components/ui/Button"; import { MWButton } from "~/components/ui/Button";
@@ -27,9 +29,24 @@ const DownloadsScreen: React.FC = () => {
}); });
}; };
const exampleShowMedia: ScrapeMedia = {
type: "show",
title: "Example Show Title",
releaseYear: 2022,
imdbId: "tt1234567",
tmdbId: "12345",
season: {
number: 1,
tmdbId: "54321",
},
episode: {
number: 3,
tmdbId: "98765",
},
};
return ( return (
<ScreenLayout> <ScreenLayout>
<YStack space={2} style={{ padding: 10 }}> <YStack gap={2} style={{ padding: 10 }}>
<MWButton <MWButton
type="secondary" type="secondary"
backgroundColor="$sheetItemBackground" backgroundColor="$sheetItemBackground"
@@ -44,6 +61,7 @@ const DownloadsScreen: React.FC = () => {
await startDownload( await startDownload(
"https://samplelib.com/lib/preview/mp4/sample-5s.mp4", "https://samplelib.com/lib/preview/mp4/sample-5s.mp4",
"mp4", "mp4",
exampleShowMedia,
).catch(console.error); ).catch(console.error);
}} }}
> >
@@ -63,6 +81,7 @@ const DownloadsScreen: React.FC = () => {
await startDownload( await startDownload(
"http://sample.vodobox.com/skate_phantom_flex_4k/skate_phantom_flex_4k.m3u8", "http://sample.vodobox.com/skate_phantom_flex_4k/skate_phantom_flex_4k.m3u8",
"hls", "hls",
exampleShowMedia,
).catch(console.error); ).catch(console.error);
}} }}
> >

View File

@@ -4,14 +4,20 @@ import { useTheme } from "tamagui";
import { findHighestQuality } from "@movie-web/provider-utils"; import { findHighestQuality } from "@movie-web/provider-utils";
import { useDownloadManager } from "~/hooks/DownloadManagerContext"; import { useDownloadManager } from "~/hooks/DownloadManagerContext";
import { convertMetaToScrapeMedia } from "~/lib/meta";
import { usePlayerStore } from "~/stores/player/store"; import { usePlayerStore } from "~/stores/player/store";
import { MWButton } from "../ui/Button"; import { MWButton } from "../ui/Button";
import { Controls } from "./Controls"; import { Controls } from "./Controls";
export const DownloadButton = () => { export const DownloadButton = () => {
const theme = useTheme(); const theme = useTheme();
const stream = usePlayerStore((state) => state.interface.currentStream);
const { startDownload } = useDownloadManager(); const { startDownload } = useDownloadManager();
const stream = usePlayerStore((state) => state.interface.currentStream);
const meta = usePlayerStore((state) => state.meta);
if (!meta) return null;
const scrapeMedia = convertMetaToScrapeMedia(meta);
let url: string | undefined | null = null; let url: string | undefined | null = null;
if (stream?.type === "file") { if (stream?.type === "file") {
@@ -36,7 +42,12 @@ export const DownloadButton = () => {
/> />
} }
onPress={() => onPress={() =>
url && startDownload(url, stream?.type === "hls" ? "hls" : "mp4") url &&
startDownload(
url,
stream?.type === "hls" ? "hls" : "mp4",
scrapeMedia,
).catch(console.error)
} }
> >
Download Download

View File

@@ -42,7 +42,7 @@ export const ScraperProcess = ({
const scrollViewRef = useRef<ScrollView>(null); const scrollViewRef = useRef<ScrollView>(null);
const { convertMovieIdToMeta } = useMeta(); const { convertIdToMeta } = useMeta();
const { startScraping, sourceOrder, sources, currentSource } = useScrape(); const { startScraping, sourceOrder, sources, currentSource } = useScrape();
const setStream = usePlayerStore((state) => state.setCurrentStream); const setStream = usePlayerStore((state) => state.setCurrentStream);
@@ -59,7 +59,7 @@ export const ScraperProcess = ({
let meta: PlayerMeta | undefined = undefined; let meta: PlayerMeta | undefined = undefined;
if (!media && data) { if (!media && data) {
meta = await convertMovieIdToMeta(data.id, data.type); meta = await convertIdToMeta(data.id, data.type);
if (!meta) return router.back(); if (!meta) return router.back();
} }
@@ -75,9 +75,9 @@ export const ScraperProcess = ({
? streamResult.stream.qualities[highestQuality]?.url ? streamResult.stream.qualities[highestQuality]?.url
: null; : null;
if (!url) return; if (!url) return;
startDownload(url, "mp4").catch(console.error); startDownload(url, "mp4", scrapeMedia).catch(console.error);
} else if (streamResult.stream.type === "hls") { } else if (streamResult.stream.type === "hls") {
startDownload(streamResult.stream.playlist, "hls").catch( startDownload(streamResult.stream.playlist, "hls", scrapeMedia).catch(
console.error, console.error,
); );
} }
@@ -126,7 +126,7 @@ export const ScraperProcess = ({
}; };
void fetchData(); void fetchData();
}, [ }, [
convertMovieIdToMeta, convertIdToMeta,
data, data,
download, download,
media, media,

View File

@@ -6,6 +6,7 @@ import * as MediaLibrary from "expo-media-library";
import VideoManager from "@salihgun/react-native-video-processor"; import VideoManager from "@salihgun/react-native-video-processor";
import { useToastController } from "@tamagui/toast"; import { useToastController } from "@tamagui/toast";
import type { ScrapeMedia } from "@movie-web/provider-utils";
import { extractSegmentsFromHLS } from "@movie-web/provider-utils"; import { extractSegmentsFromHLS } from "@movie-web/provider-utils";
import { useDownloadHistoryStore } from "~/stores/settings"; import { useDownloadHistoryStore } from "~/stores/settings";
@@ -23,12 +24,18 @@ export interface DownloadItem {
statusText?: string; statusText?: string;
asset?: Asset; asset?: Asset;
isHLS?: boolean; isHLS?: boolean;
media: ScrapeMedia;
downloadResumable?: FileSystem.DownloadResumable; downloadResumable?: FileSystem.DownloadResumable;
} }
interface DownloadManagerContextType { interface DownloadManagerContextType {
downloads: DownloadItem[]; downloads: DownloadItem[];
startDownload: (url: string, type: "mp4" | "hls") => Promise<Asset | void>; startDownload: (
url: string,
type: "mp4" | "hls",
media: ScrapeMedia,
headers?: Record<string, string>,
) => Promise<Asset | void>;
removeDownload: (id: string) => void; removeDownload: (id: string) => void;
cancelDownload: (id: string) => Promise<void>; cancelDownload: (id: string) => Promise<void>;
} }
@@ -94,6 +101,7 @@ export const DownloadManagerProvider: React.FC<{ children: ReactNode }> = ({
const startDownload = async ( const startDownload = async (
url: string, url: string,
type: "mp4" | "hls", type: "mp4" | "hls",
media: ScrapeMedia,
headers?: Record<string, string>, headers?: Record<string, string>,
): Promise<Asset | void> => { ): Promise<Asset | void> => {
toastController.show("Download started", { toastController.show("Download started", {
@@ -113,6 +121,7 @@ export const DownloadManagerProvider: React.FC<{ children: ReactNode }> = ({
url, url,
isFinished: false, isFinished: false,
isHLS: type === "hls", isHLS: type === "hls",
media,
}; };
setDownloads((currentDownloads) => [newDownload, ...currentDownloads]); setDownloads((currentDownloads) => [newDownload, ...currentDownloads]);

View File

@@ -9,7 +9,7 @@ export const useMeta = () => {
const meta = usePlayerStore((state) => state.meta); const meta = usePlayerStore((state) => state.meta);
const setMeta = usePlayerStore((state) => state.setMeta); const setMeta = usePlayerStore((state) => state.setMeta);
const convertMovieIdToMeta = useCallback( const convertIdToMeta = useCallback(
async (id: string, type: "movie" | "tv") => { async (id: string, type: "movie" | "tv") => {
const media = await fetchMediaDetails(id, type); const media = await fetchMediaDetails(id, type);
if (!media) return; if (!media) return;
@@ -54,5 +54,5 @@ export const useMeta = () => {
[meta?.episode?.number, meta?.season?.number, setMeta], [meta?.episode?.number, meta?.season?.number, setMeta],
); );
return { convertMovieIdToMeta }; return { convertIdToMeta };
}; };