From 1f569b1457d599098b0b6dd420f23571e49ca753 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Mon, 30 Dec 2024 19:59:41 +0100 Subject: [PATCH] feat(api): Clear, remove from queue --- src/index.ts | 47 ++++++++++++++++++++++++++++++++----------- src/lucida.ts | 2 +- src/queueManager.ts | 49 ++++++++++++++++++++++++++++++++------------- 3 files changed, 71 insertions(+), 27 deletions(-) diff --git a/src/index.ts b/src/index.ts index e15f34b..8faf70a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,29 +2,52 @@ import express from 'express'; import bodyParser from "body-parser"; import QueueManager from './queueManager'; -const app = express(); 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.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) => { - const album: URL = req.body.album; +app.post(api + '/queue', (req, res) => { + const album: string = req.body.album; if (!album) { - res.status(400).send('No album URL provided'); + res.status(400).send('No album URL provided'); // Bad request return; } - if (server.addToQueue(album)) { - res.status(201).send('Album added to queue'); + const result: boolean | string = qm.addToQueue(album); + if (result === true) { + res.status(201).send('Album added to queue'); // Created } 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 () => { - await server.forceStop(); + await qm.forceStop(); process.exit(); }); \ No newline at end of file diff --git a/src/lucida.ts b/src/lucida.ts index d79ce9b..8960e9f 100644 --- a/src/lucida.ts +++ b/src/lucida.ts @@ -1,6 +1,6 @@ import {firefox, Page, BrowserContext, Download, Browser, Locator} from 'playwright'; -async function lucida(album: URL, baseTimeout: number, context: BrowserContext): Promise { +export async function lucida(album: URL, baseTimeout: number, context: BrowserContext): Promise { const page: Page = await context.newPage(); await page.goto('/'); diff --git a/src/queueManager.ts b/src/queueManager.ts index 35a804e..a180b31 100644 --- a/src/queueManager.ts +++ b/src/queueManager.ts @@ -1,4 +1,5 @@ -import {firefox, Browser, BrowserContext} from "playwright"; +import {firefox, Browser, BrowserContext, Page} from "playwright"; +import {lucida} from "./lucida"; class QueueManager { private browser: Browser | null; @@ -13,17 +14,35 @@ class QueueManager { return this.queue; } - addToQueue(url: URL): boolean { - if (this.queue.includes(url)) { - return false; + addToQueue(link: string): boolean | string { + // Check if the URL is a valid URL + 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 - const promise = this.processQueue(); - - return false; + clearQueue(): void { + this.queue = []; } forceStop(): Promise { @@ -40,20 +59,22 @@ class QueueManager { } 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({ acceptDownloads: true, baseURL: 'https://lucida.to' }); 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 console.log(`Processing ${album.href}`); - await new Promise(resolve => setTimeout(resolve, 10000)); + await lucida(album, 60000, context); console.log(`Finished processing ${album.href}`); - - this.queue.shift(); } console.log('Shutting down browser');