ytsm/app/YtManagerApp/scheduler/jobs/delete_video_job.py

48 lines
1.4 KiB
Python
Raw Normal View History

import os
from YtManagerApp.models import Video
from YtManagerApp.scheduler.job import Job
2019-08-14 14:14:16 +00:00
class DeleteVideoJob(Job):
name = "DeleteVideoJob"
2019-08-14 14:14:16 +00:00
def __init__(self, job_execution, video: Video):
super().__init__(job_execution)
self._video = video
2019-08-14 14:14:16 +00:00
def get_description(self):
return f"Deleting video {self._video}"
2019-08-14 14:14:16 +00:00
def run(self):
count = 0
2019-08-14 14:14:16 +00:00
try:
for file in self._video.get_files():
self.log.info("Deleting file %s", file)
count += 1
try:
os.unlink(file)
except OSError as e:
self.log.error("Failed to delete file %s: Error: %s", file, e)
2019-08-14 14:14:16 +00:00
except OSError as e:
self.log.error("Failed to delete video %d [%s %s]. Error: %s", self._video.id,
self._video.video_id, self._video.name, e)
2019-08-14 14:14:16 +00:00
self._video.downloaded_path = None
self._video.save()
2019-08-14 14:14:16 +00:00
self.log.info('Deleted video %d successfully! (%d files) [%s %s]', self._video.id, count,
self._video.video_id, self._video.name)
@staticmethod
def schedule(video: Video):
"""
Schedules a delete video job to run immediately.
:param video:
:return:
"""
from YtManagerApp.services import Services
Services.scheduler.add_job(DeleteVideoJob, args=[video])