From eb448a31d8c8a3bf0131f06907393177c9cdd0e7 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Fri, 14 Mar 2014 19:29:14 +0100 Subject: [PATCH] add pandora_client clean --- pandora_client/__init__.py | 30 ++++++++++++++++++++++++++---- pandora_client/utils.py | 15 +++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/pandora_client/__init__.py b/pandora_client/__init__.py index 0ee4623..ee11c5a 100644 --- a/pandora_client/__init__.py +++ b/pandora_client/__init__.py @@ -816,10 +816,32 @@ class Client(object): }) return files - def clean(self): - print "remove temp videos and stills" - if os.path.exists(self.prefix()): - shutil.rmtree(self.prefix()) + def clean(self, args): + if os.path.exists(self.api.media_cache): + if args and args[0] == 'all': + print "remove all cached videos in", self.api.media_cache + + #if os.path.exists(self.api.media_cache): + # shutil.rmtree(self.api.media_cache) + else: + nothing = False + for root, folders, files in os.walk(self.api.media_cache): + for f in files: + f = os.path.join(root, f) + if f.endswith('.webm'): + oshash = os.path.dirname(f)[len(self.api.media_cache):].replace('/', '') + remove = True + for path in self.path(oshash): + if os.path.exists(path): + remove = False + break + if remove: + nothing = False + os.unlink(f) + if nothing and folders: + print "No unused files found in cache, run \"%s clean all\" to remove the entire cache" % sys.argv[0] + else: + utils.cleanup_tree(self.api.media_cache) def import_srt(self, args): ''' diff --git a/pandora_client/utils.py b/pandora_client/utils.py index 3a661aa..0cc86b8 100644 --- a/pandora_client/utils.py +++ b/pandora_client/utils.py @@ -75,3 +75,18 @@ def video_frame_positions(duration): #return [pos/4, pos/2, pos/2+pos/4, pos, pos+pos/2, pos+pos/2+pos/4] return map(int, [pos/2, pos, pos+pos/2]) +def cleanup_tree(root): + again = False + for prefix, folders, files in os.walk(root): + removed = [] + for f in files: + if f in ('.DS_Store', 'Thumbs.db'): + os.unlink(os.path.join(prefix, f)) + removed.append(f) + files = list(set(files) - set(removed)) + if not folders and not files and len(prefix) > len(root): + os.rmdir(prefix) + again = True + if again: + cleanup_tree(root) +