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") |
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
Author
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.