2022-04-23 18:37:46 +00:00
|
|
|
import { Download, Task, Tasks } from "../types/download";
|
2022-04-17 19:47:18 +00:00
|
|
|
import { getTAUrl } from "./constants";
|
2022-04-16 22:26:30 +00:00
|
|
|
|
2022-04-23 18:37:46 +00:00
|
|
|
|
|
|
|
|
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}`, {
|
2022-04-16 22:26:30 +00:00
|
|
|
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-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
|
|
|
});
|
2022-04-21 15:56:48 +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-21 15:56:48 +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();
|
2022-04-18 17:44:03 +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-04-18 17:44:03 +00:00
|
|
|
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",
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
"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",
|
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 = {
|
|
|
|
"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",
|
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-04-18 17:33:14 +00:00
|
|
|
};
|