Skip to content

Instantly share code, notes, and snippets.

@fredericcambon
Last active May 6, 2020 18:58
Show Gist options
  • Save fredericcambon/0437cf3a847c38b1e8f8 to your computer and use it in GitHub Desktop.
Save fredericcambon/0437cf3a847c38b1e8f8 to your computer and use it in GitHub Desktop.
Sketchfab - v2 api upload bash script
#!/bin/bash -e
##
# Sample script for uploading to sketchfab
# using the v2 api
##
##
# Uploading a model to Sketchfab is a two step process
#
# 1. Upload a model. If the upload is successful, the api will return
# the model's id, and the model will be placed in the processing queue
#
# 2. Poll for the processing status
# You can use your model id (see 1.) to poll the model processing status
# The processing status can be one of the following:
# - PENDING: the model is in the processing queue
# - PROCESSING: the model is being processed
# - SUCCESSED: the model has being sucessfully processed and can be view on sketchfab.com
# - FAILED: the processing has failed. An error message detailing the reason for the failure
# will be returned with the response
#
# HINTS
# - limit the rate at which you poll for the status (once every few seconds is more than enough)
##
tempfiles=( )
cleanup() {
rm "${tempfiles[@]}"
}
trap cleanup 0
function extract_json_value() {
local json="$1"
local key="$2"
cat <<-EOF | python -
import json
import re
with open("$json", "r") as response:
data = json.load(response)
assert "$key" in data
print json.dumps(data["$key"])
EOF
}
function poll_processing_status() {
local id="${1}"
local url="https://${server}/v2/models/${id}/status?token=${token}"
local model_url="https://${server}/models/${id}"
local timeout=120
local iter=0
local id_file="${2}"
echo -e "\nPolling status of model ${id}\n"
curl -k ${url} >${id_file}
while [ "$( extract_json_value ${id_file} "processing" | tr -d '"' )" != "SUCCEEDED" ]
do
if [ "${iter}" == "${timeout}" ]
then
echo "Timeout while polling on ${url}"
return 1
else
iter=$((iter+1))
sleep 1
fi
curl -k ${url} >${id_file}
done
echo -e "\nProcessing successful. Check your model here: ${model_url}\n"
}
function upload_model() {
local id_file="${7}"
echo -e "\nUploading model ${1}, to ${server}\n"
curl -k -X POST -F "source=bash" \
-F "modelFile=@${1}" \
-F "name=${2}" \
-F "description=${3}" \
-F "password=${4}" \
-F "private=${5}" \
-F "tags=${6}" \
-F "token=${token}" \
https://${server}/v2/models >${id_file}
}
function upload_v2() {
local id_file="$( mktemp -t XXXXXXXXX )"
tempfiles+=( "$id_file" )
upload_model "${1}" "${2}" "${3}" "${4}" ${5} "${6}" "${id_file}"
id="$( extract_json_value ${id_file} "uid" | tr -d '"' )"
poll_processing_status "${id}" "${id_file}"
}
##################################################
# Upload a model an poll for its processing status
##################################################
server="sketchfab.com"
token="YOUR_API_TOKEN"
# Mandatory parameters
model_file="/sketchfab/src/data/tests/triangle.obj" # path to your model
# Optional parameters
name="A Bob model"
description="This is a bob model I made with love and passion"
password="" # requires a pro account
private=0 # requires a pro account
tags="bob character video-games" # space-separated list of tags
upload_v2 "${model_file}" "${name}" "${description}" "${password}" ${private} "${tags}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment