View Single Post
Old June 26th, 2017, 04:09 PM   #17
retroanalyst
in memoriam Max
 
retroanalyst's Avatar
 
Join Date: Jun 2009
Location: Germany
Posts: 3,586
Thanks: 121,979
Thanked 47,461 Times in 3,337 Posts
retroanalyst 175000+retroanalyst 175000+retroanalyst 175000+retroanalyst 175000+retroanalyst 175000+retroanalyst 175000+retroanalyst 175000+retroanalyst 175000+retroanalyst 175000+retroanalyst 175000+retroanalyst 175000+
Default Gallery upload script

I was unhappy with the lack of gallery support in existing pixhost upload tools, so I decided to sit down and write my own little shell script. All it currently does is take a folder name as argument (and a gallery name, optionally) and upload all images in that folder to a gallery on pixhost. At the end, it prints the gallery URL which you had better save lest you never find your uploaded pictures.

Not really sophisticated, but it's less than 50 lines of code. Professionals like herkalurk might scoff at it, but for us laymen it's a start.

Code:
#!/bin/sh
# Script to upload all images in a directory to pixhost,
# creating a gallery there.
# Parameters: directory containing the files to upload (mandatory),
# gallery name (recommended but optional, use directory name by default)

set -e
set -u

DIR=$1
[ -n "$DIR" ] || { echo "$0: Need a directory argument" >&2; exit 1; }
[ -d "$DIR" ] || { echo "$0: $1 is not a directory" >&2; exit 1; }

GALLERY_NAME=${2:-$(basename "${DIR}")}

# Initialize gallery and extract returned values
PARAMS=$(curl -s -S -X POST --include "https://api.pixhost.to/galleries" \
  -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
  -H 'Accept: application/json' \
  -d "gallery_name=${GALLERY_NAME}" | tail -n1 | tr -d '"')
HASH=$(echo $PARAMS | sed 's/.*gallery_hash://' | sed 's/[,}].*//')
URL=$(echo $PARAMS | sed 's/.*gallery_url://' | sed 's/[,}].*//')
UPLOAD_HASH=$(echo $PARAMS | sed 's/.*gallery_upload_hash://' | sed 's/[,}].*//')

# Upload images, silently skipping apparent non-image files for now.
for file in "${DIR}"/* ; do
    case ${file} in
	*jpg|*.JPG|*.jpeg|*.gif|*.png)
	    curl -s -S -X POST --include "https://api.pixhost.to/images" \
		 -H 'Content-Type: multipart/form-data; charset=utf-8' \
		 -H 'Accept: application/json' \
		 -F "img=@${file}" \
		 -F 'content_type=1' \
		 -F 'max_th_size=180' \
		 -F "gallery_hash=$HASH" \
		 -F "gallery_upload_hash=$UPLOAD_HASH" > /dev/null
	    ;;
    esac
done

# Finalize gallery
curl -s -S -X POST --include "https://api.pixhost.to/galleries/$HASH/finalize" \
  -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
  -H 'Accept: application/json' \
  -d "gallery_upload_hash=$UPLOAD_HASH" > /dev/null

echo "Gallery \"$GALLERY_NAME\" available at $URL."
Windows users will have to install a few basic tools (bash, sed, coreutils) and of course curl before they can use the script.
retroanalyst is offline   Reply With Quote
The Following 4 Users Say Thank You to retroanalyst For This Useful Post: