From 61cdbd495f22a8534e6424f459707ccd284c6723 Mon Sep 17 00:00:00 2001 From: Daniel Brackett Date: Tue, 18 Jun 2019 18:36:18 -0700 Subject: [PATCH] making the files auto-delete after a week. --- fediplay/queue.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/fediplay/queue.py b/fediplay/queue.py index c855b78..19a7cfb 100644 --- a/fediplay/queue.py +++ b/fediplay/queue.py @@ -1,6 +1,7 @@ '''The play queue.''' -from os import path +from os import path, listdir, makedirs, remove, utime +from time import time, localtime import shlex from subprocess import run from threading import Thread, Lock @@ -56,7 +57,6 @@ class Getter(object): '''Fetches music from a url.''' # pylint: disable=too-few-public-methods - def __init__(self, cache_dir): self.filename = None self.filenames = [] @@ -73,6 +73,9 @@ class Getter(object): def get(self, url): '''Fetches music from the given url.''' + '''deleting files here''' + auto_delete_files(self.cache_dir) + options = { 'format': 'mp3/mp4', 'nocheckcertificate': env.no_check_certificate(), @@ -82,6 +85,9 @@ class Getter(object): with YoutubeDL(options) as downloader: downloader.download([url]) + for file in self.filenames: + utime(file) + return self.filenames def build_play_command(filename): @@ -90,3 +96,11 @@ def build_play_command(filename): escaped_filename = shlex.quote(filename) template = env.play_command() return template.format(filename=escaped_filename) + +def auto_delete_files(cache_dir): + for the_file in listdir(cache_dir): + file_path = path.join(cache_dir, the_file) + if path.isfile(file_path): + file_time = path.getmtime(file_path) + if file_time + 604800 < time(): + remove(file_path) \ No newline at end of file