pluto/handler/agora.py

120 lines
4.0 KiB
Python
Raw Normal View History

2021-12-24 02:23:38 +00:00
# Twisted/Klein imports
from twisted.logger import Logger
# Other library imports
from json import loads
2021-12-24 17:26:31 +00:00
from simplejson.errors import JSONDecodeError
2021-12-24 02:23:38 +00:00
from forex_python.converter import CurrencyRates
2021-12-24 17:26:31 +00:00
from agoradesk_py.agoradesk import AgoraDesk
2021-12-24 02:23:38 +00:00
# Project imports
from settings import settings
class Agora(object):
"""
AgoraDesk API handler.
"""
2021-12-24 17:26:31 +00:00
def __init__(self, irc):
2021-12-24 02:23:38 +00:00
self.log = Logger("agora")
2021-12-24 17:26:31 +00:00
self.agora = AgoraDesk(settings.Agora.Token)
2021-12-24 02:23:38 +00:00
self.cr = CurrencyRates()
2021-12-24 17:26:31 +00:00
self.irc = irc
2021-12-24 02:23:38 +00:00
2021-12-24 17:26:31 +00:00
def try_json(self, data):
try:
parsed = data.json()
return parsed
except JSONDecodeError:
self.log.error("Cannot parse {content}", content=data.content)
return str(data.content)
2021-12-24 02:23:38 +00:00
2021-12-24 17:35:17 +00:00
def dashboard(self):
dash = self.agora.dashboard_seller()
dash_tmp = {}
if dash["response"]["data"]["contact_count"] > 0:
for contact in dash["response"]["data"]["contact_list"]:
contact_id = contact["data"]["contact_id"]
buyer = contact["data"]["buyer"]["username"]
seller = contact["data"]["seller"]["username"]
amount = contact["data"]["amount"]
amount_xmr = contact["data"]["amount_xmr"]
fee_xmr = contact["data"]["fee_xmr"]
currency = contact["data"]["currency"]
if not contact["data"]["is_selling"]:
continue
dash_tmp[contact_id] = {
"buyer": buyer,
"seller": seller,
"amount": amount,
"amount_xmr": amount_xmr,
"fee_xmr": fee_xmr,
"currency": currency,
}
return dash_tmp
2021-12-24 02:23:38 +00:00
def get_messages(self, contact_id):
messages = self.agora.contact_messages(contact_id)
messages_tmp = []
for message in messages["response"]["data"]["message_list"]:
print("MESSAGE", message["sender"]["username"], message["msg"])
messages_tmp.append(f"({message['sender']['username']}): {message['msg']}")
return messages_tmp
def get_all_messages(self):
dash = self.dashboard()
messages_tmp = {}
for contact_id in dash:
messages = self.get_messages(contact_id)
messages_tmp[contact_id] = messages
return messages_tmp
2021-12-24 02:23:38 +00:00
def get_ads(self):
ads = self.agora.ads()
return ads
2021-12-24 02:23:38 +00:00
2021-12-24 17:26:31 +00:00
def get_rates_all(self):
rates = self.cr.get_rates("USD")
return rates
2021-12-24 02:23:38 +00:00
2021-12-24 17:26:31 +00:00
def create_ad(self, countrycode, currency):
rates = self.get_rates_all()
if currency == "USD":
min_amount = float(settings.Agora.MinUSD)
max_amount = float(settings.Agora.MaxUSD)
else:
min_amount = rates[currency] * float(settings.Agora.MinUSD)
max_amount = rates[currency] * float(settings.Agora.MaxUSD)
2021-12-26 23:12:02 +00:00
price_formula = f"coingeckoxmrusd*usd{currency.lower()}*{settings.Agora.Margin}"
# price_formula = f"coingeckoxmrusd*{settings.Agora.Margin}"
2021-12-27 13:50:32 +00:00
ad = settings.Agora.Ad
ad = ad.replace("\\t", "\t")
ad = self.agora.ad_create(
country_code=countrycode,
currency=currency,
trade_type="ONLINE_SELL",
asset="XMR",
price_equation=price_formula,
track_max_amount=False,
require_trusted_by_advertiser=False,
# verified_email_required = False,
online_provider="REVOLUT",
msg=settings.Agora.Ad,
min_amount=min_amount,
max_amount=max_amount,
payment_method_details=settings.Agora.PaymentMethodDetails,
# require_feedback_score = 0,
account_info=settings.Agora.PaymentDetails,
)
return ad
2021-12-24 02:23:38 +00:00
def dist_countries(self):
for currency, countrycode in loads(settings.Agora.DistList):
2021-12-24 17:26:31 +00:00
rtrn = self.create_ad(countrycode, currency)
2021-12-24 02:23:38 +00:00
if not rtrn:
return False
return True