feat(api): Basic api

This commit is contained in:
Tibo De Peuter 2024-12-29 21:23:05 +01:00
parent d94735a9e8
commit 4ba189fa1f
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
4 changed files with 1343 additions and 2 deletions

1235
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -5,8 +5,8 @@
"main": "src/download.ts",
"scripts": {
"build": "tsc",
"start": "node dist/lucida.js",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"server": "npx ts-node src/index.ts"
},
"repository": {
"type": "git",
@ -15,9 +15,12 @@
"author": "Tibo De Peuter <tibo@depeuter.dev>",
"license": "UNLICENSED",
"dependencies": {
"body-parser": "^1.20.3",
"express": "^4.21.2",
"playwright": "1.47.0"
},
"devDependencies": {
"@types/express": "^5.0.0",
"@types/node": "^22.10.2",
"typescript": "^5.7.2"
}

38
src/index.ts Normal file
View file

@ -0,0 +1,38 @@
import express from 'express';
import bodyParser from "body-parser";
import QueueManager from './queueManager';
const app = express();
const port = 3000;
const server = new QueueManager();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/api/queue', (req, res) => {
res.json(server.getQueue());
});
app.post('/api/queue', (req, res) => {
const album: URL = req.body.album;
if (!album) {
res.status(400).send('No album URL provided');
return;
}
if (server.addToQueue(album)) {
res.status(201).send('Album added to queue');
} else {
res.status(409).send('Album already in queue');
}
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
process.on('SIGINT', async () => {
await server.forceStop();
process.exit();
});

65
src/queueManager.ts Normal file
View file

@ -0,0 +1,65 @@
import {firefox, Browser, BrowserContext} from "playwright";
class QueueManager {
private browser: Browser | null;
private queue: URL[];
constructor() {
this.browser = null;
this.queue = [];
}
getQueue(): URL[] {
return this.queue;
}
addToQueue(url: URL): boolean {
if (this.queue.includes(url)) {
return false;
}
this.queue.push(url);
// TODO
const promise = this.processQueue();
return false;
}
forceStop(): Promise<void> {
if (this.browser === null) {
return Promise.resolve();
}
return this.browser.close();
}
private async processQueue(): Promise<void> {
if (this.browser !== null) {
return;
}
console.log('Starting browser');
this.browser = await firefox.launch({headless: true});
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');
// TODO
console.log(`Processing ${album.href}`);
await new Promise(resolve => setTimeout(resolve, 10000));
console.log(`Finished processing ${album.href}`);
this.queue.shift();
}
console.log('Shutting down browser');
await this.browser.close();
this.browser = null;
}
}
export default QueueManager;