Last active
January 8, 2025 13:57
-
-
Save nerun/515c5e77ad7ebc1b5b7fa8df6ced2a8b to your computer and use it in GitHub Desktop.
Imprime índices econômicos direto no terminal. Compatibilidade POSIX: testado em sh, bash e zsh.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# indices.sh - version 1.5 - 08/01/2025 | |
# Copyright (c) 2024, 2025 Daniel Dias Rodrigues <[email protected]>. No | |
# rights reserved. | |
# | |
# This program is free software; you can redistribute it and/or modify it | |
# under the terms of the Creative Commons Zero 1.0 Universal (CC0 1.0) Public | |
# Domain Dedication (https://creativecommons.org/publicdomain/zero/1.0/). | |
# | |
URL_DOLAR="http://www.yahii.com.br/dolardiario" | |
URL_EURO="http://www.yahii.com.br/eurodiario" | |
URL_OURO="https://pt.bullion-rates.com/gold/BRL-history.htm" | |
URL_BTC="https://www.infomoney.com.br/cotacoes/cripto/ativo/bitcoin-btc" | |
URL_DOLAR_REALTIME="https://www.xe.com/currencyconverter/convert/?Amount=1&From=USD&To=BRL" | |
URL_EURO_REALTIME="https://www.xe.com/currencyconverter/convert/?Amount=1&From=EUR&To=BRL" | |
_validContent(){ | |
local CONTENT="$(wget -qO- $1$(date +%y).html)" | |
local CURRENT="" | |
local COUNT=0 | |
while [ -z "$CURRENT" ]; do | |
COUNT=$(($COUNT + 1)) | |
DATE=$(date --date="$(date +%F) -$COUNT day" +%x) | |
YEAR=$(echo "$DATE" | cut -c 7-10) # Extrai os 4 primeiros caracteres do ano | |
if [ "$YEAR" -eq "$(($(date +%Y) - 1))" ]; then | |
CONTENT="$(wget -qO- $1$(($(date +%y)-1)).html)" | |
fi | |
CURRENT=$(echo "$CONTENT" | grep "$DATE" -A2) | |
done | |
if [ -z "$DATE" ]; then | |
DATE=$(date +%x) | |
fi | |
COIN=$(echo "$CURRENT" | tail -2 | sed 's/ //g' | sed 's/<.*=2>//g' | sed 's/<.*//g') | |
COINc=$(echo "$COIN" | head -1) | |
COINv=$(echo "$COIN" | tail -1) | |
echo "$DATE $COINc $COINv" | |
} | |
USD=$(_validContent "$URL_DOLAR") | |
USD_DATE=$(echo "$USD" | cut -d' ' -f1) | |
USD_BUY=$(echo "$USD" | cut -d' ' -f2) | |
USD_SELL=$(echo "$USD" | cut -d' ' -f3) | |
EURO=$(_validContent "$URL_EURO") | |
EURO_DATE=$(echo "$EURO" | cut -d' ' -f1) | |
EURO_BUY=$(echo "$EURO" | cut -d' ' -f2) | |
EURO_SELL=$(echo "$EURO" | cut -d' ' -f3) | |
GOLD_CONTENT="$(wget -qO- $URL_OURO)" | |
GOLD_CURRENT="$(echo "$GOLD_CONTENT" | grep "$(date +%d/%m/%y)" -A2)" | |
while [ -z "$GOLD_CURRENT" ]; do | |
COUNT=$(($COUNT + 1)) | |
GOLD_DATE=$(date --date="$(date +%F) -$COUNT day" +%d/%m/%y) | |
GOLD_CURRENT=$(echo "$GOLD_CONTENT" | grep "$GOLD_DATE" -A2) | |
done | |
if [ -z "$GOLD_DATE" ]; then | |
GOLD_DATE=$(date +%x) | |
else | |
GOLD_DATE=$(echo "$GOLD_DATE" | sed -r 's/(.{6})/\120/') | |
fi | |
GOLD_clear=$(echo "$GOLD_CURRENT" | tail -2 | sed -e 's/ //g' -e 's/<.*\">//g' -e 's/<.*//g') | |
GOLDg=$(echo "$GOLD_clear" | head -1) | |
GOLDoz=$(echo "$GOLD_clear" | tail -1) | |
# Current values | |
btc_section=$(wget -qO- "$URL_BTC" | rg -A21 'Atualizado' | tr -s ' ' | tr '\n' ' ') | |
btc=$(echo $btc_section | sed -r -e 's/.*<div class="value">([^div]*)<\/div>.*/\1/g' -e 's/[^[:digit:],\.]//g') | |
usd=$(wget -qO- "$URL_DOLAR_REALTIME" | grep "1.00 US Dollar =" \ | |
| sed -re "s/.*1.00 US Dollar =(.{70}).*/\1/g" -e 's/<[^>]*>?//g' -e 's/\./,/') | |
eur=$(wget -qO- "$URL_EURO_REALTIME" | grep "1.00 Euro =" \ | |
| sed -re "s/.*1.00 Euro =(.{70}).*/\1/g" -e 's/<[^>]*>?//g' -e 's/\./,/') | |
echo "Valores históricos | |
================== | |
Dólar comercial¹ | Euro¹ | Ouro² | |
(${USD_DATE}) | (${EURO_DATE}) | (${GOLD_DATE}) | |
Compra R$ ${USD_BUY} | Compra R$ ${EURO_BUY} | R$ ${GOLDg}/g | |
Venda R$ ${USD_SELL} | Venda R$ ${EURO_SELL} | R$ ${GOLDoz},00/oz | |
Valores correntes | |
================= | |
1,00 USD³ = R$ ${usd} | |
1,00 EUR³ = R$ ${eur} | |
1,00 BTC⁴ = R$ ${btc} | |
Fontes: | |
¹ yahii.com.br/Indices.html | |
² pt.bullion-rates.com/gold/BRL-history.htm | |
³ xe.com | |
⁴ infomoney.com.br/cotacoes/cripto/ativo/bitcoin-btc" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment