feat(api): Clear, remove from queue

This commit is contained in:
Tibo De Peuter 2024-12-30 19:59:41 +01:00
parent 4ba189fa1f
commit 1f569b1457
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
3 changed files with 71 additions and 27 deletions

View file

@ -2,29 +2,52 @@ import express from 'express';
import bodyParser from "body-parser"; import bodyParser from "body-parser";
import QueueManager from './queueManager'; import QueueManager from './queueManager';
const app = express();
const port = 3000; const port = 3000;
const server = new QueueManager(); const api = '/api/v1';
app.use(bodyParser.urlencoded({ extended: true })); const app = express();
const qm = new QueueManager();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); app.use(bodyParser.json());
app.get('/api/queue', (req, res) => {
res.json(server.getQueue()); app.get(api + '/queue', (req, res) => {
res.json(qm.getQueue());
}); });
app.post('/api/queue', (req, res) => { app.post(api + '/queue', (req, res) => {
const album: URL = req.body.album; const album: string = req.body.album;
if (!album) { if (!album) {
res.status(400).send('No album URL provided'); res.status(400).send('No album URL provided'); // Bad request
return; return;
} }
if (server.addToQueue(album)) { const result: boolean | string = qm.addToQueue(album);
res.status(201).send('Album added to queue'); if (result === true) {
res.status(201).send('Album added to queue'); // Created
} else { } else {
res.status(409).send('Album already in queue'); res.status(409).send(result); // Conflict
}
});
app.delete(api + '/queue', (req, res) => {
qm.clearQueue();
res.status(205).send(); // Client should reset content.
});
app.delete(api + '/queue/:index', (req, res) => {
const index = parseInt(req.params.index, 10);
if (isNaN(index)) {
res.status(400).send('Invalid index');
return;
}
const result: boolean | string = qm.removeFromQueue(index);
if (result === true) {
res.status(205).send(); // Client should reset content.
} else {
res.status(404).send(result); // Not found
} }
}); });
@ -33,6 +56,6 @@ app.listen(port, () => {
}); });
process.on('SIGINT', async () => { process.on('SIGINT', async () => {
await server.forceStop(); await qm.forceStop();
process.exit(); process.exit();
}); });

View file

@ -1,6 +1,6 @@
import {firefox, Page, BrowserContext, Download, Browser, Locator} from 'playwright'; import {firefox, Page, BrowserContext, Download, Browser, Locator} from 'playwright';
async function lucida(album: URL, baseTimeout: number, context: BrowserContext): Promise<void> { export async function lucida(album: URL, baseTimeout: number, context: BrowserContext): Promise<void> {
const page: Page = await context.newPage(); const page: Page = await context.newPage();
await page.goto('/'); await page.goto('/');

View file

@ -1,4 +1,5 @@
import {firefox, Browser, BrowserContext} from "playwright"; import {firefox, Browser, BrowserContext, Page} from "playwright";
import {lucida} from "./lucida";
class QueueManager { class QueueManager {
private browser: Browser | null; private browser: Browser | null;
@ -13,17 +14,35 @@ class QueueManager {
return this.queue; return this.queue;
} }
addToQueue(url: URL): boolean { addToQueue(link: string): boolean | string {
if (this.queue.includes(url)) { // Check if the URL is a valid URL
return false; try {
const url: URL = new URL(link);
// Check if the URL was already added to the queue
if (this.queue.includes(url)) {
return "Album already in queue";
}
this.queue.push(url);
this.processQueue();
return true;
} catch {
return "Invalid URL";
}
}
removeFromQueue(index: number): boolean | string {
if (index < 0 || index >= this.queue.length) {
return "Index out of bounds";
} }
this.queue.push(url); this.queue.splice(index, 1);
return true;
}
// TODO clearQueue(): void {
const promise = this.processQueue(); this.queue = [];
return false;
} }
forceStop(): Promise<void> { forceStop(): Promise<void> {
@ -40,20 +59,22 @@ class QueueManager {
} }
console.log('Starting browser'); console.log('Starting browser');
this.browser = await firefox.launch({headless: true}); this.browser = await firefox.launch({headless: false});
const context: BrowserContext = await this.browser.newContext({ const context: BrowserContext = await this.browser.newContext({
acceptDownloads: true, acceptDownloads: true,
baseURL: 'https://lucida.to' baseURL: 'https://lucida.to'
}); });
while (this.queue.length > 0) { while (this.queue.length > 0) {
const album: URL = new URL('https://google.com'); const album: URL | undefined = this.queue.shift();
if (album === undefined) {
continue;
}
// TODO // TODO
console.log(`Processing ${album.href}`); console.log(`Processing ${album.href}`);
await new Promise(resolve => setTimeout(resolve, 10000)); await lucida(album, 60000, context);
console.log(`Finished processing ${album.href}`); console.log(`Finished processing ${album.href}`);
this.queue.shift();
} }
console.log('Shutting down browser'); console.log('Shutting down browser');