Script update harga saham (indo), reksadana, dan kripto
Saya iseng mengumpulkan beberapa API gratis dari internet untuk menyusun script update harga komoditas di journal hledger saya. Berikut scriptnya, sedottt gan hehe ```bash #!/bin/bash kontan_reksadana_cache=$(mktemp) cleanup() { rm -f "$kontan_reksadana_cache" } trap cleanup EXIT ipot_stock_price() { today=$(date +"%m/%d/%Y") last_week=$(date -d "7 days ago" +"%m/%d/%Y") stock_code=$1 url="https://www.indopremier.com/module/saham/include/json-charting.php?code=$stock_code&start=$last_week&end=$today" until close_price=$(curl -s "$url" | jq '.[length -1][4]' 2>/dev/null); do sleep 1 done echo "$close_price" } kontan_reksadana_price() { if ! grep -q "recordsTotal" "$kontan_reksadana_cache"; then curl -s -X POST "https://pusatdata.kontan.co.id/reksadana/list/" >"$kontan_reksadana_cache" fi mf_name=$1 jq -r ".data[] | select(.[0] == \"$mf_name\")[2]" "$kontan_reksadana_cache" } cryptocompare_price() { fsym=$1 tsym=$2 url="https://min-api.cryptocompare.com/data/price?fsym=$fsym&tsyms=$tsym" until price=$(curl -s "$url" | jq -r ".$tsym" 2>/dev/null); do sleep 1 done echo "$price" } today=$(date +"%Y-%m-%d") tracked_stocks="BBCA BBNI BMRI" for stock in $tracked_stocks; do price=$(ipot_stock_price "$stock") echo P "$today $stock $price IDR" done mf_commodity_names=(KISI_FI I_SRILIKUID) mf_names=(" KISI FIXED INCOME FUND" "Insight Sri Kehati Likuid") for i in "${!mf_commodity_names[@]}"; do price=$(kontan_reksadana_price "${mf_names[$i]}") echo P "$today ${mf_commodity_names[$i]} $price IDR" done # Assuming USDT is approximately equal to USD echo P "$today" USDT "$(ipot_stock_price "USD-IDR")" IDR echo P "$today" BTC "$(cryptocompare_price "BTC" "USD")" USDT ```Created: 2024-10-03 10:37:41, Updated: 2024-10-04 11:30:04, ID: fc35ee2c-78ed-4b2a-b17f-b1849477779c