tubearchivist-frontend/app/lib/getDownloads.ts

170 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

2022-10-18 19:28:37 +00:00
import type { Download, Task, Tasks } from "../types/download";
2022-10-18 19:28:37 +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-10-18 19:28:37 +00:00
const response = await fetch(
`${API_URL}/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-10-18 19:28:37 +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 = {
2022-10-18 19:28:37 +00:00
data: [
{
youtube_id: input,
status: "pending",
},
],
2022-04-17 01:03:17 +00:00
};
2022-10-18 19:28:37 +00:00
const response = await fetch(`${API_URL}/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) {
2022-10-18 19:28:37 +00:00
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-18 17:33:14 +00:00
2022-10-18 19:28:37 +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-10-18 19:28:37 +00:00
const response = await fetch(`${API_URL}/api/download/?filter=${filter}`, {
2022-04-18 17:33:14 +00:00
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();
};
2022-10-18 19:28:37 +00:00
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.`);
}
2022-10-18 19:28:37 +00:00
const response = await fetch(`${API_URL}/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();
};
2022-10-18 19:28:37 +00:00
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 = {
2022-10-18 19:28:37 +00:00
status: status,
};
2022-10-18 19:28:37 +00:00
const response = await fetch(`${API_URL}/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 = {
2022-10-18 19:28:37 +00:00
run: task,
};
2022-10-18 19:28:37 +00:00
const response = await fetch(`${API_URL}/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-10-18 19:28:37 +00:00
};