Skip to content

Instantly share code, notes, and snippets.

@luisbosque
Last active December 19, 2015 01:29
Show Gist options
  • Save luisbosque/5876697 to your computer and use it in GitHub Desktop.
Save luisbosque/5876697 to your computer and use it in GitHub Desktop.
Import CartoDB file with cURL
#!/bin/bash
CDB_USER=$1
API_KEY=$2
IMPORT_FILE=$3
PROTOCOL=https
DEBUG=false
ITEM_ID_REGEX='\"([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\"'
if [[ -z $CDB_USER ]]
then
echo "Missing user"
exit 1
fi
if [[ -z $API_KEY ]]
then
echo "Missing api key"
exit 1
fi
if [[ -z $IMPORT_FILE ]]
then
echo "Missing file"
exit 1
fi
function log {
[[ ${DEBUG} == true ]] && echo $1
}
v1=$(uname)
log "Sending file..."
if [[ "$v1" = Darwin ]];
then
job_id=`curl -s -F file=@${IMPORT_FILE} "${PROTOCOL}://${CDB_USER}/api/v1/imports/?api_key=${API_KEY}" | sed -E "s/\{\"item_queue_id\":${ITEM_ID_REGEX}.*/\1/"`
else
job_id=`curl -s -F file=@${IMPORT_FILE} "${PROTOCOL}://${CDB_USER}/api/v1/imports/?api_key=${API_KEY}" | sed -r "s/\{\"item_queue_id\":${ITEM_ID_REGEX}.*/\1/"`
fi
log "Waiting for job ${job_id} to be completed..."
while true
do
if [[ "$v1" = Darwin ]];
then
status=`curl -s "${PROTOCOL}://${CDB_USER}/api/v1/imports/${job_id}?api_key=${API_KEY}" | sed -E 's/(.*)\"state\":\"([a-z]+)\"(.*)/\2/'`
else
status=`curl -s "${PROTOCOL}://${CDB_USER}/api/v1/imports/${job_id}?api_key=${API_KEY}" | sed -r 's/(.*)\"state\":\"([a-z]+)\"(.*)/\2/'`
fi
log "STATE: ${status}"
if [[ $status == 'complete' ]]
then
log "Import successful"
exit 0
elif [[ $status == 'failure' ]]
then
log "Failed import"
exit 1
fi
sleep 2
done
./cdb_import.sh <cdb_username> <api_key> <filename>
@andrewxhill
Copy link

Okay, to make this really useful, I have a few questions.

  1. Does the import api report the table name/id back?
  2. It would be really amazing if we could 'replace' a table via the API
  3. Can you toggle public/private via the API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment