Tranga-Website/Website/apiConnector.js

364 lines
10 KiB
JavaScript
Raw Normal View History

2024-03-06 05:04:11 +01:00
let apiUri = `${window.location.protocol}//${window.location.host}/api`
2023-05-25 10:42:19 +02:00
2024-03-06 05:04:11 +01:00
// if(getCookie("apiUri") != ""){
// apiUri = getCookie("apiUri");
// }
// setCookie("apiUri", apiUri);
2023-08-31 16:45:17 +02:00
function setCookie(cname, cvalue) {
const d = new Date();
d.setTime(d.getTime() + (365*24*60*60*1000));
let expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/;samesite=strict";
}
2023-05-25 10:42:19 +02:00
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
2023-05-23 12:51:21 +02:00
2023-05-23 13:52:35 +02:00
async function GetData(uri){
let request = await fetch(uri, {
2023-05-23 13:15:29 +02:00
method: 'GET',
headers: {
'Accept': 'application/json'
}
2023-05-23 13:52:35 +02:00
});
let json = await request.json();
return json;
2023-05-23 13:15:29 +02:00
}
2024-02-11 22:59:05 +01:00
async function PostData(uri){
let request = await fetch(uri, {
2023-05-23 13:52:35 +02:00
method: 'POST'
});
//console.log(request);
2023-05-23 13:52:35 +02:00
}
function DeleteData(uri){
fetch(uri, {
method: 'DELETE'
});
}
2023-09-08 16:31:55 +02:00
async function Ping(){
let ret = await GetData(`${apiUri}/Ping`);
return ret;
}
2023-05-23 13:52:35 +02:00
async function GetAvailableControllers(){
var uri = apiUri + "/Connectors";
2023-05-23 13:52:35 +02:00
let json = await GetData(uri);
return json;
}
2023-08-31 12:56:09 +02:00
async function GetPublicationFromConnector(connector, title){
2023-09-02 22:14:44 +02:00
var uri;
if(title.includes("http")){
uri = `${apiUri}/Manga/FromConnector?connector=${connector}&url=${title}`;
}else{
uri = `${apiUri}/Manga/FromConnector?connector=${connector}&title=${title}`;
}
let json = await GetData(uri);
return json;
2023-05-23 13:52:35 +02:00
}
async function GetChapters(connector, internalId, language){
var uri = `${apiUri}/Manga/Chapters?connector=${connector}&internalId=${internalId}&translatedLanguage=${language}`;
let json = await GetData(uri);
return json;
}
function GetCoverUrl(internalId){
return `${apiUri}/Manga/Cover?internalId=${internalId}`;
}
2023-08-31 12:56:09 +02:00
async function GetAllJobs(){
var uri = `${apiUri}/Jobs`;
let json = await GetData(uri);
return json;
}
2023-08-31 12:56:09 +02:00
async function GetRunningJobs(){
var uri = `${apiUri}/Jobs/Running`;
2023-05-23 13:52:35 +02:00
let json = await GetData(uri);
return json;
}
2023-08-31 12:56:09 +02:00
async function GetWaitingJobs(){
var uri = `${apiUri}/Jobs/Waiting`;
2023-05-23 13:52:35 +02:00
let json = await GetData(uri);
return json;
}
2023-05-23 13:15:29 +02:00
2023-08-31 16:45:17 +02:00
async function GetMonitorJobs(){
var uri = `${apiUri}/Jobs/MonitorJobs`;
let json = await GetData(uri);
return json;
}
2023-08-31 12:56:09 +02:00
async function GetProgress(jobId){
var uri = `${apiUri}/Jobs/Progress?jobId=${jobId}`;
2023-05-23 13:52:35 +02:00
let json = await GetData(uri);
return json;
2023-05-23 13:15:29 +02:00
}
async function GetSettings(){
2023-08-31 12:56:09 +02:00
var uri = `${apiUri}/Settings`;
2023-05-23 13:52:35 +02:00
let json = await GetData(uri);
return json;
}
2023-08-31 12:56:09 +02:00
async function GetAvailableNotificationConnectors(){
var uri = `${apiUri}/NotificationConnectors/Types`;
let json = await GetData(uri);
return json;
2023-05-24 21:48:54 +02:00
}
2023-08-31 12:56:09 +02:00
async function GetNotificationConnectors(){
var uri = `${apiUri}/NotificationConnectors`;
let json = await GetData(uri);
return json;
}
2023-08-31 12:56:09 +02:00
async function GetAvailableLibraryConnectors(){
var uri = `${apiUri}/LibraryConnectors/Types`;
let json = await GetData(uri);
return json;
2023-05-23 13:52:35 +02:00
}
2023-08-31 12:56:09 +02:00
async function GetLibraryConnectors(){
var uri = `${apiUri}/LibraryConnectors`;
let json = await GetData(uri);
return json;
}
2024-02-11 22:59:05 +01:00
async function GetRateLimits() {
var uri = `${apiUri}/Settings/customRequestLimit`
let json = await GetData(uri);
return json;
}
function CreateMonitorJob(connector, internalId, language){
var uri = `${apiUri}/Jobs/MonitorManga?connector=${connector}&internalId=${internalId}&interval=03:00:00&translatedLanguage=${language}`;
2023-05-23 13:52:35 +02:00
PostData(uri);
}
function CreateDownloadNewChaptersJob(connector, internalId, language){
var uri = `${apiUri}/Jobs/DownloadNewChapters?connector=${connector}&internalId=${internalId}&translatedLanguage=${language}`;
2023-05-23 13:52:35 +02:00
PostData(uri);
}
2023-08-31 13:12:20 +02:00
function StartJob(jobId){
var uri = `${apiUri}/Jobs/StartNow?jobId=${jobId}`;
PostData(uri);
}
2023-06-15 18:38:47 +02:00
function UpdateDownloadLocation(downloadLocation){
2023-08-31 12:56:09 +02:00
var uri = `${apiUri}/Settings/UpdateDownloadLocation?downloadLocation=${downloadLocation}`;
PostData(uri);
2023-06-15 18:38:47 +02:00
}
function RefreshLibraryMetadata() {
var uri = `${apiUri}/Jobs/UpdateMetadata`;
PostData(uri);
}
async function DownloadLogs() {
var uri = `${apiUri}/LogFile`;
2024-02-12 00:39:27 +01:00
//Below taken from https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream
fetch(uri)
.then((response) => response.body)
.then((rb) => {
const reader = rb.getReader();
return new ReadableStream({
start(controller) {
// The following function handles each data chunk
function push() {
// "done" is a Boolean and value a "Uint8Array"
reader.read().then(({ done, value }) => {
// If there is no more data to read
if (done) {
console.log("done", done);
controller.close();
return;
}
// Get the data and send it to the browser via the controller
controller.enqueue(value);
// Check chunks by logging to the console
console.log(done, value);
push();
});
}
push();
},
});
})
.then((stream) =>
// Respond with our stream
new Response(stream, { headers: { "Content-Type": "text/html" } }).text(),
)
.then((result) => {
// Do things with result
2024-02-12 00:41:21 +01:00
//console.log(result);
2024-02-12 00:39:27 +01:00
//Below download taken from https://stackoverflow.com/questions/3665115/how-to-create-a-file-in-memory-for-user-to-download-but-not-through-server
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset-utf-8,' + encodeURIComponent(result));
var newDate = new Date();
var filename = "Tranga_Logs_" + newDate.today() + "_" + newDate.timeNow() + ".log";
element.setAttribute('download', filename);
element.click();
});
}
//Following date-time code taken from: https://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript
// For todays date;
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}
// For the time now
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +"_"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +"_"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
2024-04-21 19:36:25 +02:00
function UpdateAprilFoolsMode() {
checkBox = document.getElementById("aprilFoolsMode");
var uri = `${apiUri}/Settings/AprilFoolsMode?enabled=${checkBox.checked}`;
PostData(uri);
}
2024-04-21 19:51:44 +02:00
function ResetRateLimits() {
var uri = `${apiUri}/Settings/customRequestLimit/Reset`;
PostData(uri);
OpenSettings();
}
function ResetUserAgent() {
var uri = `${apiUri}/Settings/userAgent/Reset`;
PostData(uri);
OpenSettings();
}
//Komga
2023-06-15 18:38:47 +02:00
function UpdateKomga(komgaUrl, komgaAuth){
var uri = `${apiUri}/LibraryConnectors/Update?libraryConnector=Komga&komgaUrl=${komgaUrl}&komgaAuth=${komgaAuth}`;
2023-06-15 18:38:47 +02:00
PostData(uri);
}
function ResetKomga(){
var uri = `${apiUri}/LibraryConnectors/Reset?libraryConnector=Komga`;
2024-02-04 22:02:09 +01:00
PostData(uri);
}
function TestKomga(komgaUrl, komgaAuth){
var uri = `${apiUri}/LibraryConnectors/Test?libraryConnector=Komga&komgaUrl=${komgaUrl}&komgaAuth=${komgaAuth}`;
PostData(uri);
}
//Kavita
2023-08-31 16:45:17 +02:00
function UpdateKavita(kavitaUrl, kavitaUsername, kavitaPassword){
2023-10-30 13:36:02 +01:00
var uri = `${apiUri}/LibraryConnectors/Update?libraryConnector=Kavita&kavitaUrl=${kavitaUrl}&kavitaUsername=${kavitaUsername}&kavitaPassword=${kavitaPassword}`;
2023-06-15 18:38:47 +02:00
PostData(uri);
}
function ResetKavita(){
var uri = `${apiUri}/LibraryConnectors/Reset?libraryConnector=Kavita`;
2024-02-04 22:02:09 +01:00
PostData(uri);
}
function TestKavita(kavitaUrl, kavitaUsername, kavitaPassword){
var uri = `${apiUri}/LibraryConnectors/Test?libraryConnector=Kavita&kavitaUrl=${kavitaUrl}&kavitaUsername=${kavitaUsername}&kavitaPassword=${kavitaPassword}`;
PostData(uri);
}
//Gotify
2023-06-15 18:38:47 +02:00
function UpdateGotify(gotifyUrl, gotifyAppToken){
2023-08-31 12:56:09 +02:00
var uri = `${apiUri}/NotificationConnectors/Update?notificationConnector=Gotify&gotifyUrl=${gotifyUrl}&gotifyAppToken=${gotifyAppToken}`;
2023-05-23 13:52:35 +02:00
PostData(uri);
}
function ResetGotify(){
2024-02-11 22:59:05 +01:00
var uri = `${apiUri}/NotificationConnectors/Reset?notificationConnector=Gotify`;
2024-02-04 22:02:09 +01:00
PostData(uri);
}
function TestGotify(gotifyUrl, gotifyAppToken){
var uri = `${apiUri}/NotificationConnectors/Test?notificationConnector=Gotify&gotifyUrl=${gotifyUrl}&gotifyAppToken=${gotifyAppToken}`;
PostData(uri);
}
//LunaSea
function UpdateLunaSea(lunaseaWebhook){
2023-08-31 12:56:09 +02:00
var uri = `${apiUri}/NotificationConnectors/Update?notificationConnector=LunaSea&lunaseaWebhook=${lunaseaWebhook}`;
PostData(uri);
}
function ResetLunaSea(){
2024-02-11 22:59:05 +01:00
var uri = `${apiUri}/NotificationConnectors/Reset?notificationConnector=LunaSea`;
2024-02-04 22:02:09 +01:00
PostData(uri);
}
function TestLunaSea(lunaseaWebhook){
var uri = `${apiUri}/NotificationConnectors/Test?notificationConnector=LunaSea&lunaseaWebhook=${lunaseaWebhook}`;
PostData(uri);
}
//Ntfy
function UpdateNtfy(ntfyEndpoint, ntfyAuth){
var uri = `${apiUri}/NotificationConnectors/Update?notificationConnector=Ntfy&ntfyUrl=${ntfyEndpoint}&ntfyAuth=${ntfyAuth}`;
PostData(uri);
}
function ResetNtfy(){
2024-02-11 22:59:05 +01:00
var uri = `${apiUri}/NotificationConnectors/Reset?notificationConnector=Ntfy`;
2024-02-04 22:02:09 +01:00
PostData(uri);
}
function TestNtfy(ntfyEndpoint, ntfyAuth){
var uri = `${apiUri}/NotificationConnectors/Test?notificationConnector=Ntfy&ntfyUrl=${ntfyEndpoint}&ntfyAuth=${ntfyAuth}`;
PostData(uri);
}
function UpdateUserAgent(userAgent){
var uri = `${apiUri}/Settings/userAgent?userAgent=${userAgent}`;
PostData(uri);
}
2024-02-11 22:59:05 +01:00
function UpdateRateLimit(byteValue, rateLimit) {
var uri = `${apiUri}/Settings/customRequestLimit?requestType=${byteValue}&requestsPerMinute=${rateLimit}`;
PostData(uri);
}
2023-08-31 12:56:09 +02:00
function RemoveJob(jobId){
var uri = `${apiUri}/Jobs?jobId=${jobId}`;
2023-05-23 13:52:35 +02:00
DeleteData(uri);
2023-09-02 15:05:45 +02:00
}
function CancelJob(jobId){
var uri = `${apiUri}/Jobs/Cancel?jobId=${jobId}`;
PostData(uri);
2023-09-26 20:05:50 +02:00
}
async function GetLogmessages(count){
var uri = `${apiUri}/LogMessages?count=${count}`;
let json = await GetData(uri);
console.log(json);
2023-09-26 20:05:50 +02:00
return json;
2023-05-23 12:51:21 +02:00
}