diff --git a/handler/agora.py b/handler/agora.py new file mode 100644 index 0000000..405e3f1 --- /dev/null +++ b/handler/agora.py @@ -0,0 +1,95 @@ +# Twisted/Klein imports +from twisted.logger import Logger + +# Other library imports +import requests +from json import loads +from forex_python.converter import CurrencyRates + +# Project imports +from settings import settings + + +class Agora(object): + """ + AgoraDesk API handler. + """ + + def __init__(self): + self.log = Logger("agora") + self.cr = CurrencyRates() + + def get_active_trades(self): + headers = {"Content-Type": "application/json", "User-Agent": "Agora-Py", "Authorization": settings.Agora.Token} + # data = {"username": "topmonero"} + 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): + headers = {"Content-Type": "application/json", "User-Agent": "Agora-Py", "Authorization": settings.Agora.Token} + r = requests.get(f"{settings.Agora.Base}/contact_messages/{contact_id}", headers=headers) + parsed = r.json() + return parsed + + def get_ads(self): + headers = {"Content-Type": "application/json", "User-Agent": "Agora-Py", "Authorization": settings.Agora.Token} + r = requests.get(f"{settings.Agora.Base}/ads", headers=headers) + parsed = r.json() + return parsed + + def create_ad(self, countrycode, currency, price): + headers = { + "Content-Type": "application/json", + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0", + "Authorization": settings.Agora.Token, + } + + data = { + "countrycode": countrycode, + "currency": currency, + "trade_type": "ONLINE_SELL", + "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, + "min_amount": price * float(settings.Agora.Margin), + # "max_amount": a, + # "limit_to_fiat_amounts": a, + "payment_method_detail": settings.Agora.PaymentMethodDetail, + # "first_time_limit_asset": a, + "require_feedback_score": settings.Agora.FeedbackScore, + "account_info": settings.Agora.PaymentDetails, + } + r = requests.post(f"{settings.Agora.WebBase}/ad-create", data=data, headers=headers) + self.log.info(r.content) + parsed = r.json() + return parsed + + def dist_countries(self): + prices = {} + rates = self.cr.get_rates("USD") + for currency, countrycode in loads(settings.Agora.DistList): + prices[currency] = rates[currency] + rtrn = self.create_ad(countrycode, currency, prices[currency]) + if not rtrn: + return False + return True diff --git a/handler/app.py b/handler/app.py index a07b1fa..d98776a 100755 --- a/handler/app.py +++ b/handler/app.py @@ -10,8 +10,8 @@ from json import dumps, loads from json.decoder import JSONDecodeError # Project imports -from settings import settings from revolut import Revolut +from agora import Agora from transactions import Transactions @@ -32,8 +32,9 @@ class WebApp(object): app = Klein() - def __init__(self): + def __init__(self, agora): self.revolut = Revolut() + self.agora = agora self.tx = Transactions() self.log = Logger("webapp") @@ -63,18 +64,44 @@ class WebApp(object): else: return dumps(convert(rtrn)) + @app.route("/trades") + def trades(self, request): + trade_list = self.agora.get_active_trades() + return dumps(trade_list) -def start(handler): + @app.route("/messages/") + def messages(self, request, contact_id): + message_list = self.agora.get_messages(contact_id) + return dumps(message_list) + + @app.route("/dist") + def dist_countries(self, request): + rtrn = self.agora.dist_countries() + return dumps(rtrn) + + @app.route("/ads") + def ads(self, request): + rtrn = self.agora.get_ads() + return dumps(rtrn) + + @app.route("/create///") + def create(self, request, countrycode, currency, price): + rtrn = self.agora.create_ad(countrycode, currency, price) + return dumps(rtrn) + + +def start(handler, refresh_sec): """ Schedule to refresh the API token once the reactor starts, and create LoopingCapp to refresh it periodically. """ deferLater(reactor, 1, handler.get_new_token) deferLater(reactor, 4, handler.setup_webhook) lc = LoopingCall(handler.get_new_token) - lc.start(settings.token_refresh_sec) + lc.start(refresh_sec) if __name__ == "__main__": - webapp = WebApp() - start(webapp.revolut) + agora = Agora() + webapp = WebApp(agora) + # start(webapp.revolut, int(settings.Revolut.RefreshSec)) webapp.app.run("127.0.0.1", 8080) diff --git a/handler/transactions.py b/handler/transactions.py index 09fb8df..892bced 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -5,6 +5,7 @@ from twisted.logger import Logger from json import dumps # Project imports +from agora import Agora from db import r @@ -15,6 +16,7 @@ class Transactions(object): def __init__(self): self.log = Logger("transactions") + self.agora = Agora() def transaction(self, data): """