feat(api): Clear, remove from queue
This commit is contained in:
parent
4ba189fa1f
commit
1f569b1457
3 changed files with 71 additions and 27 deletions
47
src/index.ts
47
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();
|
||||
});
|
|
@ -1,6 +1,6 @@
|
|||
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();
|
||||
|
||||
await page.goto('/');
|
||||
|
|
|
@ -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<void> {
|
||||
|
@ -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');
|
||||
|
|
Reference in a new issue