Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix currency conversion #67

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`)")
Loading
Loading