Last active
December 19, 2015 01:29
-
-
Save luisbosque/5876697 to your computer and use it in GitHub Desktop.
Import CartoDB file with cURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./cdb_import.sh <cdb_username> <api_key> <filename> |
Mac safe version => https://gist.github.com/andrewxhill/5884845
Okay, to make this really useful, I have a few questions.
- Does the import api report the table name/id back?
- It would be really amazing if we could 'replace' a table via the API
- 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
May I suggest to embed usage string in the script itself and show it on syntax error (show usage and exit)
Very useful tool, how about putting under script/ dir of cartodb ?