Пример #1
0
    def __init__(self, ip, port):
        self.ip = ip
        self.port = port

        self.socket = Client()

        @self.socket.on("new_msg")
        def on_new_msg(msg):
            remote.ee.emit("new_msg", msg)

        @remote.ee.on("send_msg")
        def send_msg(msg):
            self.socket.emit("send_msg", msg)

        @remote.ee.on("login")
        def login(room, name):
            def cb(data):
                remote.ee.emit("fetch_data", data)

            self.socket.emit("login", name, callback=cb)

        @remote.ee.on("logout")
        def logout():
            self.socket.emit("logout", remote.user["name"])
            self.socket.disconnect()

        @self.socket.on("new_user")
        def on_new_user(name):
            remote.ee.emit("new_user", name)

        @self.socket.on("remove_user")
        def on_remove_user(name):
            remote.ee.emit("remove_user", name)
Пример #2
0
 def __init__(self, logger: Logger, config: Config):
     self.logger = logger
     self.config = config
     uri = f"exa+pyodbc://{self.config.ODBC_DSN}"
     self.engine = create_engine(uri)
     self.SessionMaker = sessionmaker(bind=self.engine)
     self.socketio_client = Client()
Пример #3
0
    def join_game(client: Client, user_name, game_id, game_pwd) -> List[Dict]:
        """Join an existing game.
        :param client: SocketIO client
        :param user_name: Name of the user to join the game
        :param game_id: Game ID
        :param game_pwd: Game password
        :return: The 'players' result of SioJoinGame.emit()
        """
        players: List[Dict] = []

        # Set the handler when the server responds after the player has joined the game.
        def on_game_joined(data):
            nonlocal players
            players = data['players']

        client.on(GAME_JOINED, on_game_joined)
        client.emit(JOIN_GAME, {
            'user_name': user_name,
            'game_id': game_id,
            'game_pwd': game_pwd
        })
        # Wait until on_new_joined has been called.
        while not players:
            time.sleep(1)
        return players
Пример #4
0
 def register_self(self, player):
     # to do this correctly you would need to use rooms
     self.namespace = f"/data{player}"
     self.socket = Client(logger=True)  # this is bad
     self.socket.connect(self.endpoint, namespaces=[self.namespace])
     self.socket.emit('logged_in', {'id': player})
     self.socket.on('start', self.render, namespace=self.namespace)
     self.socket.on('change', self.render_change, namespace=self.namespace)
Пример #5
0
 def __init__(self,
              logger: Logger,
              config: Config,
              uri="sqlite:///data/crypto_trading.db"):
     self.logger = logger
     self.config = config
     self.engine = create_engine(uri)
     self.SessionMaker = sessionmaker(bind=self.engine)
     self.socketio_client = Client()
Пример #6
0
 def __init__(self, uuid, url):
     self.uuid = uuid
     self.url = url
     # self.io = SocketIO(message_queue='redis://')
     self.io = Client()
     print(">>> Connecting to {}".format(url))
     self.io.connect(url)
     self.io.emit('task_start', {
         'uuid': self.uuid,
     })
Пример #7
0
 def __init__(self,
              logger: Logger,
              config: Config,
              uri="sqlite:///data/crypto_trading.db",
              isTest=False):
     self.logger = logger
     self.config = config
     self.engine = create_engine(uri)
     self.session_factory = scoped_session(sessionmaker(bind=self.engine))
     self.socketio_client = Client()
     self.isTest = isTest
Пример #8
0
    def __init__(self, logger: Logger, config: Config):
        self.logger = logger
        self.config = config
        self.socketio_client = Client()

        engine_args = {}
        if not config.DB_URI.startswith("sqlite"):
            engine_args = {"pool_size": 10, "max_overflow": 20}

        self.engine = create_engine(config.DB_URI, **engine_args)
        self.session_factory = sessionmaker(bind=self.engine)
        self.scoped_session_factory = scoped_session(self.session_factory)
Пример #9
0
 def __init__(self,
              render,
              render_change,
              endpoint='http://127.0.0.1:5000'):
     self.endpoint = endpoint
     self.render = render
     self.render_change = render_change
     # self.user = json.loads(
     #     requests.get(f"{endpoint}/register").text)['id']
     self.socket = Client(logger=False)
     self.socket.on('login', self.register_self)
     self.socket.connect(endpoint)
     self.socket.emit('login')
