-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from Open-Webtoon-Reader/feature/better-downloads
Merge better downloads to Main
- Loading branch information
Showing
6 changed files
with
131 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import CachedWebtoonModel from "../../../modules/webtoon/webtoon/models/models/cached-webtoon.model"; | ||
import * as fs from "node:fs"; | ||
|
||
export default class DownloadQueue{ | ||
|
||
queuedDownloads: CachedWebtoonModel[]; | ||
currentDownload: CachedWebtoonModel | undefined; | ||
|
||
constructor(){ | ||
this.queuedDownloads = []; | ||
this.currentDownload = undefined; | ||
} | ||
reset(){ | ||
if(this.currentDownload) | ||
this.queuedDownloads.unshift(this.currentDownload); | ||
} | ||
enqueue(element: CachedWebtoonModel): void{ | ||
if(this.isInQueue(element)) | ||
return; | ||
this.queuedDownloads.push(element); | ||
this.saveQueue(); | ||
} | ||
dequeue(): CachedWebtoonModel | null{ | ||
if (this.isQueueEmpty()) | ||
return null; | ||
this.currentDownload = this.queuedDownloads.shift(); | ||
this.saveQueue(); | ||
return this.currentDownload; | ||
} | ||
isInQueue(element: CachedWebtoonModel): boolean{ | ||
return this.queuedDownloads.find(w => w.title === element.title && w.language === element.language) !== undefined; | ||
} | ||
isQueueEmpty(): boolean{ | ||
return this.queuedDownloads.length === 0; | ||
} | ||
clear(): void{ | ||
this.queuedDownloads = []; | ||
this.currentDownload = undefined; | ||
this.saveQueue(); | ||
} | ||
clearCurrentDownload(): void{ | ||
this.currentDownload = undefined; | ||
this.saveQueue(); | ||
} | ||
getQueue(): CachedWebtoonModel[]{ | ||
return this.queuedDownloads; | ||
} | ||
getCurrentDownload(): CachedWebtoonModel | undefined{ | ||
return this.currentDownload; | ||
} | ||
saveQueue(): void{ | ||
const jsonQueue = JSON.stringify(this, null, 2); | ||
fs.writeFileSync("./.cache/download_queue.json", jsonQueue); | ||
} | ||
static loadQueue(): DownloadQueue{ | ||
if(!fs.existsSync("./.cache/download_queue.json")) | ||
return new DownloadQueue(); | ||
const queueFile: Buffer = fs.readFileSync("./.cache/download_queue.json"); | ||
const queue = JSON.parse(queueFile.toString()); | ||
const webtoonQueue = new DownloadQueue(); | ||
webtoonQueue.queuedDownloads = queue.queuedDownloads; | ||
webtoonQueue.currentDownload = queue.currentDownload; | ||
return webtoonQueue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import {Module} from "@nestjs/common"; | ||
import {WebtoonUpdateTask} from "./webtoon_update.task"; | ||
import {WebtoonModule} from "../webtoon/webtoon/webtoon.module"; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [], | ||
providers: [], | ||
providers: [WebtoonUpdateTask], | ||
imports: [WebtoonModule] | ||
}) | ||
export class TaskModule{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {Injectable} from "@nestjs/common"; | ||
import {Cron} from "@nestjs/schedule"; | ||
import {DownloadManagerService} from "../webtoon/webtoon/download-manager.service"; | ||
|
||
|
||
@Injectable() | ||
export class WebtoonUpdateTask{ | ||
|
||
constructor( | ||
private readonly downloadManagerService: DownloadManagerService | ||
){} | ||
|
||
@Cron("0 0 17 * * *") | ||
async handleCron(){ | ||
// Called every day at 17:00 | ||
this.downloadManagerService.updateAllWebtoons(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters