2022-10-18 19:28:37 +00:00
|
|
|
import type { Download, Task, Tasks } from "../types/download";
|
2022-04-16 22:26:30 +00:00
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2022-04-20 18:33:58 +00:00
|
|
|
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 + ")",
|
|
|
|
// }
|
2022-04-20 18:33:58 +00:00
|
|
|
// error = response.statusText + " (" + response.status + ");
|
2022-04-21 15:15:57 +00:00
|
|
|
throw new Error(response.statusText + " (" + response.status + ")");
|
|
|
|
// return error;
|
2022-04-16 22:26:30 +00:00
|
|
|
}
|
|
|
|
};
|
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
|
|
|
});
|
2022-04-21 15:56:48 +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."
|
|
|
|
);
|
2022-04-21 15:56:48 +00:00
|
|
|
} 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-04-18 17:44:03 +00:00
|
|
|
};
|
|
|
|
|
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}/`, {
|
2022-04-18 17:44:03 +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:44:03 +00:00
|
|
|
});
|
|
|
|
if (!response.ok) {
|
2022-04-21 16:44:59 +00:00
|
|
|
throw new Error("Error removing video.");
|
2022-04-18 17:44:03 +00:00
|
|
|
// return response.json();
|
|
|
|
}
|
|
|
|
return response.json();
|
2022-04-18 17:51:41 +00:00
|
|
|
};
|
|
|
|
|
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.`);
|
|
|
|
}
|
2022-04-18 17:51:41 +00:00
|
|
|
var data = {
|
2022-10-18 19:28:37 +00:00
|
|
|
status: status,
|
2022-04-18 17:51:41 +00:00
|
|
|
};
|
2022-10-18 19:28:37 +00:00
|
|
|
const response = await fetch(`${API_URL}/api/download/${videoId}/`, {
|
2022-04-18 17:51:41 +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-18 17:51:41 +00:00
|
|
|
});
|
|
|
|
if (!response.ok) {
|
2022-04-21 16:44:59 +00:00
|
|
|
throw new Error("Error moving video to" + status + ".");
|
2022-04-18 17:51:41 +00:00
|
|
|
}
|
|
|
|
return response.json();
|
2022-04-23 16:40:51 +00:00
|
|
|
};
|
|
|
|
|
2022-04-23 18:37:46 +00:00
|
|
|
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.`);
|
|
|
|
}
|
2022-04-23 16:40:51 +00:00
|
|
|
var data = {
|
2022-10-18 19:28:37 +00:00
|
|
|
run: task,
|
2022-04-23 16:40:51 +00:00
|
|
|
};
|
2022-10-18 19:28:37 +00:00
|
|
|
const response = await fetch(`${API_URL}/api/task/`, {
|
2022-04-23 16:40:51 +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-23 16:40:51 +00:00
|
|
|
});
|
|
|
|
if (!response.ok) {
|
2022-04-23 18:37:46 +00:00
|
|
|
throw new Error(`Error running task: ${task}.`);
|
2022-04-23 16:40:51 +00:00
|
|
|
}
|
|
|
|
return response.json();
|
2022-10-18 19:28:37 +00:00
|
|
|
};
|