add pandora_client clean

This commit is contained in:
j 2014-03-14 19:29:14 +01:00
parent 0b2af37553
commit eb448a31d8
2 changed files with 41 additions and 4 deletions

View file

@ -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):
'''

View file

@ -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)