mkgallery

Instagram clone MDA
Log | Files | Refs

commit deb691a536621e0ec3d433faf89a2454d93c9c84
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Thu, 17 Oct 2019 18:40:47 +0300

First

Diffstat:
Amkgallery.py | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 74 insertions(+), 0 deletions(-)

diff --git a/mkgallery.py b/mkgallery.py @@ -0,0 +1,74 @@ +#! /usr/local/bin/python2 + +import base64 +import datetime +import email +import errno +import hashlib +import os +import re +import subprocess +import sys + +os.chdir( "/var/www/gallery" ) +mail = email.message_from_file( sys.stdin ) +now = datetime.datetime.now().strftime( "%Y-%m-%d" ) + +def try_mkdir( path ): + try: + os.makedirs( path ) + except OSError as e: + if e.errno != errno.EEXIST: + raise + +try_mkdir( now ) + +# parse + save images from incoming mail +def save_image( part, ext ): + body = base64.b64decode( part.get_payload() ) + checksum = hashlib.sha256( body ).hexdigest() + filename = checksum + "." + ext + fullpath = now + "/" + filename + thumbpath = os.path.splitext( now + "/thumb_" + filename )[ 0 ] + ".jpg" + + f = open( fullpath, "w" ) + f.write( body ) + f.close() + + # generate thumbnail + subprocess.call( [ "convert", fullpath, "-resize", "400x400>", "-quality", "80", "-auto-orient", thumbpath ] ) + +for part in mail.walk(): + ct = part.get_content_type() + if ct == "image/jpeg": + save_image( part, "jpg" ) + elif ct == "image/png": + save_image( part, "png" ) + +# build a new gallery page +page = open( now + "/index.html", "w" ) +page.write( "<style>" ) +page.write( "body { margin: 0; }" ) +page.write( "img { width: 100%; height: auto; margin-bottom: 1em; }" ) +page.write( "@media screen and ( min-width: 700px ) {" ) +page.write( "body { column-count: 4; column-gap: 1em; margin: 1em; }" ) +page.write( "}" ) +page.write( "</style>" ) +for filename in os.listdir( now ): + if filename.startswith( "thumb_" ): + continue + if filename.endswith( ".jpg" ) or filename.endswith( ".png" ): + page.write( '<a href="{}"><img src="{}"></a><br>\n'.format( filename, "thumb_" + filename ) ) +page.close() + +# build new index +galleries = [ ] +for gallery in os.listdir( "." ): + galleries.append( gallery ) +galleries.sort( reverse = True ) + +index = open( "index.html", "w" ) +for gallery in galleries: + if re.match( r"\d{4}-\d{2}-\d{2}", gallery ): + index.write( '<h3><a href="{}">{}</a></h3>\n'.format( gallery, gallery ) ) +index.close()