Skip to content

Update Currency Exchange Rates #288

Update Currency Exchange Rates

Update Currency Exchange Rates #288

name: 'Update Currency Exchange Rates'
on:
schedule:
- cron: '40 00 * * 2-6'
workflow_dispatch:
inputs:
start_date:
description: 'Start date'
required: true
type: string
default: '2025-02-10'
currency_list:
required: true
type: string
default: 'EUR,GBP,TRY,HKD'
permissions:
contents: write
jobs:
fetch:
runs-on: ubuntu-latest
env:
CI_COMMIT_AUTHOR: github-actions[bot]
CI_COMMIT_AUTHOR_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com
CI_COMMIT_MESSAGE: Update Currency Exchange Rates
CI_URL: https://iss.moex.com/iss/history/engines/currency/markets/index/securities
CI_USER_AGENT: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Read inputs
run: |
if [ -z "${{ inputs.start_date }}" ]; then
START_DATE=$(date --utc -d 'yesterday' +%Y-%m-%d)
BASE_CURRENCY="USD"
CURRENCY_LIST="EUR,GBP,TRY,HKD"
else
START_DATE="${{ inputs.start_date }}"
BASE_CURRENCY="USD"
CURRENCY_LIST="${{ inputs.currency_list }}"
fi
echo "START_DATE=$START_DATE" >> $GITHUB_ENV
echo "BASE_CURRENCY=$BASE_CURRENCY" >> $GITHUB_ENV
echo "CURRENCY_LIST=$CURRENCY_LIST" >> $GITHUB_ENV
- name: RUB per USD. Get exchange rates
continue-on-error: true
run: |
response_code=$(curl --silent \
--output "RUBperUSD.json.TMP" \
--write-out "%{http_code}" \
--user-agent "${{ env.CI_USER_AGENT }}" \
"${{ env.CI_URL }}/USDFIXME.json?iss.meta=on&history.columns=TRADEDATE,OPEN,HIGH,LOW,CLOSE&sort_column=TRADEDATE&sort_order=asc&FROM=${START_DATE}")
if [ "${response_code}" -ne 200 ]; then
echo "Request failed with response code: ${response_code}"
exit 1
fi
- name: Other currencies. Get exchange rates
run: |
response_code=$(curl --silent \
--output "exchangeRates.json" \
--write-out "%{http_code}" \
--user-agent "${{ env.CI_USER_AGENT }}" \
"https://api.frankfurter.dev/v1/${START_DATE}..?base=${BASE_CURRENCY}&symbols=${CURRENCY_LIST}")
if [ "${response_code}" -ne 200 ]; then
echo "Request failed with response code: ${response_code}"
exit 1
fi
- name: Convert into the required format
continue-on-error: true
run: |
jq '.history.data[] | { (.[0]): .[4] }' RUBperUSD.json.TMP | jq -s 'add | to_entries | sort_by(.key) | from_entries' > RUBperUSD.json
IFS=',' read -ra currency_list <<< "${CURRENCY_LIST}"
for currency in "${currency_list[@]}"; do
jq --arg currency "$currency" \
'.rates |
to_entries |
map({(.key): .value[$currency]}) |
add | to_entries | sort_by(.key) | from_entries' exchangeRates.json > "${currency}perUSD.json"
done
rm exchangeRates.json
- name: Merge
run: |
for datafile in ./*.json; do
filename=$(basename $datafile)
if [ -f "marketdata/${filename}" ]; then
cp "marketdata/${filename}" "${filename}.TMP"
jq -s 'add | to_entries | sort_by(.key) | from_entries' "${filename}.TMP" "${filename}" > "marketdata/${filename}"
else
cp "${filename}" "marketdata/${filename}"
fi
done
- name: Cleanup
run: |
rm ./*.json ./*.json.TMP
- name: Commit and push changes
run: |
git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}"
git config --global user.email "${{ env.CI_COMMIT_AUTHOR_EMAIL }}"
git add marketdata/*
git commit -a -m "${{ env.CI_COMMIT_MESSAGE }}"
git push