Example #1
0
    def __init__(self, *args, **kwargs):
        super(HxatLocust, self).__init__(*args, **kwargs)

        # log to stdout
        self.console = Console()

        # default is verbose=True
        self.verbose = os.environ.get("HXAT_LOCUST_VERBOSE", "true").lower() == "true"

        # settings for this hxat user instance
        random_user = os.environ.get("FORCE_SINGLE_USER", "false").lower() == "false"
        self.hxat_client = get_hxat_client(cat="from_env", random_user=random_user)

        # TODO: this dumps lots of message for each http request
        self.ssl_verify = (
            os.environ.get("HXAT_LOCUST_SSL_VERIFY", "false").lower() == "true"
        )

        # check if should use ssl (for ws)
        self.use_ssl = self.host.startswith("https")

        # do not create ws-client until a successful lti-launch
        self.ws_client = None

        for attr, value in vars(self.hxat_client).items():
            self.console.write("----- {} = {} ".format(attr, value))
        for attr, value in vars(self).items():
            self.console.write("***** {} = {}".format(attr, value))
Example #2
0
    def __init__(
        self,
        host,
        hxat_utm_source,
        hxat_resource_link_id,
        app_url_path="",
        timeout=2,
        verbose=False,
        use_ssl=True,
    ):
        self.console = Console()
        self.host = host
        self.hxat_utm_source = hxat_utm_source
        self.hxat_resource_link_id = hxat_resource_link_id
        self.ws_timeout = timeout
        self.verbose = verbose
        self.session_id = uuid4().hex
        self.protocol = "wss" if use_ssl else "ws"

        h = urlparse(self.host)
        self.hostname = h.netloc

        self.app_url_path = app_url_path

        self.ws = None
        self.thread = None

        events.quitting += self.on_close
import asyncio
from threading import Thread
from datetime import datetime, timedelta
import os.path
import json

import discord
from discord.errors import LoginFailure
from discord.utils import get as discord_fetch

from utils import Console, cfg, app_scheduler, SuccessEmbed, ErrorEmbed, InfoEmbed

loop = asyncio.new_event_loop()
app_scheduler.main_loop = loop
client = discord.Client(loop=loop)
SHL = Console(prefix="BundestagsBot")

CONTENT_PATH = "content" if os.path.isdir("content") else "content-default"
SHL.info(f"Using content path: {CONTENT_PATH}")

about_embed = InfoEmbed(title="Help")
with open(os.path.join("static", "info.txt"), "r", encoding="utf-8") as fh:
    about_embed.description = fh.read()


class UserStat:
    def __init__(self, user_obj: discord.Member):
        self.user_obj = user_obj
        self.count = 1

    def __str__(self):
Example #4
0
from flask import request
from flask_limiter import Limiter

from utils import cfg, Console
from .flask_app import app
from .runtime_settings import unittest_mode

SHL = Console("RateLimiter")


def determine_ip():
    if request.headers.get("X-Forwarded-For",
                           request.remote_addr) == cfg.get("own_ip"):
        return request.headers.get(
            "X-Auth-For",
            request.headers.get("X-Forwarded-For", request.remote_addr))
    return request.headers.get("X-Forwarded-For", request.remote_addr)


defaults = ["10000000 per day", "500000 per hour"
            ] if unittest_mode else ["10000 per day", "500 per hour"]

limiter = Limiter(
    app,
    key_func=lambda: determine_ip,
    default_limits=["10000 per day", "500 per hour"],
)

SHL.info(f"Registered flask limiter.")
Example #5
0
import pkgutil
import importlib
import traceback

from flask_socketio import emit

from app import debug_mode
from app.obj import SystemMessenger, User, Command
from utils import Console, red, white

SHL = Console("CommandLoader")
"""

init script for the commands module
when adding a new command please specify your settings in a dictionary as in every other command file 

mandatory:
    invoke: the invoke of your command as a string
        example: 'ping'

optional:
    log: a boolean that specifies if your command will log to the console when used
        default: True
    system_display_name: default display_name for the system user sending your messages
        default: None (gets replaced with "System" if None)
"""

commands = {}
systems = {}

Example #6
0
import os as __os

from flask import Flask as __Flask

from utils import Console, blue2, white

SHL = Console("FlaskApp")

SHL.info(f"Creating FlaskApp.")

