mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 18:13:25 +00:00
feat: add scrapemedia to downloaditem
This commit is contained in:
@@ -5,6 +5,8 @@ import { useRouter } from "expo-router";
|
||||
import { MaterialCommunityIcons } from "@expo/vector-icons";
|
||||
import { useTheme, YStack } from "tamagui";
|
||||
|
||||
import type { ScrapeMedia } from "@movie-web/provider-utils";
|
||||
|
||||
import { DownloadItem } from "~/components/DownloadItem";
|
||||
import ScreenLayout from "~/components/layout/ScreenLayout";
|
||||
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 (
|
||||
<ScreenLayout>
|
||||
<YStack space={2} style={{ padding: 10 }}>
|
||||
<YStack gap={2} style={{ padding: 10 }}>
|
||||
<MWButton
|
||||
type="secondary"
|
||||
backgroundColor="$sheetItemBackground"
|
||||
@@ -44,6 +61,7 @@ const DownloadsScreen: React.FC = () => {
|
||||
await startDownload(
|
||||
"https://samplelib.com/lib/preview/mp4/sample-5s.mp4",
|
||||
"mp4",
|
||||
exampleShowMedia,
|
||||
).catch(console.error);
|
||||
}}
|
||||
>
|
||||
@@ -63,6 +81,7 @@ const DownloadsScreen: React.FC = () => {
|
||||
await startDownload(
|
||||
"http://sample.vodobox.com/skate_phantom_flex_4k/skate_phantom_flex_4k.m3u8",
|
||||
"hls",
|
||||
exampleShowMedia,
|
||||
).catch(console.error);
|
||||
}}
|
||||
>
|
||||
|
@@ -4,14 +4,20 @@ import { useTheme } from "tamagui";
|
||||
import { findHighestQuality } from "@movie-web/provider-utils";
|
||||
|
||||
import { useDownloadManager } from "~/hooks/DownloadManagerContext";
|
||||
import { convertMetaToScrapeMedia } from "~/lib/meta";
|
||||
import { usePlayerStore } from "~/stores/player/store";
|
||||
import { MWButton } from "../ui/Button";
|
||||
import { Controls } from "./Controls";
|
||||
|
||||
export const DownloadButton = () => {
|
||||
const theme = useTheme();
|
||||
const stream = usePlayerStore((state) => state.interface.currentStream);
|
||||
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;
|
||||
|
||||
if (stream?.type === "file") {
|
||||
@@ -36,7 +42,12 @@ export const DownloadButton = () => {
|
||||
/>
|
||||
}
|
||||
onPress={() =>
|
||||
url && startDownload(url, stream?.type === "hls" ? "hls" : "mp4")
|
||||
url &&
|
||||
startDownload(
|
||||
url,
|
||||
stream?.type === "hls" ? "hls" : "mp4",
|
||||
scrapeMedia,
|
||||
).catch(console.error)
|
||||
}
|
||||
>
|
||||
Download
|
||||
|
@@ -42,7 +42,7 @@ export const ScraperProcess = ({
|
||||
|
||||
const scrollViewRef = useRef<ScrollView>(null);
|
||||
|
||||
const { convertMovieIdToMeta } = useMeta();
|
||||
const { convertIdToMeta } = useMeta();
|
||||
const { startScraping, sourceOrder, sources, currentSource } = useScrape();
|
||||
|
||||
const setStream = usePlayerStore((state) => state.setCurrentStream);
|
||||
@@ -59,7 +59,7 @@ export const ScraperProcess = ({
|
||||
let meta: PlayerMeta | undefined = undefined;
|
||||
|
||||
if (!media && data) {
|
||||
meta = await convertMovieIdToMeta(data.id, data.type);
|
||||
meta = await convertIdToMeta(data.id, data.type);
|
||||
if (!meta) return router.back();
|
||||
}
|
||||
|
||||
@@ -75,9 +75,9 @@ export const ScraperProcess = ({
|
||||
? streamResult.stream.qualities[highestQuality]?.url
|
||||
: null;
|
||||
if (!url) return;
|
||||
startDownload(url, "mp4").catch(console.error);
|
||||
startDownload(url, "mp4", scrapeMedia).catch(console.error);
|
||||
} else if (streamResult.stream.type === "hls") {
|
||||
startDownload(streamResult.stream.playlist, "hls").catch(
|
||||
startDownload(streamResult.stream.playlist, "hls", scrapeMedia).catch(
|
||||
console.error,
|
||||
);
|
||||
}
|
||||
@@ -126,7 +126,7 @@ export const ScraperProcess = ({
|
||||
};
|
||||
void fetchData();
|
||||
}, [
|
||||
convertMovieIdToMeta,
|
||||
convertIdToMeta,
|
||||
data,
|
||||
download,
|
||||
media,
|
||||
|
@@ -6,6 +6,7 @@ import * as MediaLibrary from "expo-media-library";
|
||||
import VideoManager from "@salihgun/react-native-video-processor";
|
||||
import { useToastController } from "@tamagui/toast";
|
||||
|
||||
import type { ScrapeMedia } from "@movie-web/provider-utils";
|
||||
import { extractSegmentsFromHLS } from "@movie-web/provider-utils";
|
||||
|
||||
import { useDownloadHistoryStore } from "~/stores/settings";
|
||||
@@ -23,12 +24,18 @@ export interface DownloadItem {
|
||||
statusText?: string;
|
||||
asset?: Asset;
|
||||
isHLS?: boolean;
|
||||
media: ScrapeMedia;
|
||||
downloadResumable?: FileSystem.DownloadResumable;
|
||||
}
|
||||
|
||||
interface DownloadManagerContextType {
|
||||
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;
|
||||
cancelDownload: (id: string) => Promise<void>;
|
||||
}
|
||||
@@ -94,6 +101,7 @@ export const DownloadManagerProvider: React.FC<{ children: ReactNode }> = ({
|
||||
const startDownload = async (
|
||||
url: string,
|
||||
type: "mp4" | "hls",
|
||||
media: ScrapeMedia,
|
||||
headers?: Record<string, string>,
|
||||
): Promise<Asset | void> => {
|
||||
toastController.show("Download started", {
|
||||
@@ -113,6 +121,7 @@ export const DownloadManagerProvider: React.FC<{ children: ReactNode }> = ({
|
||||
url,
|
||||
isFinished: false,
|
||||
isHLS: type === "hls",
|
||||
media,
|
||||
};
|
||||
|
||||
setDownloads((currentDownloads) => [newDownload, ...currentDownloads]);
|
||||
|
@@ -9,7 +9,7 @@ export const useMeta = () => {
|
||||
const meta = usePlayerStore((state) => state.meta);
|
||||
const setMeta = usePlayerStore((state) => state.setMeta);
|
||||
|
||||
const convertMovieIdToMeta = useCallback(
|
||||
const convertIdToMeta = useCallback(
|
||||
async (id: string, type: "movie" | "tv") => {
|
||||
const media = await fetchMediaDetails(id, type);
|
||||
if (!media) return;
|
||||
@@ -54,5 +54,5 @@ export const useMeta = () => {
|
||||
[meta?.episode?.number, meta?.season?.number, setMeta],
|
||||
);
|
||||
|
||||
return { convertMovieIdToMeta };
|
||||
return { convertIdToMeta };
|
||||
};
|
||||
|
Reference in New Issue
Block a user