def edit_bot(bot_id): try: bot = Bot.objects(id=bot_id).first() except ValidationError: return '', 404 if bot is None: return '', 404 if request.method == 'GET': proxies = _get_available_proxies() return render_template('edit_bot.html', proxies=proxies, bot=bot) else: try: bot_name = request.form['name'] proxy_country_code = request.form['proxy_country_code'] proxy = request.form['proxy'] vb_primary_stake = float(request.form['percent_primary']) vb_secondary_stake = float(request.form['percent_secondary']) vb_percent_increase = float(request.form['percent_increase']) vb_percent_threshold = float(request.form['percent_threshold']) rest_time_begin = int(request.form['rest_time_begin']) rest_time_end = int(request.form['rest_time_end']) except (KeyError, ValueError): flash('Invalid request', 'danger') return redirect(url_for('bots.edit_bot', bot_id=bot_id)) if bot_name != bot.name: existing_bot_name = Bot.objects(name=bot_name).first() if existing_bot_name is not None: flash(f'Name: {bot_name} is already taken.', 'name') return redirect(url_for('bots.edit_bot', bot_id=bot_id)) # Save current bot name (for creating success message) previous_bot_name = bot.name bot.name = bot_name bot.proxy_country_code = proxy_country_code bot.proxy = proxy bot.value_bet_stake_percent_1 = vb_primary_stake bot.value_bet_stake_percent_2 = vb_secondary_stake bot.value_bet_percent_increase = vb_percent_increase bot.value_bet_percent_threshold = vb_percent_threshold bot.rest_time_begin = rest_time_begin bot.rest_time_end = rest_time_end try: bot.save() except Exception as e: flash(f'Database error: {e}', 'danger') return redirect(url_for('bots.edit_bot', bot_id=bot_id)) success_message = f'Successfully updated {bot.name}' if previous_bot_name != bot_name: success_message += f' (previously: {previous_bot_name})' success_message += '!' flash(success_message, 'success') return redirect(url_for('bots.bot_overview'))
def manage_bot(bot_id): """Enable or disable bot""" try: enable = request.json['enable'] except KeyError: return response_error('Invalid request', 400) if not isinstance(enable, bool): return response_error('Invalid request', 400) try: bot = Bot.objects(id=bot_id).first() except ValidationError: return '', 404 if bot is None: return '', 404 bot.is_enabled = enable try: bot.save() except Exception as e: return response_error(f'Database error: {e}', 500) return response_success()
def get_bot_wallet_balance(bot_id): try: bot_db = Bot.objects(id=bot_id).first() except ValidationError: return '', 404 if bot_db is None: return '', 404 if bot_db.is_enabled: session_data = current_app.redis_manager.get_bot_session_data(bot_id) else: session_data = None bot = BotFactory.create_bot_with_db(bot_db, session_data) try: wallet_balance = bot.get_wallet_balance() # Logout bot if it was created without session data if session_data is None: bot.logout() except Exception as e: return response_error(f'Error while getting wallet balance: {e}', 500) else: return asdict(wallet_balance)
def _get_bots_from_database(self) -> Dict[ObjectId, BotData]: bots = {} for b in Bot.objects(is_enabled=True): b: Bot # Exclude bots that are resting if b.is_resting(): continue bot = BotFactory.create_bot(b.bookmaker_id, b.username, b.password, b.proxy, **b.init_params) bots[b.id] = BotData( bot=bot, name=b.name, value_bet_percent_increase=b.value_bet_percent_increase, value_bet_stake_percent_1=b.value_bet_stake_percent_1, value_bet_stake_percent_2=b.value_bet_stake_percent_2) return bots
def delete_bot(): try: bot_id = request.json['bot_id'] except KeyError: return response_error('Invalid request', 400) try: bot = Bot.objects(id=bot_id).first() except ValidationError: return response_error('Bot ID is invalid', 400) if bot is None: return response_error('Bot does not exist', 404) try: bot.delete() except Exception as e: return response_error(f'Database error: {e}', 500) return response_success()
def get_bookmaker_history(bot_id): try: page = int(request.args['page']) if page < 1: raise ValueError() except (KeyError, ValueError): flash('Invalid or missing page number', 'danger') return render_template('bet_bookmaker_history.html') try: bot_db = Bot.objects(id=bot_id).first() except ValidationError: return '', 404 if bot_db is None: return '', 404 if bot_db.is_enabled: session_data = current_app.redis_manager.get_bot_session_data(bot_id) else: session_data = None bot = BotFactory.create_bot_with_db(bot_db, session_data) try: history_entries, paging = bot.get_bet_history(page=page) if session_data is None: bot.logout() except Exception as e: flash(f'Error while getting bet history: {e}', 'danger') history_entries = [] paging = None return render_template('bet_bookmaker_history.html', history_entries=history_entries, paging=paging, bot=bot_db)
def _update_bots(self): """Load enabled bots from database""" self._bots = list(Bot.objects(is_enabled=True))
def add_bot(): if request.method == 'GET': return render_template('add_bot.html', proxies=_get_available_proxies(), bookmakers=_supported_bookmakers.keys()) else: try: name = request.form['name'] username = request.form['username'] password = request.form['password'] proxy = request.form['proxy'] proxy_country_code = request.form['proxy_country_code'] bookmaker = request.form['bookmaker'] vb_primary_stake = float(request.form['percent_primary']) vb_secondary_stake = float(request.form['percent_secondary']) vb_percent_increase = float(request.form['percent_increase']) vb_percent_threshold = float(request.form['percent_threshold']) rest_time_begin = int(request.form['rest_time_begin']) rest_time_end = int(request.form['rest_time_end']) except (KeyError, ValueError): flash('Invalid request.', 'danger') return redirect(url_for('bots.add_bot')) if bookmaker not in _supported_bookmakers: flash(f'Bookmaker: {bookmaker} is not supported.', 'danger') return redirect(url_for('bots.add_bot')) bookmaker_id = _supported_bookmakers[bookmaker]['bookmaker_id'] bookmaker_init_params = _supported_bookmakers[bookmaker]['init_params'] # Check for bot name uniqueness existing_bot_name = Bot.objects(name=name).first() if existing_bot_name is not None: flash(f'Name: {name} is already taken.', 'name') return redirect(url_for('bots.add_bot')) # Check for credentials uniqueness existing_bot_credentials = Bot.objects( username=username, bookmaker_id=bookmaker_id, init_params=bookmaker_init_params).first() if existing_bot_credentials is not None: flash( f'Username {username} already exists for {bookmaker} bookmaker.', 'credentials') return redirect(url_for('bots.add_bot')) # Test provided credentials test_bot = BotFactory.create_bot(bookmaker_id, username, password, proxy, **bookmaker_init_params) try: test_bot.login() test_bot.logout() except BotInvalidCredentialsError: flash('Incorrect credentials.', 'credentials') return redirect(url_for('bots.add_bot')) except Exception as e: flash(f'Other error: {e}', 'danger') return redirect(url_for('bots.add_bot')) # Add bot to the database bot = Bot(name=name, username=username, password=password, proxy=proxy, proxy_country_code=proxy_country_code, bookmaker_id=bookmaker_id, bookmaker_name=bookmaker, init_params=bookmaker_init_params, value_bet_stake_percent_1=vb_primary_stake, value_bet_stake_percent_2=vb_secondary_stake, value_bet_percent_increase=vb_percent_increase, value_bet_percent_threshold=vb_percent_threshold, rest_time_begin=rest_time_begin, rest_time_end=rest_time_end) try: bot.save() except Exception as e: flash(f'Database error: {e}', 'danger') return redirect(url_for('bots.add_bot')) return redirect(url_for('bots.bot_overview'))