2022-02-22 19:53:22 +00:00
|
|
|
# Other library imports
|
2022-03-05 21:52:31 +00:00
|
|
|
import logging
|
2022-07-15 10:09:54 +00:00
|
|
|
from datetime import datetime, timezone
|
2022-03-05 21:52:31 +00:00
|
|
|
|
2022-07-15 10:09:54 +00:00
|
|
|
from httpx import ReadError, ReadTimeout, RemoteProtocolError
|
2023-02-09 07:20:00 +00:00
|
|
|
|
2022-04-15 14:12:31 +00:00
|
|
|
# Project imports
|
|
|
|
from settings import settings
|
|
|
|
|
2022-03-05 21:52:31 +00:00
|
|
|
log = logging.getLogger("util")
|
|
|
|
|
2022-05-02 15:32:33 +00:00
|
|
|
debug = False
|
2022-03-05 21:52:31 +00:00
|
|
|
|
2022-03-06 12:10:02 +00:00
|
|
|
# Color definitions
|
2022-03-05 21:52:31 +00:00
|
|
|
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
|
2022-04-12 21:06:28 +00:00
|
|
|
COLORS = {
|
|
|
|
"WARNING": YELLOW,
|
|
|
|
"INFO": WHITE,
|
|
|
|
"DEBUG": BLUE,
|
|
|
|
"CRITICAL": YELLOW,
|
|
|
|
"ERROR": RED,
|
|
|
|
}
|
2022-03-05 21:52:31 +00:00
|
|
|
RESET_SEQ = "\033[0m"
|
|
|
|
COLOR_SEQ = "\033[1;%dm"
|
|
|
|
BOLD_SEQ = "\033[1m"
|
|
|
|
|
|
|
|
|
|
|
|
def formatter_message(message, use_color=True):
|
|
|
|
if use_color:
|
|
|
|
message = message.replace("$RESET", RESET_SEQ).replace("$BOLD", BOLD_SEQ)
|
|
|
|
else:
|
|
|
|
message = message.replace("$RESET", "").replace("$BOLD", "")
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
|
|
|
class ColoredFormatter(logging.Formatter):
|
|
|
|
def __init__(self, msg, use_color=True):
|
|
|
|
logging.Formatter.__init__(self, msg)
|
|
|
|
self.use_color = use_color
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
levelname = record.levelname
|
|
|
|
if self.use_color and levelname in COLORS:
|
2022-07-15 10:09:54 +00:00
|
|
|
levelname_color = (
|
|
|
|
COLOR_SEQ % (30 + COLORS[levelname]) + levelname + RESET_SEQ
|
|
|
|
)
|
2022-03-05 21:52:31 +00:00
|
|
|
record.levelname = levelname_color
|
|
|
|
return logging.Formatter.format(self, record)
|
|
|
|
|
|
|
|
|
|
|
|
def get_logger(name):
|
|
|
|
# Define the logging format
|
2022-04-04 18:17:57 +00:00
|
|
|
FORMAT = "%(asctime)s %(levelname)18s $BOLD%(name)13s$RESET - %(message)s"
|
2022-03-05 21:52:31 +00:00
|
|
|
COLOR_FORMAT = formatter_message(FORMAT, True)
|
|
|
|
color_formatter = ColoredFormatter(COLOR_FORMAT)
|
|
|
|
# formatter = logging.Formatter(
|
2022-02-22 19:53:22 +00:00
|
|
|
|
2022-03-05 21:52:31 +00:00
|
|
|
# Why is this so complicated?
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setLevel(logging.DEBUG)
|
|
|
|
# ch.setFormatter(formatter)
|
|
|
|
ch.setFormatter(color_formatter)
|
|
|
|
|
|
|
|
# Define the logger on the base class
|
|
|
|
log = logging.getLogger(name)
|
2022-05-02 15:32:33 +00:00
|
|
|
if debug:
|
|
|
|
log.setLevel(logging.DEBUG)
|
2022-03-05 21:52:31 +00:00
|
|
|
|
|
|
|
# Add the handler and stop it being silly and printing everything twice
|
|
|
|
log.addHandler(ch)
|
|
|
|
log.propagate = False
|
|
|
|
return log
|
|
|
|
|
|
|
|
|
|
|
|
class Base(object):
|
|
|
|
def __init__(self):
|
|
|
|
name = self.__class__.__name__
|
|
|
|
|
|
|
|
# Set up all the logging stuff
|
2022-03-06 10:03:52 +00:00
|
|
|
self.log = get_logger(name)
|
2022-03-05 21:52:31 +00:00
|
|
|
|
|
|
|
self.log.info("Class initialised")
|
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
|
2022-02-28 19:58:46 +00:00
|
|
|
def xmerge_attrs(init_map):
|
2022-02-28 20:18:42 +00:00
|
|
|
"""
|
|
|
|
Given a dictionary of strings and classes, set all corresponding class.<string> attributes
|
|
|
|
on each class, to every other class.
|
|
|
|
"a": A(), "b": B() -> A.b = B_instance, B.a = A_instance
|
|
|
|
:param init_map: dict of class names to classes
|
|
|
|
"""
|
2022-02-28 19:58:46 +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-28 23:58:00 +00:00
|
|
|
def convert(data):
|
|
|
|
"""
|
|
|
|
Recursively convert a dictionary.
|
|
|
|
"""
|
|
|
|
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)
|
|
|
|
if isinstance(data, list):
|
|
|
|
return list(map(convert, data))
|
|
|
|
return data
|
2022-02-22 19:53:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def last_online_recent(date):
|
|
|
|
"""
|
|
|
|
Check if the last online date was recent.
|
|
|
|
:param date: date last online
|
|
|
|
:type date: string
|
|
|
|
:return: bool indicating whether the date was recent enough
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2022-04-18 16:22:47 +00:00
|
|
|
if "+" in date:
|
|
|
|
# for LBTC
|
|
|
|
# 2022-04-16T08:53:58+00:00
|
|
|
|
date_split = date.split("+")
|
2022-05-05 20:55:17 +00:00
|
|
|
date_split[1].replace(".", "")
|
|
|
|
date_split[1].replace(":", "")
|
2022-04-18 16:22:47 +00:00
|
|
|
date = "+".join(date_split)
|
|
|
|
date_string = "%Y-%m-%dT%H:%M:%S%z"
|
2022-04-19 20:03:23 +00:00
|
|
|
now = datetime.now(timezone.utc)
|
2022-04-18 16:22:47 +00:00
|
|
|
else:
|
|
|
|
date_string = "%Y-%m-%dT%H:%M:%S.%fZ"
|
2022-04-19 20:03:23 +00:00
|
|
|
now = datetime.now()
|
2022-04-18 16:22:47 +00:00
|
|
|
date_parsed = datetime.strptime(date, date_string)
|
2022-02-22 19:53:22 +00:00
|
|
|
sec_ago_date = (now - date_parsed).total_seconds()
|
|
|
|
return sec_ago_date < 172800
|
|
|
|
|
|
|
|
|
|
|
|
def handle_exceptions(func):
|
2022-02-28 20:18:42 +00:00
|
|
|
"""
|
|
|
|
Wrapper helper to handle Agora API errors.
|
|
|
|
:param func: function to wrap
|
|
|
|
:rtype: func
|
|
|
|
:return: the wrapped function
|
|
|
|
"""
|
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
def inner_function(*args, **kwargs):
|
2022-02-28 20:18:42 +00:00
|
|
|
"""
|
|
|
|
Inner wrapper helper.
|
|
|
|
:rtype: any or bool
|
|
|
|
:return: False or the normal return
|
|
|
|
"""
|
2022-02-22 19:53:22 +00:00
|
|
|
try:
|
|
|
|
rtrn = func(*args, **kwargs)
|
|
|
|
except (ReadTimeout, ReadError, RemoteProtocolError):
|
|
|
|
return False
|
|
|
|
if isinstance(rtrn, dict):
|
|
|
|
if "success" in rtrn:
|
|
|
|
if "message" in rtrn:
|
|
|
|
if not rtrn["success"] and rtrn["message"] == "API ERROR":
|
|
|
|
if "error_code" in rtrn["response"]["error"]:
|
|
|
|
code = rtrn["response"]["error"]["error_code"]
|
|
|
|
if not code == 136:
|
2022-03-05 21:52:31 +00:00
|
|
|
log.error(f"API error: {code}")
|
2022-02-22 19:53:22 +00:00
|
|
|
return False
|
|
|
|
else:
|
2022-03-05 21:52:31 +00:00
|
|
|
log.error(f"API error: {rtrn['response']['error']}")
|
2022-02-22 19:53:22 +00:00
|
|
|
return False
|
|
|
|
return rtrn
|
|
|
|
|
|
|
|
return inner_function
|
2022-04-15 14:12:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_settings(platform):
|
|
|
|
if platform == "agora":
|
|
|
|
return settings.Agora
|
|
|
|
elif platform == "lbtc":
|
|
|
|
return settings.LocalBitcoins
|