-
-
Save smeech/556e0ae86495f3342de38a021f604904 to your computer and use it in GitHub Desktop.
- regex: :(?P<amount>\d+(?:\.\d+)?)(?P<src>\w{3})/(?P<dst>\w{3}) | |
replace: "{{output}}" | |
vars: | |
- name: output | |
type: shell | |
params: | |
cmd: | | |
dst_uc=$(echo "{{dst}}" | tr '[:lower:]' '[:upper:]') | |
rate=$(curl -s "https://open.er-api.com/v6/latest/{{src}}" | jq -r ".rates.$dst_uc") | |
if [ "$rate" = "null" ] || [ -z "$rate" ]; then | |
echo "ERR" | |
else | |
printf "%.2f %s" "$(echo "{{amount}} * $rate" | bc -l)" "$dst_uc" | |
fi |
- regex: :(?P<amount>\d+(?:\.\d+)?)(?P<src>\w{3})/(?P<dst>\w{3}) | |
replace: "{{output}}" | |
vars: | |
- name: output | |
type: script | |
params: | |
args: | |
- python | |
- -c | |
- | | |
import requests | |
try: | |
r = requests.get(f"https://open.er-api.com/v6/latest/{{src}}", timeout=3).json() | |
rate = r["rates"].get("{{dst}}".upper()) | |
print("ERR" if rate is None else f"{float('{{amount}}') * rate:.2f} {{dst}}".upper()) | |
except: | |
print("ERR") |
Hey, thanks so much for your help. That works great - how can I set it up so the target currency is always NZD so I can just type in :100USD or :10GBP and it will output $167 and $22.55 respectively, no currency unit? I tried ask ChatGPT to do it but for some reason it couldn't, kept getting :ERR or :1ERR for 3 digit numbers...thanks
Hey, thanks so much for your help. That works great - how can I set it up so the target currency is always NZD so I can just type in :100USD or :10GBP and it will output $167 and $22.55 respectively, no currency unit? I tried ask ChatGPT to do it but for some reason it couldn't, kept getting :ERR or :1ERR for 3 digit numbers...thanks
- regex: :(?P<amount>\d+(?:\.\d+)?)(?P<src>[A-Z]{3})
replace: ${{output}}
vars:
- name: output
type: shell
params:
cmd: |
rate=$(curl -s "https://open.er-api.com/v6/latest/{{src}}" | jq -r ".rates.NZD")
if [ "$rate" = "null" ] || [ -z "$rate" ]; then
echo "ERR"
else
printf "%.2f" "$(echo "{{amount}} * $rate" | bc -l)"
fi
Thanks. I made a slight modification so it would accept lower case currency does as well:
- regex: :(?P<amount>\d+(?:\.\d+)?)(?P<src>[a-zA-Z]{3})
replace: ${{output}}
vars:
- name: output
type: shell
params:
cmd: |
src_upper=$(echo "{{src}}" | tr '[:lower:]' '[:upper:]')
rate=$(curl -s "https://open.er-api.com/v6/latest/$src_upper" | jq -r ".rates.NZD")
if [ "$rate" = "null" ] || [ -z "$rate" ]; then
echo "ERR"
else
printf "%.2f" "$(echo "{{amount}} * $rate" | bc -l)"
fi
👍
From https://www.reddit.com/r/espanso/comments/1kyg9bf/dynamic_currency_conversion/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button and https://discord.com/channels/884163483409731584/1013916990760554608/1377695822879395911.