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-27 21:12:54 +00:00
|
|
|
def __init__(self):
|
2021-12-23 16:59:35 +00:00
|
|
|
self.log = Logger("webapp")
|
2021-12-23 14:46:51 +00:00
|
|
|
|
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 16:59:35 +00:00
|
|
|
if __name__ == "__main__":
|
2021-12-27 21:12:54 +00:00
|
|
|
# Define IRC and Agora
|
|
|
|
irc = bot()
|
|
|
|
agora = Agora()
|
|
|
|
|
|
|
|
# Pass IRC to Agora and Agora to IRC
|
|
|
|
# This is to prevent recursive dependencies
|
|
|
|
agora.set_irc(irc)
|
2021-12-27 13:50:32 +00:00
|
|
|
irc.set_agora(agora)
|
2021-12-27 21:12:54 +00:00
|
|
|
|
|
|
|
# Define Revolut
|
|
|
|
revolut = Revolut()
|
2021-12-28 11:26:51 +00:00
|
|
|
# Pass IRC to Revolut and Revolut to IRC
|
2021-12-27 21:12:54 +00:00
|
|
|
revolut.set_irc(irc)
|
2021-12-28 11:26:51 +00:00
|
|
|
irc.set_revolut(revolut)
|
|
|
|
revolut.set_agora(agora)
|
2021-12-27 21:12:54 +00:00
|
|
|
|
|
|
|
# Define Transactions
|
|
|
|
tx = Transactions()
|
|
|
|
|
2021-12-28 12:50:19 +00:00
|
|
|
# Pass Agora and IRC to Transactions and Transactions to IRC
|
2021-12-27 21:12:54 +00:00
|
|
|
tx.set_agora(agora)
|
|
|
|
tx.set_irc(irc)
|
2021-12-28 12:50:19 +00:00
|
|
|
irc.set_tx(tx)
|
2021-12-27 21:12:54 +00:00
|
|
|
|
|
|
|
# Define WebApp
|
|
|
|
webapp = WebApp()
|
|
|
|
# Handle setting up JWT and request_token from an auth code
|
|
|
|
if settings.Revolut.SetupToken == "1":
|
|
|
|
deferLater(reactor, 1, revolut.setup_auth)
|
|
|
|
else:
|
|
|
|
# Schedule refreshing the access token using the refresh token
|
|
|
|
deferLater(reactor, 1, revolut.get_new_token, True)
|
|
|
|
# Check if the webhook is set up and set up if not
|
|
|
|
deferLater(reactor, 4, revolut.setup_webhook)
|
|
|
|
# Schedule repeatedly refreshing the access token
|
|
|
|
lc = LoopingCall(revolut.get_new_token)
|
|
|
|
lc.start(int(settings.Revolut.RefreshSec))
|
|
|
|
|
|
|
|
# Run the WebApp
|
2021-12-23 16:59:35 +00:00
|
|
|
webapp.app.run("127.0.0.1", 8080)
|