app = __Flask(__name__)
app.config['SECRET_KEY'] = '1234567890!"§$%&/()=?'
app.config['JSON_SORT_KEYS'] = False
app.config['UPLOAD_FOLDER'] = __os.path.join("", "storage", "uploads")
app.config['MAX_CONTENT_LENGTH'] = 3.5 * 1024 * 1024  # 3.5 Mb limit
Example #7
0
import time
import threading
import queue
import math

import ccxt
import telegram.ext as telegram

from utils import load_config, Console, Telegram


DEBUG = False
EXCHANGE_TIMEOUT = 1

config = load_config()
console = Console(log_file_name=config.log_file_path)
telebot = Telegram(config=config.telegram)
trader_commands = queue.Queue()


def console_log_order(comment, order, color=None):
    msg = '{:20}: {}'.format(
        str(comment)[:20],
        '[DATE: {:20} PAIR: {:10} SIDE: {:5} AMNT: {:15.8f} PRICE: {:15.8f}]'.format(
            str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(order['timestamp'] / 1000)))[:20],
            str(order['symbol'])[:10],
            str(order['side'])[:5],
            order['amount'], order['price'])
    )
    console.log(msg=msg, color=color, n=True)
Example #8
0
# -*- coding: utf-8 -*-
import sys
import os
import shutil
import requests
from re import compile, MULTILINE, IGNORECASE

from flask import Flask
from flask_socketio import SocketIO
from flask import redirect, request

from app.emotes import Emotes
from app.obj import user_manager, get_default_user, user_limiter, SystemMessenger, SystemMessage, chat_history
from utils import Console, white, green2, red, cfg

SHL = Console("Init")

# APP
app = Flask(__name__)
app.config['SECRET_KEY'] = '1234567890!"§$%&/()=?'
app.config['JSON_SORT_KEYS'] = False
app.config['UPLOAD_FOLDER'] = os.path.join("app", "storage", "uploads")
app.config['MAX_CONTENT_LENGTH'] = 3.5 * 1024 * 1024  # 3.5 Mb limit

# SOCKETS
socketio = SocketIO(app,
                    logger=True,
                    engineio_logger=True,
                    cors_allowed_origins="*")

# EMOTES
Example #9
0
from app.obj import SystemMessenger, User, Command, Embed
from app.obj.games.hangmangame import HangmanGame
from utils import Console

hangman_game = HangmanGame()

SHL = Console("HangmanGame")

settings = {'invoke': 'hangman', 'system_display_name': 'Hangman'}


def no_game() -> str:
    return "No Game started! Use /hangman start *word to start a game."


def main(system: SystemMessenger, author: User, cmd: Command, params: list):
    # game command should be                            /hangman start *word*
    # then it should be                                 /hangman guess *char*
    # to check game state when connection lost try     /hangman state
    embed = Embed(
        title="Hangman",
        thumbnail="https://image.flaticon.com/icons/png/512/43/43980.png",
        color="#00ff00",
        footer="Hangman V1.1")

    if not len(params):
        embed.set_text(
            "/hangman start *word* starts a game of hangman with your own word."
        )
        system.send(embed)
        return
Example #10
0
import subprocess

from app.obj import SystemMessenger, User, Command, Embed
from utils import Console, white, red, yellow

SHL = Console("Command gitversion")

settings = {
    'invoke': 'gitversion',
    'system_display_name': 'System - gitversion'
}

# get Version
try:
    git_version_short_hash = subprocess.check_output(
        ['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip()
    git_version_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'
                                                ]).decode('ascii').strip()
    git_remote_url = subprocess.check_output(
        ['git', 'config', '--get',
         'remote.origin.url']).decode('ascii').strip().replace(".git", "")

    if git_remote_url.startswith("git@"):
        __git_remote_host = git_remote_url[4:git_remote_url.index(":")]
        __git_remote_repo = git_remote_url[git_remote_url.index(":") + 1:]
        git_remote_url = "https://" + __git_remote_host + "/" + __git_remote_repo

        SHL.output(
            f"{white}Found local version: {git_version_short_hash}, Git over SSH",
            "gitversion")
Example #11
0
import sys

from utils import Console, white, cfg, red

SHL = Console("SettingsInit")

start_args = [x.strip().lower() for x in sys.argv]

login_disabled = cfg.get("logindisabled", False)  # default from cfg
if "-disablelogin" in start_args:  # overwrite by parameter
    login_disabled = True

if login_disabled:
    SHL.info(f"Disabled authentication.")

