Skip to content

Instantly share code, notes, and snippets.

@smeech
Last active June 26, 2025 12:20
Show Gist options
  • Save smeech/556e0ae86495f3342de38a021f604904 to your computer and use it in GitHub Desktop.
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
- 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")
@smeech
Copy link
Author

smeech commented May 31, 2025

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment