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")
@blam6
Copy link

blam6 commented May 30, 2025

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

@smeech
Copy link
Author

smeech commented May 30, 2025

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

@blam6
Copy link

blam6 commented May 31, 2025

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

@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