#!/bin/sh API_LIMIT="500" api_key="../.tinify_api_key" log_info() { printf "[\e[36m%s\e[0m] [\e[32mINFO\e[0m] $*" "$(date +'%H:%M:%S')" } log_warn() { printf "[\e[36m%s\e[0m] [\e[33mWARNING\e[0m] $*" "$(date +'%H:%M:%S')" } log_error() { printf "[\e[36m%s\e[0m] [\e[91mERROR\e[0m] $*" "$(date +'%H:%M:%S')" } check_tools() { tools="curl" for tool in $tools; do if [ ! "$(command -v "$tool")" ]; then log_error "\e[1m$tool\e[0m not found! Exiting....\n" exit 1 fi done } process_image() { j=0 while [ ! -f compressed/"$file" ]; do j="$((j + 1))" if [ "$j" -gt 1 ]; then log_info "Re-try $((j - 1))\n" fi if [ "$j" -gt 10 ]; then log_error "Too many re-tries! Exiting....\n" exit 1 fi file="$(echo "$file" | cut -d '/' -f 2)" orig_size="$(($(stat --printf="%s" files/"$file") / 1024))" if [ "$orig_size" -gt 1024 ]; then orig_size_display="$(printf "%0.2f\n" "$(awk "BEGIN {print ($orig_size)/1024}")") MB" else orig_size_display="$orig_size KB" fi log_info "Compressing \"$file\".... (${orig_size_display}) ($count of $files_count)\n" curl --progress-bar --user api:"$api_key" --data-binary @files/"$file" --output api_response.txt -i https://api.tinify.com/shrink if [ -f api_response.txt ]; then status_code=$(head /dev/null fi continue fi download_url="$(grep tmp files_count="$(wc /dev/null)" if [ "$files_count" -eq 0 ]; then log_error "No pictures found! Exiting....\n" exit 1 fi count=0 while IFS= read -r file; do count="$((count + 1))" process_image done