|
|
|
|
@@ -1,21 +1,21 @@
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
import deemix from 'deemix'
|
|
|
|
|
import fs from 'fs'
|
|
|
|
|
import { sep } from 'path'
|
|
|
|
|
import { wss } from './app'
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
import deemix from 'deemix'
|
|
|
|
|
import WebSocket from 'ws'
|
|
|
|
|
import { wss } from './app'
|
|
|
|
|
|
|
|
|
|
const Downloader = deemix.downloader.Downloader
|
|
|
|
|
const { Single, Collection, Convertable } = deemix.types.downloadObjects
|
|
|
|
|
export const defaultSettings: any = deemix.settings.DEFAULTS
|
|
|
|
|
export const configFolder: string = deemix.utils.localpaths.getConfigFolder()
|
|
|
|
|
export let settings: any = deemix.settings.load(configFolder)
|
|
|
|
|
export let sessionDZ: any = {}
|
|
|
|
|
export const sessionDZ: any = {}
|
|
|
|
|
|
|
|
|
|
let deemixPlugins = {}
|
|
|
|
|
const deemixPlugins = {}
|
|
|
|
|
|
|
|
|
|
export const listener = {
|
|
|
|
|
send: function(key:string, data:any){
|
|
|
|
|
send(key: string, data: any) {
|
|
|
|
|
console.log(key, data)
|
|
|
|
|
wss.clients.forEach(client => {
|
|
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
|
|
|
@@ -30,25 +30,24 @@ export function saveSettings(newSettings: any) {
|
|
|
|
|
settings = newSettings
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export let queueOrder: string[] = []
|
|
|
|
|
export let queue: any = {}
|
|
|
|
|
export let currentJob: any = null
|
|
|
|
|
export const queueOrder: string[] = []
|
|
|
|
|
export const queue: any = {}
|
|
|
|
|
let currentJob: any = null
|
|
|
|
|
|
|
|
|
|
export async function addToQueue(dz: any, url: string, bitrate: number) {
|
|
|
|
|
if (!dz.logged_in) throw new NotLoggedIn
|
|
|
|
|
if (!dz.logged_in) throw new NotLoggedIn()
|
|
|
|
|
console.log(`Adding ${url} to queue`)
|
|
|
|
|
let downloadObjs = await deemix.generateDownloadObject(dz, url, bitrate, deemixPlugins, listener)
|
|
|
|
|
let isSingleObject = !Array.isArray(downloadObjs)
|
|
|
|
|
const isSingleObject = !Array.isArray(downloadObjs)
|
|
|
|
|
console.log(downloadObjs)
|
|
|
|
|
|
|
|
|
|
if (isSingleObject) downloadObjs = [downloadObjs]
|
|
|
|
|
|
|
|
|
|
let slimmedObjects: any[] = []
|
|
|
|
|
const slimmedObjects: any[] = []
|
|
|
|
|
|
|
|
|
|
downloadObjs.forEach(async (downloadObj: any) => {
|
|
|
|
|
downloadObjs.forEach((downloadObj: any) => {
|
|
|
|
|
// Check if element is already in queue
|
|
|
|
|
if (queueOrder.includes(downloadObj.uuid))
|
|
|
|
|
throw new AlreadyInQueue(downloadObj.getEssentialDict(), !isSingleObject)
|
|
|
|
|
if (queueOrder.includes(downloadObj.uuid)) throw new AlreadyInQueue(downloadObj.getEssentialDict(), !isSingleObject)
|
|
|
|
|
|
|
|
|
|
// Save queue status when adding something to the queue
|
|
|
|
|
if (!fs.existsSync(configFolder + 'queue')) fs.mkdirSync(configFolder + 'queue')
|
|
|
|
|
@@ -59,10 +58,8 @@ export async function addToQueue(dz: any, url: string, bitrate: number){
|
|
|
|
|
fs.writeFileSync(configFolder + `queue${sep}${downloadObj.uuid}.json`, JSON.stringify(downloadObj.toDict()))
|
|
|
|
|
slimmedObjects.push(downloadObj.getSlimmedDict())
|
|
|
|
|
})
|
|
|
|
|
if (isSingleObject)
|
|
|
|
|
listener.send('addedToQueue', downloadObjs[0].getSlimmedDict())
|
|
|
|
|
else
|
|
|
|
|
listener.send('addedToQueue', slimmedObjects)
|
|
|
|
|
if (isSingleObject) listener.send('addedToQueue', downloadObjs[0].getSlimmedDict())
|
|
|
|
|
else listener.send('addedToQueue', slimmedObjects)
|
|
|
|
|
|
|
|
|
|
startQueue(dz)
|
|
|
|
|
return slimmedObjects
|
|
|
|
|
@@ -76,20 +73,20 @@ async function startQueue(dz: any): Promise<any>{
|
|
|
|
|
}
|
|
|
|
|
currentJob = true // lock currentJob
|
|
|
|
|
|
|
|
|
|
let currentUUID: string | undefined = queueOrder.shift()
|
|
|
|
|
let currentItem: any = JSON.parse(fs.readFileSync(configFolder+`queue${sep}${currentUUID}.json`).toString())
|
|
|
|
|
const currentUUID: string | undefined = queueOrder.shift()
|
|
|
|
|
const currentItem: any = JSON.parse(fs.readFileSync(configFolder + `queue${sep}${currentUUID}.json`).toString())
|
|
|
|
|
let downloadObject: any
|
|
|
|
|
switch (currentItem.__type__) {
|
|
|
|
|
case 'Single':
|
|
|
|
|
downloadObject = new Single(currentItem)
|
|
|
|
|
break;
|
|
|
|
|
break
|
|
|
|
|
case 'Collection':
|
|
|
|
|
downloadObject = new Collection(currentItem)
|
|
|
|
|
break;
|
|
|
|
|
break
|
|
|
|
|
case 'Convertable':
|
|
|
|
|
downloadObject = new Convertable(currentItem)
|
|
|
|
|
// Convert object here
|
|
|
|
|
break;
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
currentJob = new Downloader(dz, downloadObject, settings, listener)
|
|
|
|
|
listener.send('startDownload', currentUUID)
|
|
|
|
|
@@ -101,7 +98,7 @@ async function startQueue(dz: any): Promise<any>{
|
|
|
|
|
class QueueError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = "QueueError"
|
|
|
|
|
this.name = 'QueueError'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -110,7 +107,7 @@ class AlreadyInQueue extends QueueError {
|
|
|
|
|
silent: boolean
|
|
|
|
|
constructor(dwObj: any, silent: boolean) {
|
|
|
|
|
super(`${dwObj.artist} - ${dwObj.title} is already in queue.`)
|
|
|
|
|
this.name = "AlreadyInQueue"
|
|
|
|
|
this.name = 'AlreadyInQueue'
|
|
|
|
|
this.item = dwObj
|
|
|
|
|
this.silent = silent
|
|
|
|
|
}
|
|
|
|
|
@@ -119,6 +116,6 @@ class AlreadyInQueue extends QueueError {
|
|
|
|
|
class NotLoggedIn extends QueueError {
|
|
|
|
|
constructor() {
|
|
|
|
|
super(`You must be logged in to start a download.`)
|
|
|
|
|
this.name = "NotLoggedIn"
|
|
|
|
|
this.name = 'NotLoggedIn'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|