Skip to content

Commit

Permalink
Fix currency conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhball committed Nov 18, 2023
1 parent 4187c29 commit b63b8d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
16 changes: 14 additions & 2 deletions discogs_alert/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import time

import click
Expand Down Expand Up @@ -38,6 +39,14 @@
not_required_if="list-id",
help="path to your wantlist json file (including filename)",
)
@click.option(
"-ct",
"--currency-token",
required=True,
type=str,
envvar="DA_CURRENCY_TOKEN",
help="A token for the currency conversion API (api.exchangerate.host)",
)
@click.option(
"-ua",
"--user-agent",
Expand Down Expand Up @@ -192,6 +201,7 @@ def main(
discogs_token,
list_id,
wantlist_path,
currency_token,
user_agent,
frequency,
country,
Expand All @@ -209,9 +219,11 @@ def main(
verbose,
test,
):
"""This loop queries in your watchlist at regular intervals, sending alerts if a release satisfying
your criteria is found.
"""
This loop queries your watchlist at regular intervals, alerting you if a release satisfying your criteria is found.
"""

os.environ["DA_CURRENCY_TOKEN"] = currency_token

# if both a list ID and a local wantlist path are provided, use the wantlist (to force-enable local testing)
# TODO: combine them?
Expand Down
16 changes: 11 additions & 5 deletions discogs_alert/util/currency.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from typing import Dict, Union

import requests
Expand All @@ -12,10 +13,9 @@ class InvalidCurrencyException(Exception):
...


@time_cache(seconds=3600)
@time_cache(seconds=86400)
def get_currency_rates(base_currency: str) -> CurrencyRates:
"""Get live currency exchange rates (from one base currency). Cached for one hour at a time,
per currency.
"""Get live currency exchange rates (from one base currency). Cached for one day at a time, per currency.
Args:
base_currency: one of the 3-character currency identifiers from above.
Expand All @@ -25,7 +25,13 @@ def get_currency_rates(base_currency: str) -> CurrencyRates:

if base_currency not in CURRENCY_CHOICES:
raise InvalidCurrencyException(f"{base_currency} is not a supported currency (see `discogs_alert/types.py`).")
return requests.get(f"https://api.exchangerate.host/latest?base={base_currency}").json().get("rates")

access_key = os.getenv["DA_CURRENCY_TOKEN"]
return (
requests.get(f"http://api.exchangerate.host/live?access_key={access_key}&source={base_currency}")
.json()
.get("quotes")
)


def convert_currency(value: float, old_currency: str, new_currency: str) -> float:
Expand All @@ -40,6 +46,6 @@ def convert_currency(value: float, old_currency: str, new_currency: str) -> floa
"""

try:
return float(value) / get_currency_rates(new_currency)[old_currency]
return float(value) / get_currency_rates(new_currency)[f"{new_currency}{old_currency}"]
except KeyError:
raise InvalidCurrencyException(f"{old_currency} is not a supported currency (see `discogs_alert/types.py`)")

0 comments on commit b63b8d0

Please sign in to comment.