tubearchivist-frontend/src/lib/getDownloads.ts

149 lines
4.3 KiB
TypeScript
Raw Permalink Normal View History

import { Download, Task, Tasks } from "../types/download";
2022-04-17 19:47:18 +00:00
import { getTAUrl } from "./constants";
2022-04-17 19:47:18 +00:00
const TA_BASE_URL = getTAUrl();
2022-04-21 17:39:48 +00:00
export const getDownloads = async (token: string, filter: boolean, pageNumber: number): Promise<Download> => {
2022-04-30 18:24:45 +00:00
if (!token) {
throw new Error(`Unable to fetch downloads, no token provided.`);
}
2022-04-21 17:39:48 +00:00
const response = await fetch(`${TA_BASE_URL.server}/api/download/?filter=${filter ? 'ignore' : 'pending'}&page=${pageNumber}`, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Token ${token}`,
mode: "no-cors",
},
});
if (response.ok) {
return response.json();
} else {
2022-04-21 15:15:57 +00:00
// var error: Download = {
// data: null,
// config: null,
// paginate: null,
// message: response.statusText + " (" + response.status + ")",
// }
// error = response.statusText + " (" + response.status + ");
2022-04-21 15:15:57 +00:00
throw new Error(response.statusText + " (" + response.status + ")");
// return error;
}
};
2022-04-17 01:03:17 +00:00
2022-04-18 17:33:14 +00:00
export const sendDownloads = async (token: string, input: string): Promise<Download> => {
2022-04-30 18:24:45 +00:00
if (!token) {
throw new Error(`Unable to send downloads, no token provided.`);
}
2022-04-17 01:03:17 +00:00
var data = {
"data": [{
"youtube_id": input,
"status": "pending"
}]
};
2022-04-17 19:47:18 +00:00
const response = await fetch(`${TA_BASE_URL.server}/api/download/`, {
2022-04-17 01:03:17 +00:00
body: JSON.stringify(data),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Token ${token}`,
mode: "no-cors",
},
2022-04-30 18:24:45 +00:00
method: "POST",
2022-04-17 01:03:17 +00:00
});
if (response.ok) {
return response.json();
} else if (response.status == 400) {
throw new Error("Failed to extract links. Please input IDs or URLs for videos, channels, or playlists.");
} else {
throw new Error("Failed to connect to the API.");
2022-04-17 01:03:17 +00:00
}
2022-04-17 01:03:17 +00:00
};
2022-04-18 17:33:14 +00:00
export const sendDeleteAllQueuedIgnored = async (token: string, filter: string): Promise<Download> => {
2022-04-30 18:24:45 +00:00
if (!token) {
throw new Error(`Unable to delete downloads, no token provided.`);
}
2022-04-18 17:33:14 +00:00
const response = await fetch(`${TA_BASE_URL.server}/api/download/?filter=${filter}`, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Token ${token}`,
mode: "no-cors",
},
2022-04-30 18:24:45 +00:00
method: "DELETE",
2022-04-18 17:33:14 +00:00
});
if (!response.ok) {
2022-04-21 16:44:59 +00:00
throw new Error("Error removing all videos.");
2022-04-18 17:33:14 +00:00
// return response.json();
}
return response.json();
};
export const sendDeleteVideoQueuedIgnored = async (token: string, videoId: string): Promise<Download> => {
2022-04-30 18:24:45 +00:00
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",
"Content-Type": "application/json",
Authorization: `Token ${token}`,
mode: "no-cors",
},
2022-04-30 18:24:45 +00:00
method: "DELETE",
});
if (!response.ok) {
2022-04-21 16:44:59 +00:00
throw new Error("Error removing video.");
// return response.json();
}
return response.json();
};
export const sendMoveVideoQueuedIgnored = async (token: string, videoId: string, status: string): Promise<Download> => {
2022-04-30 18:24:45 +00:00
if (!token) {
throw new Error(`Unable to move downloads, no token provided.`);
}
var data = {
"status": status
};
const response = await fetch(`${TA_BASE_URL.server}/api/download/${videoId}/`, {
body: JSON.stringify(data),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Token ${token}`,
mode: "no-cors",
},
2022-04-30 18:24:45 +00:00
method: "POST",
});
if (!response.ok) {
2022-04-21 16:44:59 +00:00
throw new Error("Error moving video to" + status + ".");
}
return response.json();
};
export const sendTasks = async (token: string, task: Tasks): Promise<Task> => {
2022-04-30 18:24:45 +00:00
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",
},
2022-04-30 18:24:45 +00:00
method: "POST",
});
if (!response.ok) {
throw new Error(`Error running task: ${task}.`);
}
return response.json();
2022-04-18 17:33:14 +00:00
};