[scripts/wastickers] Rewrote in Python

This rewrite was nessecary, since, apparently, the format of the
sticker packs that WhatsApp reads is a bit... weird. All files start with a
leading slash, which the default `zip` program strips before packing.
The `zipfile` module in Python does not have this check, so it was a nessecary sacrifice.
This commit is contained in:
Alyxia Sother 2021-07-02 22:20:57 +02:00
parent 395ea4806d
commit 1675c8174a
No known key found for this signature in database
GPG Key ID: 355968D14144B739
1 changed files with 46 additions and 45 deletions

View File

@ -1,54 +1,55 @@
#!/bin/env zsh #!/bin/env python
# import os
## Enable MAX STRICTNESS mode from shutil import copyfile, rmtree
set -euo pipefail import subprocess
setopt nomatch nullglob from glob import glob
import time
import pathlib
from zipfile import ZipFile
# Make checking for command existence easier cwd = os.getcwd()
command_exists() { whence -- "$@" &>/dev/null; } pathlib.Path(f'./tmp').mkdir(parents=True, exist_ok=True)
# Create a temporary folder for pack generation which will be # taken from https://stackoverflow.com/a/34177358
# deleted at the very end of the process def is_tool(name: str):
cwd=$(pwd) from shutil import which
mkdir -p $cwd/tmp return which(name) is not None
# Make sure the webp processing tool is present if is_tool('cwebp') == False:
if ! command_exists cwebp; then print('cwebp not found; Make sure it is installed. Exiting.')
echo "cwebp not found; Make sure it is installed. Exiting." exit(1)
exit 1
fi
if ! command_exists zip; then if is_tool('zip') == False:
echo "zip not found; Make sure it is installed. Exiting." print('zip not found; Make sure it is installed. Exiting.')
exit 1 exit(1)
fi
# Read the metadata we will pipe to `author.txt` and `title.txt` author = input('Author: ')
# Seriously, why are these separate text file? At the very least pname = input('Pack name: ')
# store the metadata somewhere reasonable such as a JSON file.
vared -p 'Author: ' -c author
vared -p 'Pack name: ' -c pname
echo "$author" > "$cwd/tmp/author.txt"
echo "$pname" > "$cwd/tmp/title.txt"
for image in *.png *.jpg *.jpeg; do with open(f'./tmp/title.txt', 'w') as f:
cp "$image" "$cwd/tmp/" f.write(pname)
done with open(f'./tmp/author.txt', 'w') as f:
f.write(author)
cd "$cwd/tmp" files = glob(f'./*.png') + glob(f'./*.jpg') + glob(f'./*.jpeg')
for file in files:
copyfile(file, f'./tmp/{file}')
# Every copy of the images we made above will be converted os.chdir(f'{cwd}/tmp')
# into a webp, then the copy will be deleted.
# To make sure all images get a separate timestamp, we wait
# one second for every image.
for image in *.png *.jpg *.jpeg; do
cwebp -q 60 $image -o "$(date +%s).webp" > /dev/null 2>&1
rm -rf $image
sleep 1
done
# Finally, pack up our stickers and clean up the temp dir for file in files:
zip -r -j "$pname.wastickers" "$cwd/tmp"; epoch = int(time.time())
mv "$cwd/tmp/$pname.wastickers" "$cwd"; print(f"Converting {file}...")
cd "$cwd"; subprocess.run(['cwebp', '-q', '60', file, '-o', str(epoch) + '.webp'], stdout=open(os.devnull, 'w'), stderr=subprocess.STDOUT)
rm -rf "$cwd/tmp"; os.remove(f'./{file}')
time.sleep(1)
files = glob('./*')
with ZipFile(f'{pname}.wastickers', 'w') as zip:
for file in files:
with open(file, 'rb') as image_file:
zip.writestr(f"/{file}", image_file.read())
os.chdir(cwd)
copyfile(f'./tmp/{pname}.wastickers', f'./{pname}.wastickers')
rmtree('./tmp')