Skip to content

Update Silver Price #242

Update Silver Price

Update Silver Price #242

Workflow file for this run

name: Update Silver Price
on:
schedule:
- cron: '0 18 * * *' # 12:00 AM BDT
workflow_dispatch:
jobs:
update-price:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Fetch Silver Price & Generate JSON (retry on failure)
uses: nick-invision/retry@v3
env:
GOLD_API_KEY: ${{ secrets.GOLD_API_KEY }}
with:
timeout_minutes: 5
max_attempts: 5
retry_wait_seconds: 20
command: |
set -e
mkdir -p price_data
echo "Fetching Silver Price (USD)..."
silver_response=$(curl -s -H "x-access-token: $GOLD_API_KEY" \
"https://www.goldapi.io/api/XAG/USD")
# Hard fail if response is not valid JSON
echo "$silver_response" | jq empty
silver_price_usd=$(echo "$silver_response" | jq -r '.price')
if [[ -z "$silver_price_usd" || "$silver_price_usd" == "null" ]]; then
echo "Silver price missing"
exit 1
fi
echo "Fetching USD to BDT Rate..."
currency_response=$(curl -s "https://open.er-api.com/v6/latest/USD")
# Hard fail if response is not valid JSON
echo "$currency_response" | jq empty
usd_to_bdt=$(echo "$currency_response" | jq -r '.rates.BDT')
if [[ -z "$usd_to_bdt" || "$usd_to_bdt" == "null" ]]; then
echo "USD/BDT rate missing"
exit 1
fi
price_bdt=$(echo "scale=4; $silver_price_usd * $usd_to_bdt" | bc)
price_per_gram=$(echo "scale=4; $price_bdt / 31.1035" | bc)
mahr_fatemi=$(echo "scale=0; $price_per_gram * 1530.9" | bc)
minimum_mahr=$(echo "scale=0; $price_per_gram * 30.618" | bc)
timestamp=$(date +%s)
jq -n \
--argjson ts "$timestamp" \
--arg metal "XAG" \
--arg currency "BDT" \
--argjson price "$price_bdt" \
--argjson rate "$silver_price_usd" \
--argjson ex "$usd_to_bdt" \
--argjson fatemi "$mahr_fatemi" \
--argjson min "$minimum_mahr" \
'{
timestamp: $ts,
metal: $metal,
currency: $currency,
price: $price,
rate_usd: $rate,
exchange_rate: $ex,
mahr_fatemi: $fatemi,
minimum_mahr: $min
}' > price_data/price.json
cat price_data/price.json
- name: Deploy to Data Branch
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./price_data
publish_branch: data
user_name: 'github-actions[bot]'
user_email: 'github-actions[bot]@users.noreply.github.com'
commit_message: "Update silver price [skip ci]"
force_orphan: true