2018-10-20 22:20:31 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from YtManagerApp.models import Video
|
2019-12-18 22:27:06 +00:00
|
|
|
from YtManagerApp.services.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,
|
2019-12-18 22:27:06 +00:00
|
|
|
self._video.provider_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,
|
2019-12-18 22:27:06 +00:00
|
|
|
self._video.provider_id, self._video.name)
|
2019-08-14 14:14:16 +00:00
|
|
|
|
|
|
|
@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
|
2019-12-18 22:27:06 +00:00
|
|
|
Services.scheduler().add_job(DeleteVideoJob, args=[video])
|