code cleanup

This commit is contained in:
2025-10-22 11:53:12 +02:00
parent 48bbec8830
commit 8490b2d16c

13
main.ts
View File

@@ -220,33 +220,28 @@ class DownloadQueue {
public enqueue(url: string, folder: string, filename: string) { public enqueue(url: string, folder: string, filename: string) {
this.queue.push({ url, folder, filename }); this.queue.push({ url, folder, filename });
// Only process the queue if we're not at max capacity
this.processQueue(); this.processQueue();
} }
private async processQueue(): Promise<void> { private async processQueue(): Promise<void> {
// Keep processing while we have capacity and items in the queue
while (this.activeDownloads < this.maxConcurrency && this.queue.length > 0) { while (this.activeDownloads < this.maxConcurrency && this.queue.length > 0) {
const { url, folder, filename } = this.queue.shift()!; const { url, folder, filename } = this.queue.shift()!;
this.activeDownloads++; this.activeDownloads++;
// Process the download asynchronously but continue the loop immediately
// to potentially start more downloads if there's capacity
this.processDownload(url, folder, filename).then(() => { this.processDownload(url, folder, filename).then(() => {
// After download is complete, decrease activeDownloads and continue processing
this.activeDownloads--; this.activeDownloads--;
this.processQueue(); // Process next item in queue this.processQueue();
}).catch((error) => { }).catch((error) => {
console.error('Error downloading song:', error); console.error('Error downloading song:', error);
this.activeDownloads--; this.activeDownloads--;
this.processQueue(); // Continue processing queue even if there's an error this.processQueue();
}); });
} }
} }
private async processDownload(url: string, folder: string, filename: string): Promise<void> { private async processDownload(url: string, folder: string, filename: string): Promise<void> {
await downloadSong(url, folder, filename); await downloadSong(url, folder, filename);
songCount++; // Increment the song count after successful download songCount++;
console.log(`${songCount} songs downloaded successfully \n${this.queue.length} remaining`); console.log(`${songCount} songs downloaded successfully \n${this.queue.length} remaining`);
} }
} }
@@ -281,7 +276,7 @@ for (const mentalState of mentalStates) {
// Phase 2 : Download songs to device // Phase 2 : Download songs to device
// //
console.log(chalk.green(`Started downloading ${genre} ${mentalState}`)) console.log(chalk.green(`Started queuing ${genre} ${mentalState}`))
for (const song of data) { for (const song of data) {
let activity = song.track.tags.filter((x: any) => x.type == 'activity').map((x: any) => x.value).join('/'); let activity = song.track.tags.filter((x: any) => x.type == 'activity').map((x: any) => x.value).join('/');
let NEL = song.trackVariation.neuralEffectLevel; let NEL = song.trackVariation.neuralEffectLevel;