2021-12-23 16:59:35 +00:00
|
|
|
#!/usr/sbin/env python3
|
|
|
|
# Twisted/Klein imports
|
2021-12-23 14:46:51 +00:00
|
|
|
from twisted.logger import Logger
|
|
|
|
from twisted.internet import reactor
|
|
|
|
from twisted.internet.task import LoopingCall, deferLater
|
|
|
|
from klein import Klein
|
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
# Other library imports
|
2021-12-23 14:46:51 +00:00
|
|
|
from json import dumps, loads
|
|
|
|
from json.decoder import JSONDecodeError
|
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
# Project imports
|
2021-12-24 17:27:36 +00:00
|
|
|
from settings import settings
|
2021-12-23 16:59:35 +00:00
|
|
|
from revolut import Revolut
|
2021-12-24 02:23:38 +00:00
|
|
|
from agora import Agora
|
2021-12-23 18:46:39 +00:00
|
|
|
from transactions import Transactions
|
2021-12-24 17:27:36 +00:00
|
|
|
from irc import bot
|
2021-12-23 14:46:51 +00:00
|
|
|
|
|
|
|
|
2021-12-23 19:09:16 +00:00
|
|
|
def convert(data):
|
|
|
|
if isinstance(data, bytes):
|
|
|
|
return data.decode("ascii")
|
|
|
|
if isinstance(data, dict):
|
|
|
|
return dict(map(convert, data.items()))
|
|
|
|
if isinstance(data, tuple):
|
|
|
|
return map(convert, data)
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
class WebApp(object):
|
|
|
|
"""
|
|
|
|
Our Klein webapp.
|
|
|
|
"""
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
app = Klein()
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2021-12-24 17:27:36 +00:00
|
|
|
def __init__(self, agora, irc):
|
|
|
|
self.revolut = Revolut(irc) # Initialise Revolut client and pass IRC
|
2021-12-24 02:23:38 +00:00
|
|
|
self.agora = agora
|
2021-12-24 17:27:36 +00:00
|
|
|
self.tx = Transactions(agora, irc) # Initialise Transactions client and pass Agora and IRC
|
2021-12-23 16:59:35 +00:00
|
|
|
self.log = Logger("webapp")
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2021-12-24 17:27:36 +00:00
|
|
|
@app.route("/newkey")
|
|
|
|
def newkey(self, request):
|
|
|
|
self.revolut.create_new_jwt()
|
|
|
|
self.revolut.get_access_token()
|
|
|
|
return dumps(True)
|
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
@app.route("/callback", methods=["POST"])
|
|
|
|
def callback(self, request):
|
|
|
|
content = request.content.read()
|
|
|
|
try:
|
|
|
|
parsed = loads(content)
|
|
|
|
except JSONDecodeError:
|
|
|
|
self.log.error("Failed to parse JSON callback: {content}", content=content)
|
2021-12-24 17:27:36 +00:00
|
|
|
return dumps(False)
|
2021-12-23 19:09:16 +00:00
|
|
|
self.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"])
|
2021-12-23 18:46:39 +00:00
|
|
|
self.tx.transaction(parsed)
|
2021-12-24 17:27:36 +00:00
|
|
|
return dumps(True)
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2021-12-23 19:09:16 +00:00
|
|
|
@app.route("/find/<string:reference>/<string:amount>")
|
|
|
|
def find(self, request, reference, amount):
|
|
|
|
try:
|
|
|
|
int(amount)
|
|
|
|
except ValueError:
|
|
|
|
return dumps({"success": False, "msg": "Amount is not an integer"})
|
|
|
|
rtrn = self.tx.find_tx(reference, amount)
|
|
|
|
if rtrn == "AMOUNT_INVALID":
|
|
|
|
return dumps({"success": False, "msg": "Reference found but amount invalid"})
|
|
|
|
elif not rtrn:
|
|
|
|
return dumps({"success": False, "msg": "Reference not found"})
|
|
|
|
else:
|
|
|
|
return dumps(convert(rtrn))
|
|
|
|
|
2021-12-24 02:23:38 +00:00
|
|
|
@app.route("/trades")
|
|
|
|
def trades(self, request):
|
2021-12-24 17:35:17 +00:00
|
|
|
trade_list = self.agora.dashboard()
|
2021-12-24 02:23:38 +00:00
|
|
|
return dumps(trade_list)
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2021-12-24 02:23:38 +00:00
|
|
|
@app.route("/messages/<string:contact_id>")
|
|
|
|
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)
|
|
|
|
|
2021-12-24 17:27:36 +00:00
|
|
|
@app.route("/create/<string:countrycode>/<string:currency>")
|
|
|
|
def create(self, request, countrycode, currency):
|
|
|
|
rtrn = self.agora.create_ad(countrycode, currency)
|
2021-12-24 02:23:38 +00:00
|
|
|
return dumps(rtrn)
|
|
|
|
|
|
|
|
|
|
|
|
def start(handler, refresh_sec):
|
2021-12-23 16:59:35 +00:00
|
|
|
"""
|
|
|
|
Schedule to refresh the API token once the reactor starts, and create LoopingCapp to refresh it periodically.
|
|
|
|
"""
|
2021-12-24 17:27:36 +00:00
|
|
|
if settings.Revolut.SetupToken == "1":
|
|
|
|
deferLater(reactor, 1, handler.setup_auth)
|
|
|
|
else:
|
2021-12-26 23:11:48 +00:00
|
|
|
deferLater(reactor, 1, handler.get_new_token, True)
|
2021-12-23 18:46:39 +00:00
|
|
|
deferLater(reactor, 4, handler.setup_webhook)
|
|
|
|
lc = LoopingCall(handler.get_new_token)
|
2021-12-24 02:23:38 +00:00
|
|
|
lc.start(refresh_sec)
|
2021-12-23 14:46:51 +00:00
|
|
|
|
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
if __name__ == "__main__":
|
2021-12-24 17:27:36 +00:00
|
|
|
irc = bot() # Initialise IRC client
|
|
|
|
agora = Agora(irc) # Initialise Agora client and pass IRC
|
2021-12-27 13:50:32 +00:00
|
|
|
irc.set_agora(agora)
|
2021-12-24 17:27:36 +00:00
|
|
|
webapp = WebApp(agora, irc) # Initialise API and pass agora and IRC
|
|
|
|
start(webapp.revolut, int(settings.Revolut.RefreshSec))
|
2021-12-23 16:59:35 +00:00
|
|
|
webapp.app.run("127.0.0.1", 8080)
|