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"; import { getTAUrl } from "./constants";
const TA_BASE_URL = getTAUrl(); const TA_BASE_URL = getTAUrl();
export const getDownloads = async (token: string, filter: boolean, pageNumber: number): Promise<Download> => { 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}`, { const response = await fetch(`${TA_BASE_URL.server}/api/download/?filter=${filter ? 'ignore' : 'pending'}&page=${pageNumber}`, {
headers: { headers: {
Accept: "application/json", 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> => { export const sendDownloads = async (token: string, input: string): Promise<Download> => {
if (!token) {
throw new Error(`Unable to send downloads, no token provided.`);
}
var data = { var data = {
"data": [{ "data": [{
"youtube_id": input, "youtube_id": input,
@ -42,7 +50,7 @@ export const sendDownloads = async (token: string, input: string): Promise<Downl
Authorization: `Token ${token}`, Authorization: `Token ${token}`,
mode: "no-cors", mode: "no-cors",
}, },
method: "POST" method: "POST",
}); });
if (response.ok) { if (response.ok) {
return response.json(); 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> => { 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}`, { const response = await fetch(`${TA_BASE_URL.server}/api/download/?filter=${filter}`, {
headers: { headers: {
Accept: "application/json", Accept: "application/json",
@ -62,7 +73,7 @@ export const sendDeleteAllQueuedIgnored = async (token: string, filter: string):
Authorization: `Token ${token}`, Authorization: `Token ${token}`,
mode: "no-cors", mode: "no-cors",
}, },
method: "DELETE" method: "DELETE",
}); });
if (!response.ok) { if (!response.ok) {
throw new Error("Error removing all videos."); 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> => { 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}/`, { const response = await fetch(`${TA_BASE_URL.server}/api/download/${videoId}/`, {
headers: { headers: {
Accept: "application/json", Accept: "application/json",
@ -79,7 +93,7 @@ export const sendDeleteVideoQueuedIgnored = async (token: string, videoId: strin
Authorization: `Token ${token}`, Authorization: `Token ${token}`,
mode: "no-cors", mode: "no-cors",
}, },
method: "DELETE" method: "DELETE",
}); });
if (!response.ok) { if (!response.ok) {
throw new Error("Error removing video."); 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> => { 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 = { var data = {
"status": status "status": status
}; };
@ -100,10 +117,33 @@ export const sendMoveVideoQueuedIgnored = async (token: string, videoId: string,
Authorization: `Token ${token}`, Authorization: `Token ${token}`,
mode: "no-cors", mode: "no-cors",
}, },
method: "POST" method: "POST",
}); });
if (!response.ok) { if (!response.ok) {
throw new Error("Error moving video to" + status + "."); throw new Error("Error moving video to" + status + ".");
} }
return response.json(); 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 { CustomHead } from "../components/CustomHead";
import { Layout } from "../components/Layout"; import { Layout } from "../components/Layout";
import NextImage from "next/image"; import NextImage from "next/image";
import { getDownloads, sendDeleteAllQueuedIgnored, sendDeleteVideoQueuedIgnored, sendMoveVideoQueuedIgnored } from "../lib/getDownloads"; import { getDownloads, sendDownloads, sendDeleteAllQueuedIgnored, sendDeleteVideoQueuedIgnored, sendMoveVideoQueuedIgnored, sendTasks } from "../lib/getDownloads";
import { sendDownloads } from "../lib/getDownloads";
import RescanIcon from "../images/icon-rescan.svg"; import RescanIcon from "../images/icon-rescan.svg";
import DownloadIcon from "../images/icon-download.svg"; import DownloadIcon from "../images/icon-download.svg";
import AddIcon from "../images/icon-add.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 StopIcon from "../images/icon-stop.svg";
import CloseIcon from "../images/icon-close.svg"; import CloseIcon from "../images/icon-close.svg";
import { getTAUrl } from "../lib/constants"; import { getTAUrl } from "../lib/constants";
import { Tasks } from "../types/download";
const TA_BASE_URL = getTAUrl(); const TA_BASE_URL = getTAUrl();
@ -124,6 +124,18 @@ const Download: NextPage = () => {
.catch(error => handleSetErrorMessage(error.message)); .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 ( return (
<> <>
<CustomHead title="Downloads" /> <CustomHead title="Downloads" />
@ -202,7 +214,7 @@ const Download: NextPage = () => {
alt="rescan-icon" alt="rescan-icon"
title="Rescan subscriptions" title="Rescan subscriptions"
// className="rotate-img" // Set when rescanning // 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> */} {/* <img id="rescan-icon" onclick="rescanPending()" src="{% static 'img/icon-rescan.svg' %}" alt="rescan-icon"></img> */}
<p>Rescan subscriptions</p> <p>Rescan subscriptions</p>
@ -215,7 +227,7 @@ const Download: NextPage = () => {
alt="download-icon" alt="download-icon"
title="Start download" title="Start download"
// className="bounce-img" // Set when video is downloading // 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> */} {/* <img id="download-icon" onclick="dlPending()" src="{% static 'img/icon-download.svg' %}" alt="download-icon"></img> */}
<p>Start download</p> <p>Start download</p>

View File

@ -5,6 +5,13 @@ export interface Download {
message: string; message: string;
} }
export interface Task {
success: boolean;
task: string;
}
export type Tasks = "download_pending" | "rescan_pending";
export interface Paginate { export interface Paginate {
page_size: number; page_size: number;
page_from: number; page_from: number;
@ -16,11 +23,6 @@ export interface Paginate {
total_hits: number; total_hits: number;
} }
// export interface DownloadResponse {
// data: Datum[];
// message: string;
// }
export interface Config { export interface Config {
archive: Archive; archive: Archive;
default_view: DefaultView; default_view: DefaultView;
@ -106,19 +108,6 @@ export interface Datum {
title: string; title: string;
vid_thumb_url: string; vid_thumb_url: string;
youtube_id: 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 { export enum Category {