Begin implementing Agora ad calls

This commit is contained in:
Mark Veidemanis 2021-12-24 17:26:31 +00:00
parent ad1fb040c4
commit ff91b0ac81
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 65 additions and 53 deletions

View File

@ -2,9 +2,11 @@
from twisted.logger import Logger from twisted.logger import Logger
# Other library imports # Other library imports
from json import dumps, loads
from simplejson.errors import JSONDecodeError
import requests import requests
from json import loads
from forex_python.converter import CurrencyRates from forex_python.converter import CurrencyRates
from agoradesk_py.agoradesk import AgoraDesk
# Project imports # Project imports
from settings import settings from settings import settings
@ -15,81 +17,91 @@ class Agora(object):
AgoraDesk API handler. AgoraDesk API handler.
""" """
def __init__(self): def __init__(self, irc):
self.log = Logger("agora") self.log = Logger("agora")
self.agora = AgoraDesk(settings.Agora.Token)
self.cr = CurrencyRates() self.cr = CurrencyRates()
self.irc = irc
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)
def get_active_trades(self): def get_active_trades(self):
headers = {"Content-Type": "application/json", "User-Agent": "Agora-Py", "Authorization": settings.Agora.Token} rtrn = self.agora.dashboard()
# data = {"username": "topmonero"} return rtrn
r = requests.get(f"{settings.Agora.Base}/dashboard", headers=headers)
parsed = r.json()["data"]
count = parsed["contact_count"]
if not count:
return False
# for contact in parsed["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
return parsed
def get_messages(self, contact_id): def get_messages(self, contact_id):
headers = {"Content-Type": "application/json", "User-Agent": "Agora-Py", "Authorization": settings.Agora.Token} headers = {"Authorization": settings.Agora.Token, "Content-Type": "application/json", "User-Agent": "Agora-Py"}
r = requests.get(f"{settings.Agora.Base}/contact_messages/{contact_id}", headers=headers) r = requests.get(f"{settings.Agora.Base}/contact_messages/{contact_id}", headers=headers)
parsed = r.json() parsed = self.try_json(r)
return parsed return parsed
def get_ads(self): def get_ads(self):
headers = {"Content-Type": "application/json", "User-Agent": "Agora-Py", "Authorization": settings.Agora.Token} headers = {"Authorization": settings.Agora.Token, "Content-Type": "application/json", "User-Agent": "Agora-Py"}
r = requests.get(f"{settings.Agora.Base}/ads", headers=headers) r = requests.get(f"{settings.Agora.Base}/ads", headers=headers)
parsed = r.json() parsed = self.try_json(r)
return parsed return parsed
def create_ad(self, countrycode, currency, price): def get_rates_all(self):
headers = { rates = self.cr.get_rates("USD")
"Content-Type": "application/json", return rates
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0",
"Authorization": settings.Agora.Token,
}
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)
data = { data = {
"price_equation": priceformula,
"countrycode": countrycode, "countrycode": countrycode,
"currency": currency, "currency": currency,
"trade_type": "ONLINE_SELL", # "account_info": "My WeChat ID is XXXXX.",
"asset": "XMR",
"price_equation": f"coingeckoxmrusd*{settings.Agora.Margin}",
"track_max_amount": False,
"require_trusted_by_advertiser": False,
"online_provider": "REVOLUT",
"verified_email_required": False,
"msg": settings.Agora.Ad, "msg": settings.Agora.Ad,
"min_amount": price * float(settings.Agora.Margin), # "account_info": settings.Agora.PaymentDetails,
# "max_amount": a, "track_max_amount": 0,
# "limit_to_fiat_amounts": a, "online_provider": "REVOLUT",
"payment_method_detail": settings.Agora.PaymentMethodDetail, "trade_type": "ONLINE_SELL",
# "first_time_limit_asset": a, "min_amount": minamount,
"require_feedback_score": settings.Agora.FeedbackScore, "max_amount": maxamount,
"account_info": settings.Agora.PaymentDetails, "asset": "XMR",
"payment_method_details": settings.Agora.PaymentMethodDetails,
# "firsttimelimit_xmr": "0.5",
# "require_feedback_score": 0,
"require_trusted_by_advertiser": 0,
"verified_email_required": 0,
} }
r = requests.post(f"{settings.Agora.WebBase}/ad-create", data=data, headers=headers) print(dumps(data))
self.log.info(r.content) r = requests.post(f"{settings.Agora.Base}/ad-create", json=dumps(data), headers=headers)
parsed = r.json() # self.log.info(str(r.content))
parsed = self.try_json(r)
return parsed return parsed
def dist_countries(self): def dist_countries(self):
prices = {}
rates = self.cr.get_rates("USD")
for currency, countrycode in loads(settings.Agora.DistList): for currency, countrycode in loads(settings.Agora.DistList):
prices[currency] = rates[currency] rtrn = self.create_ad(countrycode, currency)
rtrn = self.create_ad(countrycode, currency, prices[currency])
if not rtrn: if not rtrn:
return False return False
return True return True