Merge pull request #4 from n8detar/master

Added re-scan subs and start download functionality to downloads page
This commit is contained in:
Sean 2022-09-10 21:58:49 -05:00 committed by GitHub
commit b246dbcd75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 27 deletions

View File

@ -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<Download> => {
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<Download> => {
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<Downl
Authorization: `Token ${token}`,
mode: "no-cors",
},
method: "POST"
method: "POST",
});
if (response.ok) {
return response.json();
@ -55,6 +63,9 @@ export const sendDownloads = async (token: string, input: string): Promise<Downl
};
export const sendDeleteAllQueuedIgnored = async (token: string, filter: string): Promise<Download> => {
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<Download> => {
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<Download> => {
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<Task> => {
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();
};

View File

@ -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 (
<>
<CustomHead title="Downloads" />
@ -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")}
/>
{/* <img id="rescan-icon" onclick="rescanPending()" src="{% static 'img/icon-rescan.svg' %}" alt="rescan-icon"></img> */}
<p>Rescan subscriptions</p>
@ -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")}
/>
{/* <img id="download-icon" onclick="dlPending()" src="{% static 'img/icon-download.svg' %}" alt="download-icon"></img> */}
<p>Start download</p>

View File

@ -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 {