40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
|
# Twisted/Klein imports
|
||
|
from twisted.logger import Logger
|
||
|
|
||
|
# Other library imports
|
||
|
from pycoingecko import CoinGeckoAPI
|
||
|
|
||
|
|
||
|
class Money(object):
|
||
|
"""
|
||
|
Generic class for handling money-related matters that aren't Revolut or Agora.
|
||
|
"""
|
||
|
|
||
|
def __init__(self):
|
||
|
"""
|
||
|
Initialise the Money object.
|
||
|
Set the logger.
|
||
|
Initialise the CoinGecko API.
|
||
|
"""
|
||
|
self.log = Logger("money")
|
||
|
self.cg = CoinGeckoAPI()
|
||
|
|
||
|
def lookup_rates(self, ads, rates=None):
|
||
|
"""
|
||
|
Lookup the rates for a list of public ads.
|
||
|
"""
|
||
|
if not rates:
|
||
|
rates = self.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=self.markets.get_all_currencies())
|
||
|
# Set the price based on the asset
|
||
|
for ad in ads:
|
||
|
if ad[4] == "XMR":
|
||
|
coin = "monero"
|
||
|
elif ad[4] == "BTC":
|
||
|
coin = "bitcoin" # No s here
|
||
|
currency = ad[5]
|
||
|
base_currency_price = rates[coin][currency.lower()]
|
||
|
price = float(ad[2])
|
||
|
rate = round(price / base_currency_price, 2)
|
||
|
ad.append(rate)
|
||
|
return sorted(ads, key=lambda x: x[2])
|