Пример #10
0
def test_admin_namespace_with_correct_token(server_url: str, client: socketio.Client):
    server_metric_event = "server metric"

    mock_ack = Mock()

    @client.on(server_metric_event, namespace="/admin")
    def _server_metric(data):
        assert data["name"] == "CPU_COUNT"
        assert data["value"] > 0
        mock_ack(server_metric_event)

    client.connect(f"{server_url}?token=admin", namespaces=["/admin"])
    mock_ack.assert_called_once_with(server_metric_event)
Пример #11
0
    def setup(self):
        self.publ_info = self.sio_queue[0].get()
        self.cfg = self.sio_queue[0].get()

        assert isinstance(self.publ_info, SlavePublInfo)
        self.publ_info.cfg = self.cfg
        self.pvte_info.cfg = self.cfg

        self.setup_logger()

        self.publ_info.update_load()

        # S1.1: Setup namespace.
        from waldorf.namespace.slave import Namespace
        self.logger.debug(
            'Connect to {}:{} with uid {}'.format(
                self.cfg.master_ip, self.cfg.waldorf_port,
                self.publ_info.uid))
        if self.cfg.debug >= 2:
            sio = Client(logger=self.logger)
        else:
            sio = Client()
        self.pvte_info.sio = sio
        ns = Namespace('/slave')
        ns.setup(self)
        self.pvte_info.ns = ns
        sio.register_namespace(ns)
        sio.connect('http://{}:{}'.format(
            self.cfg.master_ip, self.cfg.waldorf_port))
Пример #12
0
    def setup(self):
        # S1: Setup socketio client.
        self.publ_info = self.sio_queue[0].get()
        self.cfg = self.sio_queue[0].get()

        assert isinstance(self.publ_info, ClientPublInfo)
        self.publ_info.cfg = self.cfg
        self.pvte_info.cfg = self.cfg
        self.ignore_events = ['submit']

        # S1.1: Setup logger.
        self.setup_logger()

        # S1.2: Connect to Waldorf master.
        from waldorf.namespace.client import Namespace
        self.logger.debug(
            'Connect to {}:{} with uid {}'.format(
                self.cfg.master_ip, self.cfg.waldorf_port,
                self.publ_info.uid))
        if self.cfg.debug >= 2:
            sio = Client(logger=self.logger)
        else:
            sio = Client()
        self.pvte_info.sio = sio
        ns = Namespace('/client')
        ns.setup(self)
        self.pvte_info.ns = ns
        sio.register_namespace(ns)
        sio.connect('http://{}:{}'.format(
            self.cfg.master_ip, self.cfg.waldorf_port))

        self.result_q = [queue.Queue(), self.submit_queue[0]]
        CeleryResultThread(self).start()
Пример #13
0
    def assign_roles(client: Client) -> List[Dict[str, str]]:
        """Triggers the role assignment.
        :param client: Client that triggers the role assignment.
        :return: List of dicts with data of the players and their roles.
        """
        roles: List[Dict[str, str]] = []

        # Set the handler when the server responds after the roles have been assigned.
        def on_roles_assigned(data):
            nonlocal roles
            roles = data['players']

        client.on(ROLES_ASSIGNED, on_roles_assigned)
        client.emit(ASSIGN_ROLES, {})
        # Wait until on_roles_assigned has been called.
        while not roles:
            time.sleep(1)
        return roles
Пример #14
0
def generate_request(beds=None):
    if beds is None:
        beds = api.API.get_instance().get_all_beds_info()
    for bed in beds:
        namespace = bed['UUID'] + "_" + bed['MAC']
        print("Comenzamos a escuchar")
        sio = Client()
        max_tries = 6
        tries = 0
        flag = False
        while max_tries > tries and not flag:
            try:
                sio.connect(get_sio_connect())
                flag = True
            except:
                tries += 1
                eventlet.sleep(1)
        if flag:
            sio.emit("give_me_data", {
                "namespace": namespace,
                "bedname": "Cama 1"
            })
        else:
            raise Exception("Conexión fallada")
        if v.version < 1:
            break  # En esta situación solo se crea una request.
Пример #15
0
    def run(self, scenario: Scenario) -> Optional[FailedInteraction]:
        client: Client = Client()

        interactions: List[Interaction] = self.resolve_interactions(scenario)
        runner_namespace = SocketIORunnerClientNamespace(
            self, interactions.copy(), dict(os.environ))

        client.register_namespace(runner_namespace)

        # have to force polling or else python-socketio tries to close
        # non-existant websockets which produces warning logs.
        client.connect(self.url, transports="polling")
        result = runner_namespace.run()
        client.disconnect()

        return result
