Skip to content

Instantly share code, notes, and snippets.

@stevenqzhang
Created October 1, 2015 01:43
Show Gist options
  • Save stevenqzhang/e48132f2e9b7f4f79766 to your computer and use it in GitHub Desktop.
Save stevenqzhang/e48132f2e9b7f4f79766 to your computer and use it in GitHub Desktop.
script for getting paco data automatically
#/bin/bash
# be sure to generate a stored refresh token by running set_oauth.sh first (should only need it once unless you revoke the token)
# Read the stored refresh token. Get a new access token with it. Then, finally, doing something with a Paco endpoint.
refresh_token=`cat ~/.qs_paco`
# it's late and I can't seem to reliably cut out wrapping quotes from set_outh. Make sure they are gone here on reload. TODO fix the storage to begin with.
export refresh_token=`sed -e 's/^"//' -e 's/"$//' <<< $refresh_token`
# get a new access token from the refresh token
export new_access_token_response=`curl https://www.googleapis.com/oauth2/v3/token \
-d client_id=$client_id \
-d client_secret=$client_secret \
-d refresh_token=$refresh_token \
-d grant_type=refresh_token`
echo "new_access_token_response: $new_access_token_response"
export access_token=`echo $new_access_token_response | grep 'access_token' | cut -d ":" -f 2 | cut -d "," -f 1 | tr -d '" '`
echo "parsed acess_token: ${access_token}"
echo "new_access_token: $access_token"
# now you can access data with this token
echo "enter experimentId (from definition page on server)"
read experiment_id
echo "enter report format (csv,json,html)"
read report_format
echo "output file name"
read outfile
# we do json directly - it is paged and will not time out on big data sets. Otherwise use csv or html.
# add include_photos query param to also pull base64 encoded photo data where it exists (assuming you collected photos)
if [ "$report_format" = "json" ]; then
result=`curl -H "Authorization: Bearer $access_token" -L "https://quantifiedself.appspot.com/events?q=experimentId=$experiment_id&json"`
else
joburl=`curl -H "Authorization: Bearer $access_token" -L -v "https://quantifiedself.appspot.com/events?q=experimentId=$experiment_id&$report_format" 2>&1 | grep "GET /jobStatus" | cut -d " " -f 3`
echo "joburl = $joburl"
joburl="$joburl&cmdline=1"
result=`curl -H "Authorization: Bearer $access_token" -L "https://quantifiedself.appspot.com$joburl"`
# refresh until the report is ready
while [ "$result" = pending ]; do
result=`curl -H "Authorization: Bearer $access_token" -L "https://quantifiedself.appspot.com$joburl"`
done
fi
echo "$result" > $outfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment