Skip to content

Instantly share code, notes, and snippets.

@davidselassie
Created January 25, 2018 21:56
Show Gist options
  • Save davidselassie/a3abfbef468ae97ea76a586b2b6bb437 to your computer and use it in GitHub Desktop.
Save davidselassie/a3abfbef468ae97ea76a586b2b6bb437 to your computer and use it in GitHub Desktop.
Script to find matching Plaid Item IDs from a list of Plaid Access Tokens
#!/bin/bash
if [[ "$1" == '-h' || "$1" == '--help' ]]
then
echo "USAGE: $(basename $0) < ACCESS_TOKENS.csv > ITEM_ACCESS_TOKENS.csv" 1>&2
echo "Takes a CSV of Access Tokens from Plaid in the column 'access_token' and finds the" 1>&2
echo "corresponding Item ID and outputs that in the column 'item_id', along with the" 1>&2
echo "original Access Token in 'access_token'." 1>&2
exit 1
fi
set -Eeuo pipefail
SECRET="$(cat ~/everything/tmp/env/pantry/PLAID_API_SECRET)"
CLIENT_ID=57e43d13f5c9c9795f46a4d6
ACCESS_TOKENS_JSONS="access_tokens.jsons"
ITEM_IDS_JSONS="item_ids.jsons"
mlr --icsv --ojson filter '$access_token =~ "^access-"' > "$ACCESS_TOKENS_JSONS"
# Even though we're Plaid-bound, only 30 jobs is possible otherwise parallel explodes and opens a
# too many FDs.
# mlr also doesn't like multi-line JSON blobs, so pass Plaid's multi-line output through jq -c.
mlr --json put "\$client_id = \"$CLIENT_ID\"" then \
put "\$secret = \"$SECRET\"" "$ACCESS_TOKENS_JSONS" | \
parallel --bar -j 30 -k -q \
curl -s https://production.plaid.com/item/get \
-H 'Content-Type: application/json' \
-d {} | \
jq --unbuffered -c '{item_id: .item.item_id}' > "$ITEM_IDS_JSONS"
paste <(mlr --ijson --otsv cat "$ACCESS_TOKENS_JSONS") \
<(mlr --ijson --otsv cat "$ITEM_IDS_JSONS") | \
mlr --itsv --ocsv cat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment