2021-12-28 12:52:19 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-12-23 16:59:35 +00:00
|
|
|
# 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
|
2022-02-18 08:41:17 +00:00
|
|
|
from signal import signal, SIGINT
|
2021-12-23 14:46:51 +00:00
|
|
|
|
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
|
2022-01-27 19:12:15 +00:00
|
|
|
from notify import Notify
|
2022-02-07 13:24:09 +00:00
|
|
|
from markets import Markets
|
2022-02-22 20:06:56 +00:00
|
|
|
from money import Money
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2022-02-18 08:41:17 +00:00
|
|
|
init_map = None
|
|
|
|
|
|
|
|
|
2022-02-24 22:27:27 +00:00
|
|
|
def cleanup(sig, frame):
|
2022-02-18 08:41:17 +00:00
|
|
|
if init_map:
|
|
|
|
try:
|
|
|
|
init_map["tx"].lc_es_checks.stop()
|
|
|
|
init_map["agora"].lc_dash.stop()
|
|
|
|
init_map["agora"].lc_cheat.stop()
|
|
|
|
except: # noqa
|
|
|
|
pass # noqa
|
|
|
|
reactor.stop()
|
|
|
|
|
|
|
|
|
2022-02-24 22:27:27 +00:00
|
|
|
signal(SIGINT, cleanup) # Handle Ctrl-C and run the cleanup routine
|
2022-02-18 08:41:17 +00:00
|
|
|
|
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__":
|
2022-02-07 13:24:09 +00:00
|
|
|
init_map = {
|
|
|
|
"notify": Notify(),
|
|
|
|
"irc": bot(),
|
|
|
|
"agora": Agora(),
|
|
|
|
"markets": Markets(),
|
|
|
|
"revolut": Revolut(),
|
|
|
|
"tx": Transactions(),
|
|
|
|
"webapp": WebApp(),
|
2022-02-22 20:06:56 +00:00
|
|
|
"money": Money(),
|
2022-02-07 13:24:09 +00:00
|
|
|
}
|
|
|
|
for classname, object_instance in init_map.items():
|
|
|
|
# notify, Notify
|
|
|
|
for classname_inside, object_instance_inside in init_map.items():
|
|
|
|
if not classname == classname_inside:
|
|
|
|
# irc, bot
|
|
|
|
setattr(object_instance, classname_inside, object_instance_inside)
|
2021-12-27 21:12:54 +00:00
|
|
|
|
|
|
|
# Handle setting up JWT and request_token from an auth code
|
|
|
|
if settings.Revolut.SetupToken == "1":
|
2022-02-07 13:24:09 +00:00
|
|
|
deferLater(reactor, 1, init_map["revolut"].setup_auth)
|
2021-12-27 21:12:54 +00:00
|
|
|
else:
|
|
|
|
# Schedule refreshing the access token using the refresh token
|
2022-02-07 13:24:09 +00:00
|
|
|
deferLater(reactor, 1, init_map["revolut"].get_new_token, True)
|
2021-12-27 21:12:54 +00:00
|
|
|
# Check if the webhook is set up and set up if not
|
2022-02-07 13:24:09 +00:00
|
|
|
deferLater(reactor, 4, init_map["revolut"].setup_webhook)
|
2021-12-27 21:12:54 +00:00
|
|
|
# Schedule repeatedly refreshing the access token
|
2022-02-07 13:24:09 +00:00
|
|
|
lc = LoopingCall(init_map["revolut"].get_new_token)
|
2021-12-27 21:12:54 +00:00
|
|
|
lc.start(int(settings.Revolut.RefreshSec))
|
2022-02-18 08:41:17 +00:00
|
|
|
# Set up the loops to put data in ES
|
|
|
|
init_map["tx"].setup_loops()
|
2021-12-27 21:12:54 +00:00
|
|
|
|
|
|
|
# Run the WebApp
|
2022-02-07 13:24:09 +00:00
|
|
|
init_map["webapp"].app.run("127.0.0.1", 8080)
|