alexprengere / currencyconverter

A Python currency converter using the European Central Bank data.
http://alexprengere.github.io/currencyconverter
Apache License 2.0
215 stars 59 forks source link

Doe the rates auto update? #18

Closed KolbyML closed 5 years ago

KolbyML commented 5 years ago

Doe the rates auto update?

alexprengere commented 5 years ago

Hello, As you can probably guess from the commits, I add the new rates in the sources about twice a week, so people cloning from GitHub have fresh rates. PyPI is updated much less often.

Nevertheless, you can get the latest rates by changing the CurrencyConverter initialization (also works with a local file):

>>> from currency_converter import CurrencyConverter
>>> c = CurrencyConverter('http://www.ecb.int/stats/eurofxref/eurofxref-hist.zip')
Qwerty-Space commented 4 years ago

Oh that's unfortunate.

Do you think you could add a function to check for the latest rates?

alexprengere commented 4 years ago

I am not sure I understand. Basically by using the piece of code above, you ensure that you are using the latest available rates. Adding a function to check for rates would just download the data again, so using this way of creating the CurrencyConverter is what you need, I think.

Qwerty-Space commented 4 years ago

I just feel like it should be built into the library instead of updating the GitHub repo every time

alexprengere commented 4 years ago

But you don't need to update the GitHub repo, as I explained in my previous comment. What you need to do is to create the CurrencyConverter object this way:

from currency_converter import CurrencyConverter
c = CurrencyConverter('http://www.ecb.int/stats/eurofxref/eurofxref-hist.zip')

Then it will download the latest available rates from the url. Adding some sort of method like c.update_rates() would just be implemented by re-doing the __init__ with a different argument:

c = CurrencyConverter()  # created with the embedded data from the repo
c.__init__('http://www.ecb.int/stats/eurofxref/eurofxref-hist.zip')
# rates have been updated

If you find this ugly, you can add your own method either by subclassing or just:

URL = 'http://www.ecb.int/stats/eurofxref/eurofxref-hist.zip'
CurrencyConverter.update_rates = lambda self: self.__init__(URL)

c = CurrencyConverter()
c.update_rates()  # rates have been updated
Qwerty-Space commented 3 years ago

Sorry to yet again awaken an old thread. But if I do c = CurrencyConverter('...'), can I later do c.__init__('...') to update, or will it keep using the one it downloaded on creation of the object?

alexprengere commented 3 years ago

It will download it again.