debug_mode = cfg.get("debug_enabled", False)  # default from cfg
if "-debug" in start_args:  # overwrite by parameter
    debug_mode = True

if debug_mode:
    SHL.info(f"Enabled debug_mode.")

unittest_mode = False
if "-unittest" in start_args:
    SHL.info(f"Enabled unittest mode.")
    unittest_mode = True
    debug_mode = False
    cfg.load_unittest_config()

production_mode = False
if "-prod" in start_args:
Example #12
0
import dateutil.parser

from utils import Console
from .errors import CreationError, DataMissingError, InvalidValueError, InvalidInputTypeError
from .sql_types import SQLTodo

SHL = Console("Todo")


def return_raw(raw):
    return raw


def get_value_and_parse(raw,
                        key_attribute: str,
                        parse_method,
                        optional: bool = True,
                        default=None):
    if isinstance(raw, dict):
        try:
            value = raw[key_attribute]
        except KeyError:
            value = default
            if not optional:
                SHL.error(
                    f"Failed creating Todo. Mandatory key '{key_attribute}' missing."
                )
                raise DataMissingError(missing_key=key_attribute)
    else:
        try:
            value = raw.__getattribute__(key_attribute)
Example #13
0
import sys
import logging

from flask import Flask

from utils import Console

SHL = Console("Startup")

if "-log" in [x.strip().lower() for x in sys.argv]:
    SHL.info(f"Setting loggers to info level.")
else:
    SHL.info(f"Setting loggers to error level.")
    logging.getLogger('app').setLevel(logging.ERROR)
    logging.getLogger('app.flask_app').setLevel(logging.ERROR)
    logging.getLogger('flask').setLevel(logging.ERROR)
    logging.getLogger('flask.app').setLevel(logging.ERROR)
    cli = sys.modules['flask.cli']
    cli.show_server_banner = lambda *x: None
Example #14
0
from typing import Union

from flask_socketio import emit
from flask import request

from .system_message import SystemMessage
from .embed import Embed
from . import chat_history, user_manager
from utils import Console, red, white

SHL = Console("SystemMessenger")


class SystemMessenger:
    def __init__(self,
                 display_name: str = None,
                 append_allow: bool = True,
                 save_in_history: bool = False):
        self.__display_name = display_name
        self.__append_allow = append_allow
        self.__save_in_history = save_in_history

    def send(self, message: Union[SystemMessage, str, Embed], **kwargs):
        if isinstance(message, str):
            message = SystemMessage(content=message)

        if kwargs.get("display_name", self.__display_name):
            message.change_display_name(
                kwargs.get("display_name", self.__display_name))
        if kwargs.get("append_allow", self.__append_allow) is not None:
            message.change_append_allow(
Example #15
0
from app.obj import SystemMessenger, User, Command
from utils import Console

SHL = Console("Command Ping")

settings = {'invoke': 'ping'}


def main(system: SystemMessenger, author: User, cmd: Command, params: list):
    system.send("Pong!")
import time

import schedule

from utils import Console

SHL = Console("Scheduler")


class Scheduler:
    main_loop = None

    def starter(self, f, args=None):
        self.main_loop.create_task(f(args))

    def clear_tag(self, tag):
        schedule.clear(str(tag))

    def schedule_daily(self, func, tag, args=None):
        schedule.every().day.at("01:00").do(self.starter, func,
                                            args).tag(str(tag))

    def schedule_check(self):
        SHL.info("Started scheduler.")

        while True:
            time.sleep(60)
            schedule.run_pending()


app_scheduler = Scheduler()
# -*- coding: utf-8 -*-
import time
import threading
import os.path

from utils import Console, white, green2, red, cfg

SHL = Console("Resource Manager")

root_filepath = os.path.join("app", "storage", "uploads")


class ResourceManager:
    def __init__(self, upload_dict):
        self.max_age = cfg.get('uploads_max_age', 172800)
        self.check_cooldown = cfg.get('uploads_check_cooldown', 43200)
        self.runCheck = False
        self.upload_dict = upload_dict

    def start_reloader(self):
        self.runCheck = True
        thread = threading.Thread(target=self.run, args=())
        thread.daemon = True  # Daemonize thread
        thread.start()

    def stop_reloader(self):
        self.runCheck = False

    def run(self):
        while self.runCheck:
            to_delete = []
Example #18
0
from app.obj import SystemMessenger, User, Command
from utils import Console, white, red
from app.obj import SystemMessage

SHL = Console("Command Example")

settings = {
    'invoke':
    'example',  # user would have to use /example to call main function below
    'system_display_name':
    'System - Example'  # name of the system user you can send messages with (default: "System")
}

# implement static variables here
path = ""


def main(system: SystemMessenger, author: User, cmd: Command,
         params: list):  # gets called by commandhandler
    return  # TODO: remove this for your command
    # system: object to send/broadcast messages:
    # author: user object of the user who invoked the command
    # cmd: Command(Message) object of the cmd the user sent
    # params: list of all parameters (without the invoke, split by space)
    # TODO: please try to handle all errors to give a detailed traceback to users,
    # default error message if you do not handle errors is "Something went wrong"
    if not len(params):
        msg = SystemMessage("Example answer")
        system.send(msg)
        system.send("Example answer 2."
                    )  # you can commit a string or SystemMessage obj
Example #19
0
# -*- coding: utf-8 -*-
import json
import time
import threading
import os.path

from utils import Console

SHL = Console("EmotesHandler")

filename = os.path.join("app", "emotes", "emotes.json")


class Emotes:
    def __init__(self, start):
        self.emotes = {}
        self.runCheck = start
        self.emit_status = None
        if not self.runCheck:
            SHL.output(f"Getting Emotes once!")
            SHL.output(f"Setting new emotes!")
            self.emotes = self.get_emotes()
        else:
            self.start_reloader()

    @staticmethod
    def get_emotes():
        if filename:
            with open(filename, encoding='utf-8', mode='r') as f:
                return json.load(f)
import os.path
import sys

from utils import Console, red, white

SHL = Console("Checkup")

if not os.path.exists(os.path.join("app", "public")):
    if "-unittest" in [x.strip().lower() for x in sys.argv]:  # start args
        SHL.warning(f"public folder is missing but ignored for unittest mode.")
        SHL.info(f"Creating dummy index.html for unittests.")
        os.mkdir(os.path.join("app", "public"))
        with open(os.path.join("app", "public", "index.html"), "w") as fh:
            fh.write("<body>ok<body/>")
    else:
        raise RuntimeError(f"{red}public folder is missing, use 'ng build --prod' and try again{white}")

SHL.info(f"Start_checkup passed.")
Example #21
0
from datetime import datetime, timedelta
import os.path
import json
import uuid
from typing import Optional

from werkzeug.security import generate_password_hash, check_password_hash
import pyotp

from utils import Console, red, white, cfg
from app.runtime_settings import production_mode, unittest_mode

SHL = Console("UserManager")

SHL.info(f"Initializing UserManager.")

BASE_PATH = os.path.dirname(__file__)
login_file = "login.json" if os.path.isfile(
    os.path.join(BASE_PATH, "login.json")) else "login-default.json"
if unittest_mode:
    login_file = "login-unittest.json"
LOGIN_INFO = os.path.join(BASE_PATH, login_file)
SHL.info(f"Login info in file {LOGIN_INFO}")


class __UserManager:
    def __init__(self):
        SHL.info(f"Loading login info.")
        try:
            with open(LOGIN_INFO, 'r', encoding="utf-8") as fh:
                data = json.load(fh)
Example #22
0
from typing import List, Optional
from contextlib import contextmanager

from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm.exc import NoResultFound

from .db_connector import DatabaseConnection
from app.obj import Todo, TodoList, CreationError, SQLTodo, SQLTodoList, DatabaseError
from utils import Console

SHL = Console("DatabaseInterface")


class TodoListSelectAll(object):
    def go(self, session) -> List[SQLTodoList]:
        return session.query(SQLTodoList)


class TodoListSelectByListId(object):
    def go(self, session, list_id: int) -> SQLTodoList:
        return session.query(SQLTodoList).filter(
            SQLTodoList.list_id == list_id).one()


class TodoListInsertOrUpdate(object):
    def go(self, session, obj):
        return session.merge(obj)


class TodoSelectAll(object):
Example #23
0
# -*- coding: utf-8 -*-
import sys
import os

os.system("cls" if os.name == "nt" else "clear")

from utils import Console, cfg
from app import app, socketio

SHL = Console("Start")


def run():
    port = cfg.get("port", 5000)
    start_args = [x.strip().lower() for x in sys.argv]

    if "--port" in start_args:
        try:
            port = int(sys.argv[sys.argv.index("--port") + 1])
        except IndexError:
            pass
        except ValueError:
            sys.exit(
                f'Invalid port "{sys.argv[sys.argv.index("--port") + 1]}"')
    port = os.environ.get("FLASKPORT", port)

    if "--cfg-debug" in start_args:
        cfg.reload(debug=True)

    SHL.output("Starting up.")
    SHL.output(f"Using port: {port}")
Example #24
0
import json

from flask import make_response, jsonify, request
from sqlalchemy.orm.exc import NoResultFound

from utils import Console
from app.authentication import token_auth
from app.flask_app import app
from app.obj import Todo, TodoList, CreationError, DatabaseError
from app.db_connector import db_interface

SHL = Console("Routes")


@app.route("/api/todo", methods=["GET", "POST", "DELETE", "PATCH"])
@token_auth.login_required
def api_todo():
    if request.method == "GET":
        if request.args.get("list_id"):
            SHL.info(f"Returning Todo by list_id", f"GET /api/todo")
            try:
                list_id = int(request.args.get("list_id"))
            except ValueError:
                SHL.warning(f"Invalid list_id provided", f"GET /api/todo")
                return make_response(jsonify({
                    "status": "failed",
                    "message": "invalid list_id"
                }), 400)
            except TypeError:
                SHL.warning(f"Invalid list_id provided", f"GET /api/todo")
                return make_response(jsonify({
Example #25
0
# -*- coding: utf-8 -*-
import requests
import re

from flask_socketio import emit
from validators import url as val_url

from app import socketio, emote_handler,  user_manager, \
    emote_regex, html_regex, newline_html_regex, link_regex, youtube_regex, image_regex, video_regex, audio_regex, \
    code_regex, quote_regex, special_image_regex, request, user_limiter, chat_history, announcer
from app import handle_command as command_handler
from app.obj import User, Command, Message, get_default_user
from utils import Console, yellow2, white, green2, cfg

SHL = Console("Socket")


@socketio.on('chat_command')
def handle_command(command):
    if user_limiter.check_cooldown(request.sid):
        SHL.output(
            f"{yellow2}Spam protection triggered {white}for SID: {request.sid}",
            "S.ON chat_command")
        return
    user_limiter.update_cooldown(request.sid)
    SHL.output(f"Received message {command}", "S.ON chat_message")

    try:
        cmd = Command(author=get_default_user(),
                      content=str(command['message']).strip(),
                      system=False)
Example #26
0
import dateutil.parser
from datetime import datetime

from utils import Console
from .errors import CreationError, DataMissingError, InvalidValueError, InvalidInputTypeError
from .sql_types import SQLTodoList

SHL = Console("TodoList")


def return_raw(raw):
    return raw


def get_value_and_parse(raw, key_attribute: str, parse_method, optional: bool = True, default=None):
    if isinstance(raw, dict):
        try:
            value = raw[key_attribute]
        except KeyError:
            value = default
            if not optional:
                SHL.error(f"Failed creating TodoList. Mandatory key '{key_attribute}' missing.")
                raise DataMissingError(missing_key=key_attribute)
    else:
        try:
            value = raw.__getattribute__(key_attribute)
        except AttributeError:
            value = default
            if not optional:
                SHL.error(f"Failed creating TodoList. Mandatory key '{key_attribute}' missing.")
                raise DataMissingError(missing_key=key_attribute)
Example #27
0
from app.obj import SystemMessenger, User, Command, Embed
from utils import Console, green2, white
import random
import json
import os.path

SHL = Console("Command quote")

settings = {'invoke': 'quote', 'system_display_name': 'Quotinator'}

if not os.path.exists(os.path.join("app", "storage", "quotes")):
    os.makedirs(os.path.join("app", "storage", "quotes"), exist_ok=True)
    SHL.output(
        f"{green2}Quotes  folder was not present, created quotes folder.{white}",
        "Quotes")

filename = os.path.join("app", "storage", "quotes", "quotes.json")
with open(filename, 'r', encoding="utf-8") as c:
    quotes = list(set(json.load(c)))


def main(system: SystemMessenger, author: User, cmd: Command, params: list):
    embed = Embed(
        title="Quote",
        thumbnail=
        "http://simpleicon.com/wp-content/uploads/right_double_quotation_mark.png",
        color="#00ff00")

    if not len(params):
        embed.set_text("/quote can be either used to register or show quotes.")
        system.send(embed)