for f in self.server.player.playlist]
}
if 'download' in subscriptions:
+ yt_id = YoutubeId(download_id)
with DbConn() as conn:
- last_update = self.server.downloads.last_update_for(
- conn, YoutubeId(download_id))
- if last_updates['download'] < last_update['time']:
- last_updates['download'] = last_update['time']
- payload['download'] = last_update
+ update = self.server.downloads.last_update_for(conn, yt_id)
+ if last_updates['download'] < update['time']:
+ last_updates['download'] = update['time']
+ if update['status'] == 'present':
+ file = VideoFile.get_by_yt_id(conn, yt_id)
+ update['path'] = str(file.rel_path)
+ update['digest'] = file.digest.b64
+ payload['download'] = update
if not payload:
sleep(_EVENTS_UPDATE_INTERVAL_S)
self._send_rendered_template(
_NAME_TEMPLATE_YT_RESULT,
{'video_data': video_data,
- 'is_temp': video_id in self.server.downloads.ids_unfinished,
'file_digest': file_digest,
'file_path': file_path,
'queries': linked_queries,
def __init__(self) -> None:
self._to_download: list[YoutubeId] = []
ensure_expected_dirs([PATH_DOWNLOADS, PATH_TEMP])
- self._last_updates = {}
+ self._last_updates: dict[YoutubeId, dict[str, str]] = {}
self._sync_db()
def _sync_db(self):
yt_id = self._id_from_filename(path)
if (yt_id not in self._last_updates
or 'present' != self._last_updates[yt_id]['status']):
- digest64 = VideoFile.get_by_yt_id(conn, yt_id).digest.b64
- self._update_status(yt_id, 'present', str(path), digest64)
+ self._update_status(yt_id, 'present')
self._files = VideoFile.get_all(conn)
chdir(old_cwd)
self._update_status(yt_id, 'absent')
return self._last_updates[yt_id]
- def _update_status(self,
- yt_id: YoutubeId,
- status: str,
- path: str = '',
- digest: str = ''
- ) -> None:
+ def _update_status(self, yt_id: YoutubeId, status: str) -> None:
self._last_updates[yt_id] = {'status': status, 'time': _now_string()}
- for k, v in [('path', path), ('digest', digest)]:
- if k:
- self._last_updates[yt_id] |= {k: v}
@staticmethod
def _id_from_filename(path: Path) -> YoutubeId:
return YoutubeId(path.stem.split('[')[-1].split(']')[0])
- @property
- def ids_unfinished(self) -> set[YoutubeId]:
- """Return set of IDs of videos awaiting or currently in download."""
- in_temp_dir = []
- for path in [p for p in PATH_TEMP.iterdir() if p.is_file()]:
- in_temp_dir += [self._id_from_filename(path)]
- return set(self._to_download + in_temp_dir)
-
def clean_unfinished(self) -> None:
"""Empty temp directory of unfinished downloads."""
for path in [p for p in PATH_TEMP.iterdir() if p.is_file()]:
def queue_download(self, video_id: YoutubeId) -> None:
"""Add video_id to download queue *if* not already processed."""
self._sync_db()
- pre_existing = (self.ids_unfinished
- | set(self._to_download
- + [f.full_path for f in self._files]))
- if video_id not in pre_existing:
- self._to_download += [video_id]
- self._update_status(video_id, 'queued')
+ if video_id in self._to_download:
+ return
+ for path in ([p for p in PATH_TEMP.iterdir() if p.is_file()]
+ + [f.full_path for f in self._files]):
+ if self._id_from_filename(path) == video_id:
+ return
+ self._to_download += [video_id]
+ self._update_status(video_id, 'queued')
def _download_next(self) -> None:
if self._to_download: