pluto/handler/agora.py

108 lines
3.8 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
2021-12-24 17:26:31 +00:00
from json import dumps, loads
from simplejson.errors import JSONDecodeError
2021-12-24 02:23:38 +00:00
import requests
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:26:31 +00:00
def get_active_trades(self):
rtrn = self.agora.dashboard()
return rtrn
2021-12-24 02:23:38 +00:00
def get_messages(self, contact_id):
2021-12-24 17:26:31 +00:00
headers = {"Authorization": settings.Agora.Token, "Content-Type": "application/json", "User-Agent": "Agora-Py"}
2021-12-24 02:23:38 +00:00
r = requests.get(f"{settings.Agora.Base}/contact_messages/{contact_id}", headers=headers)
2021-12-24 17:26:31 +00:00
parsed = self.try_json(r)
2021-12-24 02:23:38 +00:00
return parsed
def get_ads(self):
2021-12-24 17:26:31 +00:00
headers = {"Authorization": settings.Agora.Token, "Content-Type": "application/json", "User-Agent": "Agora-Py"}
2021-12-24 02:23:38 +00:00
r = requests.get(f"{settings.Agora.Base}/ads", headers=headers)
2021-12-24 17:26:31 +00:00
parsed = self.try_json(r)
2021-12-24 02:23:38 +00:00
return parsed
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):
headers = {"Authorization": settings.Agora.Token, "Content-Type": "application/json"}
print(dumps(headers))
# data = {
# "price_equation": f"coingeckoxmrusd*{settings.Agora.Margin}",
# "countrycode": countrycode,
# "currency": currency,
# "msg": settings.Agora.Ad,
# "track_max_amount": "false",
# "online_provider": "REVOLUT",
# "trade_type": "ONLINE_SELL",
# "min_amount": price * float(settings.Agora.Margin),
# # "max_amount": maxAmount,
# "asset": "XMR",
# "payment_method_detail": settings.Agora.PaymentMethodDetail,
# "require_feedback_score": 0,
# "account_info": settings.Agora.PaymentDetails,
# }
priceformula = f"coingeckoxmrusdusd{currency.lower()}*{settings.Agora.Margin}"
rates = self.get_rates_all()
minamount = rates[currency] * float(settings.Agora.MinUSD)
maxamount = rates[currency] * float(settings.Agora.MaxUSD)
2021-12-24 02:23:38 +00:00
data = {
2021-12-24 17:26:31 +00:00
"price_equation": priceformula,
2021-12-24 02:23:38 +00:00
"countrycode": countrycode,
"currency": currency,
2021-12-24 17:26:31 +00:00
# "account_info": "My WeChat ID is XXXXX.",
"msg": settings.Agora.Ad,
# "account_info": settings.Agora.PaymentDetails,
"track_max_amount": 0,
"online_provider": "REVOLUT",
2021-12-24 02:23:38 +00:00
"trade_type": "ONLINE_SELL",
2021-12-24 17:26:31 +00:00
"min_amount": minamount,
"max_amount": maxamount,
2021-12-24 02:23:38 +00:00
"asset": "XMR",
2021-12-24 17:26:31 +00:00
"payment_method_details": settings.Agora.PaymentMethodDetails,
# "firsttimelimit_xmr": "0.5",
# "require_feedback_score": 0,
"require_trusted_by_advertiser": 0,
"verified_email_required": 0,
2021-12-24 02:23:38 +00:00
}
2021-12-24 17:26:31 +00:00
print(dumps(data))
r = requests.post(f"{settings.Agora.Base}/ad-create", json=dumps(data), headers=headers)
# self.log.info(str(r.content))
parsed = self.try_json(r)
2021-12-24 02:23:38 +00:00
return parsed
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