def get_tracked_stocks_news(): """ Show me all the news articles for stocks im tracking, filter by params Params: symbol, start, end """ try: data = TrackedStocksNews().load(request.args.to_dict()) if "symbol" in data: symbol = data["symbol"].upper() else: symbol = "" start = data["start"] end = data["end"] detailed = data["detailed"] tracked_stocks_news_list = get_tracked_stocks_news_details( detailed, start, end, symbol) if not tracked_stocks_news_list: logger = get_logger_with_context("") logger.info("Get Tracked Stocks News - error: 404 Not Found") return jsonify({}), 404 resp = jsonify({"status": "success", "data": tracked_stocks_news_list}) resp.status_code = 200 logger = get_logger_with_context("") logger.info( "Get Tracked Stocks News- status: 200 OK, Tracked Stocks News:") return resp except ValidationError as error: resp = jsonify({"error": error.messages}), 400 logger = get_logger_with_context("") logger.info("Check Stock News - error: 400 Bad Request") return resp
def get_tracked_stocks(): """ Show me all the stocks im tracking, filter by params Params: symbol, detailed = True/False If "detailed" = True, show percent difference and last modified date """ try: data = TrackedStocksSchema().load(request.args.to_dict()) if "symbol" in data: symbol = data["symbol"].upper() else: symbol = "" detailed = data["detailed"] tracked_stocks_list = get_tracked_stocks_details(detailed, symbol) if not tracked_stocks_list: logger = get_logger_with_context("") logger.info("Get Tracked Stocks - error: 404 Not Found") return jsonify({}), 404 resp = jsonify({"status": "success", "data": tracked_stocks_list}) resp.status_code = 200 logger = get_logger_with_context("") logger.info( "Get Tracked Stocks - status: 200 OK, Tracked Stocks: {tracked_stocks_list}" .format(tracked_stocks_list=tracked_stocks_list)) return resp except ValidationError as error: resp = jsonify({"error": error.messages}), 400 logger = get_logger_with_context("") logger.info("Check Stock Difference - error: 400 Bad Request") return resp
async def stats_operations(ctx, *params): """ Outputs stats based on the parameters provided. Valid options are: 1. Stats diff of the squad players today 2. Average stats of the players faced today """ logger = get_logger_with_context(ctx) params = list(params) command = params.pop(0) if params else None if not command: message = "Please specify a command, ex: `!stats diff` or `!stats played`" logger.warning(message) await ctx.send(message) return usernames = params or SQUAD_PLAYERS_LIST if command in commands.STATS_DIFF_COMMANDS: logger.info(f"Querying stats diff today for {', '.join(usernames)}") await _stats_diff_today(ctx, usernames) elif command in commands.STATS_OPPONENTS_COMMANDS: logger.info("Querying opponent stats today") await _opponent_stats_today(ctx) else: await ctx.send(f"Command provided '{command}' is not valid")
def check_stock_difference(): """ POST request Body { "symbol": "F", "avg_purchase_cost": 5.00, "percent": 5, } """ try: data = StockDifferenceSchema().load(request.get_json()) symbol = data["symbol"].upper() avg_purchase_cost = float(data["avg_purchase_cost"]) percent = data["percent"] response = get_stock_quote(symbol) if not response: return jsonify( {"error": f"You entered an invalid stock symbol: {symbol}"}), 404 percent_difference = calculate_percent_change(response, avg_purchase_cost) resp = jsonify({ "status": "success", "data": { "symbol": symbol, "percent_difference": percent_difference } }) resp.status_code = 200 logger = get_logger_with_context("") logger.info( "Check Stock Difference - status: 200 OK, Symbol: {symbol}, Percent Difference: {percent_difference}" .format(symbol=symbol, percent_difference=percent_difference)) return resp except ValidationError as error: resp = jsonify({"error": error.messages}), 400 logger = get_logger_with_context("") logger.info("Check Stock Difference - error: 400 Bad Request") return resp
async def replays_operations(ctx, *params): """ Outputs replays stats based on the command provided. Valid options are: 1. killed/elims - show stats of players that we eliminated 2. log - log to db 3. show stats of players that eliminated us """ logger = get_logger_with_context(ctx) params = list(params) global eliminated_by_me_dict global eliminated_me_dict if not eliminated_me_dict and not eliminated_by_me_dict: await ctx.send("No replay file found") return command = None username = None if len(params) == 2: username = params.pop(1) command = params.pop(0) if username not in SQUAD_PLAYERS_LIST: await ctx.send(f"{username} provided is not a valid squad player") return if len(params) == 1: if params[0] in SQUAD_PLAYERS_LIST: username = params.pop(0) else: command = params.pop(0) if command in commands.REPLAYS_ELIMINATED_COMMANDS: logger.info("Outputting players that got eliminated by us") await output_replay_eliminated_by_me_stats_message(ctx, eliminated_by_me_dict, username, silent=False) elif command in commands.REPLAYS_LOG_COMMANDS: logger.info("Silent logging players that got eliminated by us and eliminated us") await output_replay_eliminated_me_stats_message(ctx, eliminated_me_dict, username, silent=True) await output_replay_eliminated_by_me_stats_message(ctx, eliminated_by_me_dict, username, silent=True) else: if not command: logger.info("Outputting players that eliminated us") await output_replay_eliminated_me_stats_message(ctx, eliminated_me_dict, username, silent=False) elif command != commands.REPLAYS_COMMAND and \ command not in commands.REPLAYS_ELIMINATED_COMMANDS and \ command not in commands.REPLAYS_LOG_COMMANDS: await ctx.send(f"Command provided '{command}' is not valid") else: await ctx.send(f"{command} left the channel")
async def player_search(ctx, *player_name, guid=False, silent=False): """ Searches for a player's stats, output to Discord, and log in database """ player_name = " ".join(player_name) logger = get_logger_with_context(ctx) logger.info("Looking up stats for '%s' ", player_name) if not player_name: await ctx.send("Please specify an Epic username after the command, " "ex: `!hunted LigmaBalls12`") return try: await fortnite_tracker.get_player_stats(ctx, player_name, silent) except Exception as e: logger.warning(e, exc_info=_should_log_traceback(e)) # Fortnite API stats are unnecessary in silent mode if silent: return logger.warning(f"Falling back to Fortnite API for '{player_name}'..") await fortnite_api.get_player_stats(ctx, player_name, guid)
def healthcheck(): logger = get_logger_with_context("") logger.info("Health check status: OK") return jsonify({"status": "ok"}), 200
async def on_guild_join(guild): """ Event handler to log when the bot joins a new server """ logger = get_logger_with_context(identifier="Main") logger.info("Bot added to new server! Server name: %s", guild.name)
async def on_ready(): """ Event handler to setup logger on load """ logger = get_logger_with_context(identifier="Main") logger.info("Started up %s", bot.user.name) logger.info("Bot running on servers: %s", ", ".join([guild.name for guild in bot.guilds]))