Created
November 19, 2018 20:47
-
-
Save chocolatkey/8bbc7a0cd55bb7eee7ca2d9cf7c1c9f5 to your computer and use it in GitHub Desktop.
Automatically optimize images with guetzli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Cron Image optimizer | |
# chocolatkey (Henry) 2018-11-13 | |
version = "1.1.0" | |
THREADS = 8 | |
GUETZLI = "/opt/optim/guetzli" | |
BASE = "/data/cache" | |
LOCK_FILE = BASE + "/optim.lock" | |
import os | |
import sys | |
import csv | |
import datetime | |
from queue import Queue | |
import threading | |
def worker(): | |
global already_optimized, q | |
while True: | |
obj = q.get() | |
if obj is None: | |
break | |
fpath = os.path.join(obj[0], obj[1]) | |
print("Optimizing " + fpath) | |
# Run Guetzli (will take a while) | |
os.system("nice -5 " + GUETZLI + " --nomemlimit " + fpath + " " + fpath) | |
# Add to optimized list | |
already_optimized.append(obj[1]) | |
q.task_done() | |
def main(): | |
global already_optimized, q | |
print("CronOptim " + version) | |
if os.path.exists(LOCK_FILE): | |
print("Lock file exists, exiting") | |
sys.exit(0) | |
else: | |
with open(LOCK_FILE, 'w', encoding='utf-8') as lockfile: | |
lockfile.write(str(datetime.datetime.now())) | |
for root, dirs, files in os.walk(BASE): | |
if root is BASE: | |
continue | |
# Each part cache directory | |
already_optimized = [] | |
print("---------" + root + "---------") | |
stat_path = root + "_optim.csv" | |
if os.path.exists(stat_path): | |
print("Stat file exists for " + root) | |
with open(stat_path, newline='', encoding='utf-8') as csvfile: | |
stat_reader = csv.reader(csvfile) | |
for row in stat_reader: | |
already_optimized.append(row[0]) # add filename to skip list | |
print("Directory " + root + " has " + str(len(already_optimized)) + ", " + str((len(files) - len(already_optimized))) + " left") | |
else: | |
print("Directory " + root + " has not yet been optimized at all") | |
q = Queue(maxsize=0) | |
threads = [] | |
for i in range(THREADS): | |
t = threading.Thread(target=worker) | |
t.start() | |
threads.append(t) | |
# Optimize files | |
for name in files: | |
if name in already_optimized: | |
print(name + " already optimized") | |
else: | |
q.put((root, name)) | |
for i in range(THREADS): | |
q.put(None) | |
for t in threads: | |
t.join() | |
csv_data = [] | |
for item in already_optimized: | |
csv_data.append((item,)) | |
with open(stat_path, 'w', newline='', encoding='utf-8') as csvfile: | |
writer = csv.writer(csvfile) | |
writer.writerows(csv_data) | |
print("Finished, removing lock") | |
os.remove(LOCK_FILE) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment