tubearchivist-frontend/src/lib/getDownloads.ts

109 lines
3.2 KiB
TypeScript
Raw Normal View History

import { Download } 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> => {
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-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",
},
method: "POST"
});
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> => {
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",
},
method: "DELETE"
});
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> => {
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",
},
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> => {
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",
},
method: "POST"
});
if (!response.ok) {
2022-04-21 16:44:59 +00:00
throw new Error("Error moving video to" + status + ".");
}
return response.json();
2022-04-18 17:33:14 +00:00
};