Пример #16
0
class SocketTaskLog(TaskLog):
    def __init__(self, uuid, url):
        self.uuid = uuid
        self.url = url
        # self.io = SocketIO(message_queue='redis://')
        self.io = Client()
        print(">>> Connecting to {}".format(url))
        self.io.connect(url)
        self.io.emit('task_start', {
            'uuid': self.uuid,
        })

    def emit(self, message, data):
        self.io.emit('task_update', {
            'uuid': self.uuid,
            'type': message,
            'data': data,
        })

    def done(self):
        time.sleep(1.5)  # pause to make sure buffers are flushed
        self.io.disconnect()
Пример #17
0
#!/usr/bin/env python
"""
[WHEN TO USE THIS FILE]
[INSTRUCTIONS FOR USING THIS FILE]

Project name: [MISSING]
Author: Micah Parks

This lives on the web at: [MISSING URL]
Target environment: python 3.7
"""

# Start standard library imports.
# End standard library imports.

# Start third party imports.
# End third party imports.

# Start project imports.
from socketio import Client
# End project imports.

if __name__ == '__main__':
    clientObj = Client()
    clientObj.connect('http://localhost:5000')
    # for a in range(1000):
    clientObj.emit('flarba', {'foo': 'bar'}, namespace='/narba')
Пример #18
0
    def on_disconnect(self):
        print('on_disconnect')

    def get_current_price(self):
        tickers = pyupbit.get_tickers(fiat="KRW")

        while True:
            markets = pyupbit.get_current_price(tickers)
            client.emit(
                "send_price",
                dict(tickers=tickers, markets=markets, size=len(markets)))
            time.sleep(1)


client = UpbitClientNamespace('/upbit')

sio = Client()
sio.register_namespace(client)
sio.connect('http://localhost:3000', namespaces=['/upbit'])

sio.start_background_task(client.get_current_price)

print(sio.connected)
print(sio.connection_namespaces)

try:
    sio.wait()
except KeyboardInterrupt as e:
    if sio.connected:
        sio.disconnect()
Пример #19
0
# receiver.py

from socketio import Client

