]> git.0d.be Git - botaradio.git/commitdiff
Clear tmp folder and good creating time
authorazlux <github@azlux.fr>
Tue, 12 Jun 2018 23:16:32 +0000 (01:16 +0200)
committerazlux <github@azlux.fr>
Tue, 12 Jun 2018 23:16:32 +0000 (01:16 +0200)
fix for #10
feature for #8

configuration.default.ini
media.py
mumbleBot.py

index 927fa6f488a6ff047b3e85babe5af8aea3e6cda2..f519775035904a090c2646983f8af342aa4eb8d7 100644 (file)
@@ -1,9 +1,13 @@
 [bot]
 comment = Hi, I'm here to play radio, local music or youtube/soundcloud music. Have fun !
 volume = 0.1
-admin = User1;User2;
-music_folder = /home/azlux/botamusique/music_folder
+admin = User1;User2; # Allow user to kill the bot
+music_folder = /home/azlux/botamusique/music_folder/
 tmp_folder = /tmp/
+
+# in Mo, 0 for no cache, -1 for illimited size
+tmp_folder_max_size = 10
+
 ignored_folders = tmp
 ignored_files = Thumbs.db
 
index 8fbae1d5950f316561dfbb9acff824f64f998716..30c512a261f80d6f68882fba507f35319f48789c 100644 (file)
--- a/media.py
+++ b/media.py
@@ -4,6 +4,7 @@ import logging
 import json
 import http.client
 import struct
+import os
 
 
 def get_radio_server_description(url):
@@ -76,3 +77,40 @@ def get_url(string):
         return res.group(1)
     else:
         return False
+
+
+def get_size_folder(path):
+    folder_size = 0
+    for (path, dirs, files) in os.walk(path):
+        for file in files:
+            filename = os.path.join(path, file)
+            folder_size += os.path.getsize(filename)
+    return int(folder_size / (1024 * 1024))
+
+
+def clear_tmp_folder(path, size):
+    if size == -1:
+        return
+    elif size == 0:
+        for (path, dirs, files) in os.walk(path):
+            for file in files:
+                filename = os.path.join(path, file)
+                os.remove(filename)
+    else:
+        if get_size_folder(path=path) > size:
+            all_files = ""
+            for (path, dirs, files) in os.walk(path):
+                all_files = [os.path.join(path, file) for file in files]
+                all_files.sort(key=lambda x: os.path.getmtime(x))
+            size_tp = 0
+            print(all_files)
+            for idx, file in enumerate(all_files):
+                size_tp += os.path.getsize(file)
+                if int(size_tp/(1024*1024)) > size:
+                    logging.info("Cleaning tmp folder")
+                    to_remove = all_files[idx:]
+                    print(to_remove)
+                    for f in to_remove:
+                        logging.debug("Removing " + f)
+                        os.remove(os.path.join(path, f))
+                    return
index fb533c600629c3aad9f03c4daaa96cd400e5bb23..9447fc37c19025ed4710b4e7355e55c2981f6f5e 100644 (file)
@@ -249,6 +249,7 @@ class MumbleBot:
             url = media.get_url(var.current_music[1])
             if not url:
                 return
+            media.clear_tmp_folder(var.config.get('bot', 'tmp_folder'), var.config.getint('bot', 'tmp_folder_max_size'))
             path, title = self.download_music(url)
             var.current_music[1] = url
 
@@ -295,6 +296,7 @@ class MumbleBot:
             'outtmpl': path,
             'noplaylist': True,
             'writethumbnail': True,
+            'updatetime': False,
             'postprocessors': [{
                 'key': 'FFmpegExtractAudio',
                 'preferredcodec': 'mp3',