Skip to content

Instantly share code, notes, and snippets.

@scarytom
Created June 15, 2016 09:00
Show Gist options
  • Save scarytom/c5d59bc9aa97d20f6eb7f1f180f0f02f to your computer and use it in GitHub Desktop.
Save scarytom/c5d59bc9aa97d20f6eb7f1f180f0f02f to your computer and use it in GitHub Desktop.
Script to call openfigi with a list of ISINs and return instrument data as JSON
#!/bin/sh -eu
BATCH_SIZE=100
debug() {
#echo "${1}" 1>&2
true
}
isin_to_json() {
echo "{\"idType\": \"ID_ISIN\", \"idValue\": \"${1}\"}"
}
curl_to_figi() {
RESPONSE="$(curl -s -H "X-OPENFIGI-APIKEY: ${API_KEY}" \
-H 'Content-Type: text/json' \
-d "${1}" \
'https://api.openfigi.com/v1/mapping')"
debug "^^${RESPONSE}^^"
echo "${RESPONSE}" | sed -r 's/,\{\"(data|error)\":/,\n\{\"\1\":/g'
}
process_isin_batch() {
REQUEST=''
for ISIN in ${@}; do
REQUEST="${REQUEST},$(isin_to_json "${ISIN}")"
done
RESULT="$(curl_to_figi "[${REQUEST#,}]")"
debug "${@}"
debug ">>${RESULT}<<"
CNT=1
echo "${RESULT}" | \
while IFS=$'\n' read JSON; do
debug "-->${CNT} ${JSON}"
ISIN=$(eval echo \${${CNT}})
echo "${JSON}" | sed -r "s/\"(data|error)/\"isin\":\"${ISIN}\", \"\1/"
CNT=$((${CNT}+1))
done
unset IFS
}
CNT=0
ISIN_BATCH=''
for ISIN in "$@"; do
ISIN_BATCH="${ISIN_BATCH} ${ISIN}"
CNT=$((${CNT}+1))
if [ "${CNT}" -eq "${BATCH_SIZE}" ]; then
process_isin_batch ${ISIN_BATCH}
CNT=0
ISIN_BATCH=''
fi
done
if [ -n "${ISIN_BATCH}" ]; then
process_isin_batch ${ISIN_BATCH}
fi
@scarytom
Copy link
Author

Usage:
API_KEY='my openfigi API key' openfigi.sh ISIN1 ISIN2 ISIN3 ISIN4 etc

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