socketio = Client()
socketio.connect("http://*****:*****@socketio.on("message")
def on_message(message):
    jbt = message.get("message")
    print(jbt)


socketio.wait()
Пример #20
0
class Database:
    def __init__(self, logger: Logger, config: Config, uri="sqlite:///data/crypto_trading.db"):
        self.logger = logger
        self.config = config
        self.engine = create_engine(uri)
        self.SessionMaker = sessionmaker(bind=self.engine)
        self.socketio_client = Client()

    def socketio_connect(self):
        if self.socketio_client.connected and self.socketio_client.namespaces:
            return True
        try:
            if not self.socketio_client.connected:
                self.socketio_client.connect("http://api:5123", namespaces=["/backend"])
            while not self.socketio_client.connected or not self.socketio_client.namespaces:
                time.sleep(0.1)
            return True
        except SocketIOConnectionError:
            return False

    @contextmanager
    def db_session(self):
        """
        Creates a context with an open SQLAlchemy session.
        """
        session: Session = scoped_session(self.SessionMaker)
        yield session
        session.commit()
        session.close()

    def set_coins(self, symbols: List[str]):
        session: Session

        # Add coins to the database and set them as enabled or not
        with self.db_session() as session:
            # For all the coins in the database, if the symbol no longer appears
            # in the config file, set the coin as disabled
            coins: List[Coin] = session.query(Coin).all()
            for coin in coins:
                if coin.symbol not in symbols:
                    coin.enabled = False

            # For all the symbols in the config file, add them to the database
            # if they don't exist
            for symbol in symbols:
                coin = next((coin for coin in coins if coin.symbol == symbol), None)
                if coin is None:
                    session.add(Coin(symbol))
                else:
                    coin.enabled = True

        # For all the combinations of coins in the database, add a pair to the database
        with self.db_session() as session:
            coins: List[Coin] = session.query(Coin).filter(Coin.enabled).all()
            for from_coin in coins:
                for to_coin in coins:
                    if from_coin != to_coin:
                        pair = session.query(Pair).filter(Pair.from_coin == from_coin, Pair.to_coin == to_coin).first()
                        if pair is None:
                            session.add(Pair(from_coin, to_coin))

    def get_coins(self, only_enabled=True) -> List[Coin]:
        session: Session
        with self.db_session() as session:
            if only_enabled:
                coins = session.query(Coin).filter(Coin.enabled).all()
            else:
                coins = session.query(Coin).all()
            session.expunge_all()
            return coins

    def get_coin(self, coin: Union[Coin, str]) -> Coin:
        if isinstance(coin, Coin):
            return coin
        session: Session
        with self.db_session() as session:
            coin = session.query(Coin).get(coin)
            session.expunge(coin)
            return coin

    def set_current_coin(self, coin: Union[Coin, str]):
        coin = self.get_coin(coin)
        session: Session
        with self.db_session() as session:
            if isinstance(coin, Coin):
                coin = session.merge(coin)
            cc = CurrentCoin(coin)
            session.add(cc)
            self.send_update(cc)

    def get_current_coin(self) -> Optional[Coin]:
        session: Session
        with self.db_session() as session:
            current_coin = session.query(CurrentCoin).order_by(CurrentCoin.datetime.desc()).first()
            if current_coin is None:
                return None


            coin = current_coin.coin
            session.expunge(coin)
            return coin

    def get_pair(self, from_coin: Union[Coin, str], to_coin: Union[Coin, str]):
        from_coin = self.get_coin(from_coin)
        to_coin = self.get_coin(to_coin)
        session: Session
        with self.db_session() as session:
            pair: Pair = session.query(Pair).filter(Pair.from_coin == from_coin, Pair.to_coin == to_coin).first()
            session.expunge(pair)
            return pair

    def get_pairs_from(self, from_coin: Union[Coin, str], only_enabled=True) -> List[Pair]:
        from_coin = self.get_coin(from_coin)

        session: Session
        with self.db_session() as session:
            pairs = session.query(Pair).filter(Pair.from_coin == from_coin)
            if only_enabled:
                pairs = pairs.filter(Pair.enabled.is_(True))
            pairs = pairs.all()
            session.expunge_all()
            return pairs

    def get_pairs(self, only_enabled=True) -> List[Pair]:
        session: Session
        with self.db_session() as session:
            pairs = session.query(Pair)
            if only_enabled:
                pairs = pairs.filter(Pair.enabled.is_(True))
            pairs = pairs.all()
            session.expunge_all()
            return pairs

    def log_scout(
        self,
        pair: Pair,
        target_ratio: float,
        current_coin_price: float,
        other_coin_price: float,
    ):
        session: Session
        with self.db_session() as session:
            pair = session.merge(pair)
            sh = ScoutHistory(pair, target_ratio, current_coin_price, other_coin_price)
            session.add(sh)
            session.commit()
            #self.send_update(sh)

    def prune_scout_history(self):
        time_diff = datetime.now() - timedelta(hours=self.config.SCOUT_HISTORY_PRUNE_TIME)
        session: Session
        with self.db_session() as session:
            session.query(ScoutHistory).filter(ScoutHistory.datetime < time_diff).delete()

    def prune_value_history(self):
        session: Session
        with self.db_session() as session:
            # Sets the first entry for each coin for each hour as 'hourly'
            hourly_entries: List[CoinValue] = (
                session.query(CoinValue).group_by(CoinValue.coin_id, func.strftime("%H", CoinValue.datetime)).all()
            )
            for entry in hourly_entries:
                entry.interval = Interval.HOURLY

            # Sets the first entry for each coin for each day as 'daily'
            daily_entries: List[CoinValue] = (
                session.query(CoinValue).group_by(CoinValue.coin_id, func.date(CoinValue.datetime)).all()
            )
            for entry in daily_entries:
                entry.interval = Interval.DAILY

            # Sets the first entry for each coin for each month as 'weekly'
            # (Sunday is the start of the week)
            weekly_entries: List[CoinValue] = (
                session.query(CoinValue).group_by(CoinValue.coin_id, func.strftime("%Y-%W", CoinValue.datetime)).all()
            )
            for entry in weekly_entries:
                entry.interval = Interval.WEEKLY

            # The last 24 hours worth of minutely entries will be kept, so
            # count(coins) * 1440 entries
            time_diff = datetime.now() - timedelta(hours=24)
            session.query(CoinValue).filter(
                CoinValue.interval == Interval.MINUTELY, CoinValue.datetime < time_diff
            ).delete()

            # The last 28 days worth of hourly entries will be kept, so count(coins) * 672 entries
            time_diff = datetime.now() - timedelta(days=28)
            session.query(CoinValue).filter(
                CoinValue.interval == Interval.HOURLY, CoinValue.datetime < time_diff
            ).delete()

            # The last years worth of daily entries will be kept, so count(coins) * 365 entries
            time_diff = datetime.now() - timedelta(days=365)
            session.query(CoinValue).filter(
                CoinValue.interval == Interval.DAILY, CoinValue.datetime < time_diff
            ).delete()

            # All weekly entries will be kept forever

    def create_database(self):
        Base.metadata.create_all(self.engine)

    def start_trade_log(self, from_coin: Coin, to_coin: Coin, selling: bool):
        return TradeLog(self, from_coin, to_coin, selling)

    def get_last_trade(self, previous_trade:Trade=None):
        session: Session
        with self.db_session() as session:
            last_trade = session.query(Trade).filter(
                Trade.crypto_trade_amount is not None,
                Trade.state == "COMPLETE",
                Trade.selling == "0"
            )
            if previous_trade is not None:
                last_trade = last_trade.filter(Trade.alt_coin_id == previous_trade.alt_coin_id,
                                               Trade.datetime < previous_trade.datetime)
            last_trade = last_trade.order_by(Trade.datetime.desc())
            last_trade = last_trade.first()
            session.expunge_all()
            return last_trade

    def send_update(self, model):
        if not self.socketio_connect():
            return

        self.socketio_client.emit(
            "update",
            {"table": model.__tablename__, "data": model.info()},
            namespace="/backend",
        )

    def migrate_old_state(self):
        """
        For migrating from old dotfile format to SQL db. This method should be removed in
        the future.
        """
        if os.path.isfile(".current_coin"):
            with open(".current_coin") as f:
                coin = f.read().strip()
                self.logger.info(f".current_coin file found, loading current coin {coin}")
                self.set_current_coin(coin)
            os.rename(".current_coin", ".current_coin.old")
            self.logger.info(f".current_coin renamed to .current_coin.old - You can now delete this file")

        if os.path.isfile(".current_coin_table"):
            with open(".current_coin_table") as f:
                self.logger.info(f".current_coin_table file found, loading into database")
                table: dict = json.load(f)
                session: Session
                with self.db_session() as session:
                    for from_coin, to_coin_dict in table.items():
                        for to_coin, ratio in to_coin_dict.items():
                            if from_coin == to_coin:
                                continue
                            pair = session.merge(self.get_pair(from_coin, to_coin))
                            pair.ratio = ratio
                            session.add(pair)

            os.rename(".current_coin_table", ".current_coin_table.old")
            self.logger.info(".current_coin_table renamed to .current_coin_table.old - " "You can now delete this file")
Пример #21
0
# sender.py

from socketio import Client

socketio = Client()
socketio.connect("http://localhost:5000")

while True:
    nama = input("nama : ")
    message = input("message : ")
    data = {'nama': nama, 'massage': message}
    socketio.emit("message", data)
import sys
import logging
from typing import Any
from base64 import b64encode

from socketio import Client

log = logging.getLogger(__name__)
client = Client()


@client.event
def connect():
    log.info("Connected")


@client.event
def disconnect():
    log.error("Disconnected")
    exit(1)


def simulation_update(response: Any) -> None:
    log.info(response)


def simulation_created(succeeded: bool) -> None:
    if not succeeded:
        log.error("Unable to start simulation")
        exit(1)
Пример #23
0
from socketio import Client
from time import sleep

c = Client()

c.connect('http://*****:*****@c.on('new_hand', namespace='/event')
def event(data):
    print('new_hand')
    print(data)


c.emit('new_hand',
       data={
           'user_id': 'hoge',
           'room_id': 'fuga',
           'hand': 1
       },
       namespace='/event')
Пример #24
0
import yaml
import os
import time
import random
import requests
import json
from register import controllers as configured_controllers
from devices.DefaultDevice import DefaultDevice
from helpers import setup
from queue import Queue
import threading

with open(r'config.yaml') as file:
    config = yaml.load(file, Loader=yaml.FullLoader)

sio = Client()


@sio.event(namespace='/sensor')
def connect():
    print('connection established')
    update_targets_on_server()
    sio.emit("new_data", {"data": {}}, namespace="/sensor")
    print('targets updated')


@sio.event
def disconnect():
    global connected
    connected = False
    print('disconnected from server')
Пример #25
0
Target environment: python 3.7
"""

# Start standard library imports.
from json import loads
# End standard library imports.

# Start third party imports.
from bluetooth import PORT_ANY, RFCOMM, SERIAL_PORT_CLASS, SERIAL_PORT_PROFILE, BluetoothSocket, advertise_service
from socketio import Client
# End third party imports.

# Start project imports.
# End project imports.

CLIENT_OBJ = Client()
BLUETOOTH_SOCKET_OBJ = BluetoothSocket(RFCOMM)
BLUETOOTH_SOCKET_OBJ.bind((str(), PORT_ANY))
BLUETOOTH_SOCKET_OBJ.listen(1)
PORT_INT = BLUETOOTH_SOCKET_OBJ.getsockname()[1]
UUID_STR = '94f39d29-7d6d-437d-973b-fba39e49d4ee'


def get_json_from_socket(socketObj) -> dict:
    """"
    """
    receiveBytes = socketObj.recv(1024)
    if len(receiveBytes) == 0:
        print("Received 0 bytes")
        return None
    return loads(receiveBytes.decode('utf-8'))
Пример #26
0
os.environ['DEVICE_NAME'] = sys.argv[1]
os.environ['INSTALL_DIR'] = install_dir
os.environ['DATA_DIR'] = data_dir
os.environ['TMATE_SOCK'] = os.path.abspath(data_dir + os.path.sep +
                                           'tmate.sock')
os.environ['CONFIG_FILE'] = os.path.abspath(data_dir + os.path.sep + 'config')
os.environ['EXPIRATION_TIMESTAMP'] = os.path.abspath(data_dir + os.path.sep +
                                                     'expiration-timestamp')
os.environ['USE_DOCKER'] = str(use_docker)

auth_hdr = {
    'Authorization':
    'Basic ' + b64encode((username + ':' + password).encode()).decode()
}

c = Client()

initial_connect = False
is_provisioned = False
error = 1

ssh = ''
web = ''
web_ro = ''


def run_external(args):
    def preexec():
        os.setgid(gid)
        os.setuid(uid)
        os.chdir(data_dir)
Пример #27
0
class Database:
    def __init__(self,
                 logger: Logger,
                 config: Config,
                 uri="sqlite:///data/crypto_trading.db",
                 isTest=False):
        self.logger = logger
        self.config = config
        self.engine = create_engine(uri)
        self.session_factory = scoped_session(sessionmaker(bind=self.engine))
        self.socketio_client = Client()
        self.isTest = isTest

    def socketio_connect(self):
        if self.isTest: return False
        if self.socketio_client.connected and self.socketio_client.namespaces:
            return True
        try:
            if not self.socketio_client.connected:
                self.socketio_client.connect("http://api:5123",
                                             namespaces=["/backend"])
            while not self.socketio_client.connected or not self.socketio_client.namespaces:
                time.sleep(0.1)
            return True
        except SocketIOConnectionError:
            return False

    @contextmanager
    def db_session(self):
        """
        Creates a context with an open SQLAlchemy session.
        """
        session: Session = self.session_factory()
        yield session
        session.commit()
        session.close()

    def set_coins(self, symbols: List[str]):
        session: Session

        # Add coins to the database and set them as enabled or not
        with self.db_session() as session:
            # For all the coins in the database, if the symbol no longer appears
            # in the config file, set the coin as disabled
            coins: List[Coin] = session.query(Coin).all()
            for coin in coins:
                if coin.symbol not in symbols:
                    coin.enabled = False

            # For all the symbols in the config file, add them to the database
            # if they don't exist
            for symbol in symbols:
                coin = next((coin for coin in coins if coin.symbol == symbol),
                            None)
                if coin is None:
                    session.add(Coin(symbol))
                else:
                    coin.enabled = True

        # For all the combinations of coins in the database, add a pair to the database
        with self.db_session() as session:
            coins: List[Coin] = session.query(Coin).filter(Coin.enabled).all()
            for from_coin in coins:
                for to_coin in coins:
                    if from_coin != to_coin:
                        pair = session.query(Pair).filter(
                            Pair.from_coin == from_coin,
                            Pair.to_coin == to_coin).first()
                        if pair is None:
                            session.add(Pair(from_coin, to_coin))

    def get_coins(self, only_enabled=True) -> List[Coin]:
        session: Session
        with self.db_session() as session:
            if only_enabled:
                coins = session.query(Coin).filter(Coin.enabled).all()
            else:
                coins = session.query(Coin).all()
            session.expunge_all()
            return coins

    def get_coin(self, coin: Union[Coin, str]) -> Coin:
        if isinstance(coin, Coin):
            return coin
        session: Session
        with self.db_session() as session:
            coin = session.query(Coin).get(coin)
            session.expunge(coin)
            return coin

    def set_current_coin(self, coin: Union[Coin, str]):
        coin = self.get_coin(coin)
        session: Session
        with self.db_session() as session:
            if isinstance(coin, Coin):
                coin = session.merge(coin)
            cc = CurrentCoin(coin)
            session.add(cc)
            self.send_update(cc)

    def get_current_coin(self) -> Optional[Coin]:
        session: Session
        with self.db_session() as session:
            current_coin = session.query(CurrentCoin).order_by(
                CurrentCoin.datetime.desc()).first()
            if current_coin is None:
                return None
            coin = current_coin.coin
            session.expunge(coin)
            return coin

    def get_pair(self, from_coin: Union[Coin, str], to_coin: Union[Coin, str]):
        from_coin = self.get_coin(from_coin)
        to_coin = self.get_coin(to_coin)
        session: Session
        with self.db_session() as session:
            pair: Pair = session.query(Pair).filter(
                Pair.from_coin == from_coin, Pair.to_coin == to_coin).first()
            session.expunge(pair)
            return pair

    def get_pairs_from(self,
                       from_coin: Union[Coin, str],
                       only_enabled=True) -> List[Pair]:
        from_coin = self.get_coin(from_coin)
        session: Session
        with self.db_session() as session:
            pairs = session.query(Pair).filter(Pair.from_coin == from_coin)
            if only_enabled:
                pairs = pairs.filter(Pair.enabled.is_(True))
            pairs = pairs.all()
            session.expunge_all()
            return pairs

    def get_pairs(self, only_enabled=True) -> List[Pair]:
        session: Session
        with self.db_session() as session:
            pairs = session.query(Pair)
            if only_enabled:
                pairs = pairs.filter(Pair.enabled.is_(True))
            pairs = pairs.all()
            session.expunge_all()
            return pairs

    def batch_log_scout(self, logs: List[LogScout]):
        session: Session
        with self.db_session() as session:
            dt = datetime.now()
            session.execute(
                insert(ScoutHistory),
                [{
                    "pair_id": ls.pair.id,
                    "target_ratio": ls.target_ratio,
                    "current_coin_price": ls.coin_price,
                    "other_coin_price": ls.optional_coin_price,
                    "datetime": dt,
                } for ls in logs],
            )

    def log_scout(
        self,
        pair: Pair,
        target_ratio: float,
        current_coin_price: float,
        other_coin_price: float,
    ):
        session: Session
        with self.db_session() as session:
            pair = session.merge(pair)
            sh = ScoutHistory(pair, target_ratio, current_coin_price,
                              other_coin_price)
            session.add(sh)
            self.send_update(sh)

    def prune_scout_history(self):
        time_diff = datetime.now() - timedelta(
            hours=self.config.SCOUT_HISTORY_PRUNE_TIME)
        session: Session
        with self.db_session() as session:
            session.query(ScoutHistory).filter(
                ScoutHistory.datetime < time_diff).delete()

    def prune_value_history(self):
        session: Session
        with self.db_session() as session:
            session.query(CoinValue).delete()

    def create_database(self):
        Base.metadata.create_all(self.engine)

    def start_trade_log(self, from_coin: Coin, to_coin: Coin, selling: bool):
        return TradeLog(self, from_coin, to_coin, selling)

    def send_update(self, model):
        if not self.socketio_connect():
            return

        self.socketio_client.emit(
            "update",
            {
                "table": model.__tablename__,
                "data": model.info()
            },
            namespace="/backend",
        )

    def migrate_old_state(self):
        """
        For migrating from old dotfile format to SQL db. This method should be removed in
        the future.
        """
        if os.path.isfile(".current_coin"):
            with open(".current_coin") as f:
                coin = f.read().strip()
                self.logger.info(
                    f".current_coin file found, loading current coin {coin}")
                self.set_current_coin(coin)
            os.rename(".current_coin", ".current_coin.old")
            self.logger.info(
                ".current_coin renamed to .current_coin.old - You can now delete this file"
            )

        if os.path.isfile(".current_coin_table"):
            with open(".current_coin_table") as f:
                self.logger.info(
                    ".current_coin_table file found, loading into database")
                table: dict = json.load(f)
                session: Session
                with self.db_session() as session:
                    for from_coin, to_coin_dict in table.items():
                        for to_coin, ratio in to_coin_dict.items():
                            if from_coin == to_coin:
                                continue
                            pair = session.merge(
                                self.get_pair(from_coin, to_coin))
                            pair.ratio = ratio
                            session.add(pair)

            os.rename(".current_coin_table", ".current_coin_table.old")
            self.logger.info(
                ".current_coin_table renamed to .current_coin_table.old - "
                "You can now delete this file")

    def batch_update_coin_values(self, cv_batch: List[CoinValue]):
        session: Session
        with self.db_session() as session:
            session.execute(
                insert(CoinValue),
                [{
                    "coin_id": cv.coin.symbol,
                    "balance": cv.balance,
                    "usd_price": cv.usd_price,
                    "btc_price": cv.btc_price,
                    "interval": cv.interval,
                    "datetime": cv.datetime,
                } for cv in cv_batch],
            )
Пример #28
0
from datetime import datetime, timedelta
from typing import List, Union, Optional

from socketio import Client
from socketio.exceptions import ConnectionError
from sqlalchemy import create_engine, func
from sqlalchemy.orm import sessionmaker, scoped_session, Session

from models import *

engine = create_engine("sqlite:///data/crypto_trading.db")

SessionMaker = sessionmaker(bind=engine)
SessionMaker()

socketio_client = Client()


def socketio_connect():
    if socketio_client.connected and socketio_client.namespaces:
        return True
    try:
        if not socketio_client.connected:
            socketio_client.connect('http://api:5123', namespaces=["/backend"])
        while not socketio_client.connected or not socketio_client.namespaces:
            time.sleep(0.1)
        return True
    except ConnectionError:
        return False

Пример #29
0
def test_automatic_mock_event_emission(
    server_url_fixture: str,
    mock_client_wait_timeout: float,
    mock_client_wait_interval: float,
    client: socketio.Client,
    request: pytest.FixtureRequest,
):
    server_url: str = request.getfixturevalue(server_url_fixture)
    new_message_event = "new message"
    new_message_mock_ack = Mock()

    @client.on(new_message_event)
    def _new_message_handler(data):
        jsonschema.validate(data, {
            "username": {
                "type": "string"
            },
            "message": {
                "type": "string"
            }
        })

        # Assert that message is of sentence format:
        assert data["message"].endswith(".")
        assert " " in data["message"]

        # Assert that username is a first name:
        assert data["username"].istitle()

        new_message_mock_ack(new_message_event)

    typing_event = "typing"
    typing_mock_ack = Mock()

    @client.on(typing_event)
    def _typing_handler(data):
        jsonschema.validate(data, {"username": {"type": "string"}})

        # Assert that username is a first name:
        assert data["username"].istitle()
        typing_mock_ack(typing_event)

    user_joined_event = "user joined"
    user_joined_mock_ack = Mock()

    @client.on(user_joined_event)
    def _user_joined_handler(data):
        jsonschema.validate(data, {
            "username": {
                "type": "string"
            },
            "numUsers": {
                "type": "integer"
            }
        })

        # Assert that username is a first name:
        assert data["username"].istitle()
        user_joined_mock_ack(user_joined_event)

    client.connect(server_url, wait_timeout=mock_client_wait_timeout)
    # Wait for all messages to arrive:
    client.sleep(mock_client_wait_interval)

    new_message_mock_ack.assert_called_with(new_message_event)
    typing_mock_ack.assert_called_with(typing_event)
    user_joined_mock_ack.assert_called_with(user_joined_event)
Пример #30
0
import requests
import subprocess
import os
import json
from socketio import Client as SocketClient
from bs4 import BeautifulSoup
import getFromPath

dir_path = os.path.dirname(os.path.realpath(__file__))

player = subprocess.Popen(
    [os.path.join(dir_path, 'player\\youtubecli-win32-x64\\youtubecli.exe')],
    stdout=subprocess.PIPE,
    stdin=subprocess.PIPE)
playerSocket = SocketClient()
playerSocket.connect('http://localhost:8081')

scraper = requests.Session()
scraper.headers = {
    'User-Agent':
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}

filters = {'video': 'EgIQAQ==', 'channel': 'EgIQAg==', 'playlist': 'EgIQAw=='}
commands = ['play', 'pause', 'forward', 'rewind', 'volumeUp', 'volumeDown']

BLANK_ROW = '|' + ('-' * 3) + '|' + ('-' * 94) + '|\n'


def main():
    try: