From 79cda3127613f640e49ff89753c8f2d58fc55ce4 Mon Sep 17 00:00:00 2001 From: rra Date: Mon, 19 Mar 2018 22:40:27 +0100 Subject: [PATCH] added ability to make thumbnails, cleaned code --- README.md | 14 ++++++++++++-- distribusi.py | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e1d8680..0e92555 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,12 @@ distbusi is a content management system for the web that produces static index p ##installation -distribusi depends on `python-magic` 0.4+ to do automatic mime (filetype) detection +distribusi depends on: +* `python-magic` 0.4+ to do automatic mime (filetype) detection +* `pillow` to generate thumbnails using pip: - sudo pip3 install python-magic + sudo pip3 install python-magic pillow ##usage @@ -17,4 +19,12 @@ Make a distribusi of your home folder: You will find that you now have an `index.html` in every folder. +Create a quick gallery for the web: + python3 distribusi -d /path/to/my/photos -t + +This creates an `index.html` with `base64` encoded thumbnails + +Generate verbose output: + python3 distribusi -v + #✌ diff --git a/distribusi.py b/distribusi.py index 27d9bee..32ea3e6 100755 --- a/distribusi.py +++ b/distribusi.py @@ -2,19 +2,31 @@ # -*- coding: iso-8859-15 -*- #depends on python-magic 0.4 -import os, magic, argparse, sys +import os, magic, argparse, sys, base64 from page_template import * +from PIL import Image +from io import BytesIO parser = argparse.ArgumentParser(""" distbusi is a content management system for the web that produces static index pages based on folders in the filesystem. It is inspired by the automatic index functions featured in several web servers. It works by traversing the file system and directory hierarchy to automatically list all the files in the directory and providing them with html classes and tags for easy styling. """) parser.add_argument('-d', '--directory', help="Select which directory to distribute") parser.add_argument('-v', '--verbose', help="Print verbose debug output", action="store_true") +parser.add_argument('-t', '--thumbnail', help="Generate 150x150 thumbnails for images", action="store_true") + +#Todo: +# build an 'undo' function that traverses the same directories and removes the index.html files +# set the 'maxdepth' +# toggle .folders + + args = parser.parse_args() if args.directory: if args.verbose: print('Generating directory listing for', args.directory) + if args.thumbnail: + print('Making thumbnails') directory = args.directory else: directory = '.' @@ -26,20 +38,26 @@ file_types = {'image':'', 'pdf':'".format(name, data_url) -def dict_by_value(dictionary, value): - return(list(dictionary.keys())[list(dictionary.values()).index(value)]) # returns the key for the given value - def div(mime, tag, *values): #name, full_path - class_name = values[0].split('.')[0].replace(' ', '_') + id_name = values[0].split('.')[0].replace(' ', '_') + thumbnail = values[1] if 'image' in mime: - html ='
'.format(class_name)+tag+'
{}
'.format(values[0]) - + html = '
'.format(id_name)+tag+'
{}
'.format(values[0]) elif 'pdf' in format: - html ='
'.format(class_name)+tag+'
{}
'.format(values[0]) + html ='
'.format(id_name)+tag+'
{}
'.format(values[0]) else: - html = '
'.format(class_name)+tag+'
'.format(values[0]) + html = '
'.format(id_name)+tag+'
'.format(values[0]) return html for root, dirs, files in os.walk(directory): @@ -65,6 +83,9 @@ for root, dirs, files in os.walk(directory): a = "
"+open(full_path).read()+"
" else: a = file_types[mime] + + if mime == 'image' and args.thumbnail: + a = thumbnail(full_path, name) else: a = file_types[mime] @@ -76,8 +97,9 @@ for root, dirs, files in os.walk(directory): if args.verbose: print ('mime-type not in list, adding as href: \n', mime,format, name) + a = a.replace('{}',name) - html.append(div(mime,a,name)) + html.append(div(mime,a,name,args.thumbnail)) #html.append(a) if root != directory: html.append('../')