2018-10-20 22:20:31 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from YtManagerApp.models import Video
|
2019-12-16 20:19:50 +00:00
|
|
|
from YtManagerApp.scheduler.job import Job
|
2018-10-20 22:20:31 +00:00
|
|
|
|
|
|
|
|
2019-08-14 14:14:16 +00:00
|
|
|
class DeleteVideoJob(Job):
|
|
|
|
name = "DeleteVideoJob"
|
2018-10-20 22:20:31 +00:00
|
|
|
|
2019-08-14 14:14:16 +00:00
|
|
|
def __init__(self, job_execution, video: Video):
|
|
|
|
super().__init__(job_execution)
|
|
|
|
self._video = video
|
2018-10-20 22:20:31 +00:00
|
|
|
|
2019-08-14 14:14:16 +00:00
|
|
|
def get_description(self):
|
|
|
|
return f"Deleting video {self._video}"
|
2018-10-20 22:20:31 +00:00
|
|
|
|
2019-08-14 14:14:16 +00:00
|
|
|
def run(self):
|
|
|
|
count = 0
|
2018-10-20 22:20:31 +00:00
|
|
|
|
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)
|
2018-10-20 22:20:31 +00:00
|
|
|
|
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)
|
2018-10-20 22:20:31 +00:00
|
|
|
|
2019-08-14 14:14:16 +00:00
|
|
|
self._video.downloaded_path = None
|
|
|
|
self._video.save()
|
2018-10-20 22:20:31 +00:00
|
|
|
|
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:
|
|
|
|
"""
|
2019-12-16 20:19:50 +00:00
|
|
|
from YtManagerApp.services import Services
|
|
|
|
Services.scheduler.add_job(DeleteVideoJob, args=[video])
|