Last active
June 26, 2025 12:20
-
-
Save smeech/556e0ae86495f3342de38a021f604904 to your computer and use it in GitHub Desktop.
[Currency conversion] Responds to e.g. ":10gbp/usd" to convert GBP to USD, can handle decimals, and any currency codes listed in the website below. #espanso #bash #python
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
- 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 |
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
- 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
- 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
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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