Skip to content

Instantly share code, notes, and snippets.

@tomachalek
Created October 30, 2015 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomachalek/dc9545277fd767fe884a to your computer and use it in GitHub Desktop.
Save tomachalek/dc9545277fd767fe884a to your computer and use it in GitHub Desktop.
A script to shuffle wallpapers from a source directory to a dest one on our home NAS
import os
import argparse
import random
import hashlib
from functools import partial
import shutil
class TreeWalker(object):
def __init__(self, root_path):
self._root = root_path
def _process_dir(self, path, fn):
for item in os.listdir(path):
abs_path = os.path.realpath('%s/%s' % (path, item))
print(abs_path)
if os.path.isdir(abs_path):
self._process_dir(abs_path, fn)
else:
fn(abs_path)
def __call__(self, fn):
self._process_dir(self._root, fn)
def test_image(path):
try:
suff = path.rsplit('.', 1)[1]
if suff.lower() in ('jpg', 'jpeg', 'gif', 'png'):
return suff.lower()[:3]
except:
return False
def create_random_name(path, dst_dir, suff):
new_basename = hashlib.md5('%s-%s' % (path, random.random())).hexdigest()
return os.path.realpath('%s/%s.%s' % (dst_dir, new_basename, suff))
def copy_image(src_path, dst_dir):
suff = test_image(src_path)
if suff:
dest_path = create_random_name(src_path, dst_dir, suff)
shutil.copyfile(src_path, dest_path)
def delete_stuff(src_path):
os.unlink(src_path)
def import_folder(src_path, dst_path):
death_walker = TreeWalker(dst_path)
death_walker(delete_stuff)
tree_walker = TreeWalker(src_path)
tree_walker(partial(copy_image, dst_dir=dst_path))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Wallpaper shuffle and import')
parser.add_argument('src_dir', metavar='SRC_DIR', type=str, help='Source directory')
parser.add_argument('dst_dir', metavar='DST_DIR', type=str, help='Destination directory')
parser.add_argument('-l', '--limit', type=int)
args = parser.parse_args()
import_folder(args.src_dir, args.dst_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment