diff --git a/src/lib/getDownloads.ts b/src/lib/getDownloads.ts index 02f9ead..c317481 100755 --- a/src/lib/getDownloads.ts +++ b/src/lib/getDownloads.ts @@ -1,9 +1,14 @@ -import { Download } from "../types/download"; +import { Download, Task, Tasks } from "../types/download"; import { getTAUrl } from "./constants"; + + const TA_BASE_URL = getTAUrl(); export const getDownloads = async (token: string, filter: boolean, pageNumber: number): Promise => { + if (!token) { + throw new Error(`Unable to fetch downloads, no token provided.`); + } const response = await fetch(`${TA_BASE_URL.server}/api/download/?filter=${filter ? 'ignore' : 'pending'}&page=${pageNumber}`, { headers: { Accept: "application/json", @@ -28,6 +33,9 @@ export const getDownloads = async (token: string, filter: boolean, pageNumber: n }; export const sendDownloads = async (token: string, input: string): Promise => { + if (!token) { + throw new Error(`Unable to send downloads, no token provided.`); + } var data = { "data": [{ "youtube_id": input, @@ -42,7 +50,7 @@ export const sendDownloads = async (token: string, input: string): Promise => { + if (!token) { + throw new Error(`Unable to delete downloads, no token provided.`); + } const response = await fetch(`${TA_BASE_URL.server}/api/download/?filter=${filter}`, { headers: { Accept: "application/json", @@ -62,7 +73,7 @@ export const sendDeleteAllQueuedIgnored = async (token: string, filter: string): Authorization: `Token ${token}`, mode: "no-cors", }, - method: "DELETE" + method: "DELETE", }); if (!response.ok) { throw new Error("Error removing all videos."); @@ -72,6 +83,9 @@ export const sendDeleteAllQueuedIgnored = async (token: string, filter: string): }; export const sendDeleteVideoQueuedIgnored = async (token: string, videoId: string): Promise => { + if (!token) { + throw new Error(`Unable to delete downloads, no token provided.`); + } const response = await fetch(`${TA_BASE_URL.server}/api/download/${videoId}/`, { headers: { Accept: "application/json", @@ -79,7 +93,7 @@ export const sendDeleteVideoQueuedIgnored = async (token: string, videoId: strin Authorization: `Token ${token}`, mode: "no-cors", }, - method: "DELETE" + method: "DELETE", }); if (!response.ok) { throw new Error("Error removing video."); @@ -89,6 +103,9 @@ export const sendDeleteVideoQueuedIgnored = async (token: string, videoId: strin }; export const sendMoveVideoQueuedIgnored = async (token: string, videoId: string, status: string): Promise => { + if (!token) { + throw new Error(`Unable to move downloads, no token provided.`); + } var data = { "status": status }; @@ -100,10 +117,33 @@ export const sendMoveVideoQueuedIgnored = async (token: string, videoId: string, Authorization: `Token ${token}`, mode: "no-cors", }, - method: "POST" + method: "POST", }); if (!response.ok) { throw new Error("Error moving video to" + status + "."); } return response.json(); +}; + +export const sendTasks = async (token: string, task: Tasks): Promise => { + if (!token) { + throw new Error(`Unable to start task, no token provided.`); + } + var data = { + "run": task + }; + const response = await fetch(`${TA_BASE_URL.server}/api/task/`, { + body: JSON.stringify(data), + headers: { + Accept: "application/json", + "Content-Type": "application/json", + Authorization: `Token ${token}`, + mode: "no-cors", + }, + method: "POST", + }); + if (!response.ok) { + throw new Error(`Error running task: ${task}.`); + } + return response.json(); }; \ No newline at end of file diff --git a/src/pages/download.tsx b/src/pages/download.tsx index 3682de5..2de63fd 100755 --- a/src/pages/download.tsx +++ b/src/pages/download.tsx @@ -5,8 +5,7 @@ import { dehydrate, QueryClient, useQuery } from "react-query"; import { CustomHead } from "../components/CustomHead"; import { Layout } from "../components/Layout"; import NextImage from "next/image"; -import { getDownloads, sendDeleteAllQueuedIgnored, sendDeleteVideoQueuedIgnored, sendMoveVideoQueuedIgnored } from "../lib/getDownloads"; -import { sendDownloads } from "../lib/getDownloads"; +import { getDownloads, sendDownloads, sendDeleteAllQueuedIgnored, sendDeleteVideoQueuedIgnored, sendMoveVideoQueuedIgnored, sendTasks } from "../lib/getDownloads"; import RescanIcon from "../images/icon-rescan.svg"; import DownloadIcon from "../images/icon-download.svg"; import AddIcon from "../images/icon-add.svg"; @@ -15,6 +14,7 @@ import ListViewIcon from "../images/icon-listview.svg"; import StopIcon from "../images/icon-stop.svg"; import CloseIcon from "../images/icon-close.svg"; import { getTAUrl } from "../lib/constants"; +import { Tasks } from "../types/download"; const TA_BASE_URL = getTAUrl(); @@ -124,6 +124,18 @@ const Download: NextPage = () => { .catch(error => handleSetErrorMessage(error.message)); } + const handleSendTask = (session: string, task: Tasks) => { + sendTasks(session, task).then((response) => { + if (response.success) { + handleSetErrorMessage(null); + } else { + handleSetErrorMessage(`Error running task: ${response.task}.`); + } + + }) + .catch(error => handleSetErrorMessage(error.message)); + } + return ( <> @@ -202,7 +214,7 @@ const Download: NextPage = () => { alt="rescan-icon" title="Rescan subscriptions" // className="rotate-img" // Set when rescanning - onClick={() => console.log("rescanPending()")} + onClick={() => handleSendTask(session.ta_token.token, "rescan_pending")} /> {/* rescan-icon */}

Rescan subscriptions

@@ -215,7 +227,7 @@ const Download: NextPage = () => { alt="download-icon" title="Start download" // className="bounce-img" // Set when video is downloading - onClick={() => console.log("dlPending()")} + onClick={() => handleSendTask(session.ta_token.token, "download_pending")} /> {/* download-icon */}

Start download

diff --git a/src/types/download.ts b/src/types/download.ts index c14eadb..2f4beb6 100755 --- a/src/types/download.ts +++ b/src/types/download.ts @@ -5,6 +5,13 @@ export interface Download { message: string; } +export interface Task { + success: boolean; + task: string; +} + +export type Tasks = "download_pending" | "rescan_pending"; + export interface Paginate { page_size: number; page_from: number; @@ -16,11 +23,6 @@ export interface Paginate { total_hits: number; } -// export interface DownloadResponse { -// data: Datum[]; -// message: string; -// } - export interface Config { archive: Archive; default_view: DefaultView; @@ -106,19 +108,6 @@ export interface Datum { title: string; vid_thumb_url: string; youtube_id: string; - - // active: boolean; - // category: Category[]; - // channel: Channel; - // date_downloaded: number; - // description: string; - // media_url: string; - // player: Player; - // playlist: Playlist[]; - // stats: Stats; - // tags: string[]; - // vid_last_refresh: LastRefresh; - // vid_thumb_base64: string; } export enum Category {