Added sorting by Timestamp

This commit is contained in:
Erazor2 2022-01-21 21:23:59 +00:00
parent 1ebf1da076
commit 9994777974

View file

@ -2,6 +2,7 @@ import os
import yt_dlp import yt_dlp
from collections import OrderedDict from collections import OrderedDict
import shelve import shelve
import time
import asyncio import asyncio
import multiprocessing import multiprocessing
import logging import logging
@ -32,6 +33,7 @@ class DownloadInfo:
self.format = format self.format = format
self.status = self.msg = self.percent = self.speed = self.eta = None self.status = self.msg = self.percent = self.speed = self.eta = None
self.filename = None self.filename = None
self.timestamp = time.time_ns()
class Download: class Download:
manager = None manager = None
@ -145,9 +147,8 @@ class PersistentQueue:
shelf.close() shelf.close()
def __loadShelve(self): def __loadShelve(self):
with shelve.open(self.shelvePath, 'r') as shelf: for k, v in self.savedItems():
for key in shelf.keys(): self.dict[k] = Download(None, None, None, None, {}, v)
self.dict[key] = shelf[key]
def exists(self, key): def exists(self, key):
return key in self.dict return key in self.dict
@ -160,9 +161,10 @@ class PersistentQueue:
def savedItems(self): def savedItems(self):
with shelve.open(self.shelvePath, 'r') as shelf: with shelve.open(self.shelvePath, 'r') as shelf:
return dict(shelf).items() return sorted(shelf.items(), key=lambda item: item[1].timestamp)
def put(self, key, value): def put(self, value):
key = value.info.id
self.dict[key] = value self.dict[key] = value
with shelve.open(self.shelvePath, 'w') as shelf: with shelve.open(self.shelvePath, 'w') as shelf:
shelf[key] = value.info shelf[key] = value.info
@ -227,7 +229,7 @@ class DownloadQueue:
if not self.queue.exists(entry['id']): if not self.queue.exists(entry['id']):
dl = DownloadInfo(entry['id'], entry['title'], entry.get('webpage_url') or entry['url'], quality, format) dl = DownloadInfo(entry['id'], entry['title'], entry.get('webpage_url') or entry['url'], quality, format)
dldirectory = self.config.DOWNLOAD_DIR if (quality != 'audio' and format != 'mp3') else self.config.AUDIO_DOWNLOAD_DIR dldirectory = self.config.DOWNLOAD_DIR if (quality != 'audio' and format != 'mp3') else self.config.AUDIO_DOWNLOAD_DIR
self.queue.put(entry['id'], Download(dldirectory, self.config.OUTPUT_TEMPLATE, quality, format, self.config.YTDL_OPTIONS, dl)) self.queue.put(Download(dldirectory, self.config.OUTPUT_TEMPLATE, quality, format, self.config.YTDL_OPTIONS, dl))
self.event.set() self.event.set()
await self.notifier.added(dl) await self.notifier.added(dl)
return {'status': 'ok'} return {'status': 'ok'}
@ -298,5 +300,5 @@ class DownloadQueue:
if entry.canceled: if entry.canceled:
await self.notifier.canceled(id) await self.notifier.canceled(id)
else: else:
self.done.put(id, entry) self.done.put(entry)
await self.notifier.completed(entry.info) await self.notifier.completed(entry.info)