This commit is contained in:
ArabCoders 2023-11-28 17:29:45 +03:00
parent f0d4f14b1f
commit 825a42b497
2 changed files with 72 additions and 42 deletions

View file

@ -38,11 +38,33 @@ class DataStore:
default_ytdl_opts=self.config.ytdl_options, default_ytdl_opts=self.config.ytdl_options,
) )
def exists(self, key: str) -> bool: def exists(self, key: str = None, url: str = None) -> bool:
return key in self.dict if not key and not url:
raise KeyError('key or url must be provided')
def get(self, key: str) -> Download: if key and key in self.dict:
return self.dict[key] return True
if url:
for key in self.dict:
if self.dict[key].info.url == url:
return True
return False
def get(self, key: str, url: str = None) -> Download:
if not key and not url:
raise KeyError('key or url must be provided')
if key and key in self.dict:
return self.dict[key]
if url:
for key in self.dict:
if self.dict[key].info.url == url:
return self.dict[key]
raise KeyError('key or url not found')
def items(self) -> list[tuple[str, Download]]: def items(self) -> list[tuple[str, Download]]:
return self.dict.items() return self.dict.items()

View file

@ -93,49 +93,57 @@ class DownloadQueue:
return {'status': 'error', 'msg': ', '.join(res['msg'] for res in results if res['status'] == 'error' and 'msg' in res)} return {'status': 'error', 'msg': ', '.join(res['msg'] for res in results if res['status'] == 'error' and 'msg' in res)}
return {'status': 'ok'} return {'status': 'ok'}
elif etype == 'video' or etype.startswith('url') and 'id' in entry and 'title' in entry: elif (etype == 'video' or etype.startswith('url')) and 'id' in entry and 'title' in entry:
if not self.queue.exists(entry['id']):
dl = ItemDTO( if self.done.exists(key=entry['id'], url=entry.get('webpage_url') or entry['url']):
id=entry['id'], item = self.done.get(key=entry['id'], url=entry.get(
title=entry['title'], 'webpage_url') or entry['url'])
url=entry.get('webpage_url') or entry['url'], self.done.clear([item.info._id])
quality=quality,
format=format, if self.queue.exists(key=entry['id'], url=entry.get('webpage_url') or entry['url']):
folder=folder, return {'status': 'error', 'msg': 'Link already queued for downloading.'}
ytdlp_cookies=ytdlp_cookies,
ytdlp_config=ytdlp_config, dl = ItemDTO(
output_template=output_template if output_template else self.config.output_template, id=entry['id'],
datetime=formatdate(time.time()), title=entry['title'],
error=error, url=entry.get('webpage_url') or entry['url'],
is_live=entry['is_live'] if 'is_live' in entry else None, quality=quality,
format=format,
folder=folder,
ytdlp_cookies=ytdlp_cookies,
ytdlp_config=ytdlp_config,
output_template=output_template if output_template else self.config.output_template,
datetime=formatdate(time.time()),
error=error,
is_live=entry['is_live'] if 'is_live' in entry else None,
)
try:
dldirectory = calcDownloadPath(
basePath=self.config.download_path,
folder=folder
) )
except Exception as e:
return {'status': 'error', 'msg': str(e)}
try: output_chapter = self.config.output_template_chapter
dldirectory = calcDownloadPath(
basePath=self.config.download_path,
folder=folder
)
except Exception as e:
return {'status': 'error', 'msg': str(e)}
output_chapter = self.config.output_template_chapter for property, value in entry.items():
if property.startswith("playlist"):
dl.output_template = dl.output_template.replace(
f"%({property})s", str(value))
for property, value in entry.items(): itemDownload = self.queue.put(Download(
if property.startswith("playlist"): info=dl,
dl.output_template = dl.output_template.replace( download_dir=dldirectory,
f"%({property})s", str(value)) temp_dir=self.config.temp_path,
output_template_chapter=output_chapter,
default_ytdl_opts=self.config.ytdl_options,
debug=bool(self.config.ytdl_debug)
))
itemDownload = self.queue.put(Download( self.event.set()
info=dl, await self.notifier.added(itemDownload.info)
download_dir=dldirectory,
temp_dir=self.config.temp_path,
output_template_chapter=output_chapter,
default_ytdl_opts=self.config.ytdl_options,
debug=bool(self.config.ytdl_debug)
))
self.event.set()
await self.notifier.added(itemDownload.info)
return { return {
'status': 'ok' 'status': 'ok'