def comment_handler(self, comment): """Generic comment handler.""" logger.info(f"Handling COMMENT with ID {comment.id}.") match = re.search(versus_pattern, comment.body.lower()) if not match: logger.info(f"Comment with ID {comment.id} does not match pattern.") return player_name = match.group(1).lower().strip() opponent_name = match.group(2).lower().replace(".", "").strip() number = match.group(3) if number: number = int(number) if to_fpl_team(opponent_name) in fpl_team_names: reply_text = self.versus_team_handler( player_name, opponent_name, number) else: reply_text = self.versus_player_handler( player_name, opponent_name, number) if reply_text: logger.info(f"Replying ({player_name} vs. {opponent_name}) to " f"comment with ID {comment.id}.") comment.reply(reply_text) self.add_comment_to_database(comment)
def versus_team_handler(self, player_name, team_name, number_of_fixtures): """Function for handling player vs. team comment.""" player = find_player(player_name) if not player: return if not number_of_fixtures: number_of_fixtures = len(player["understat_history"]) fixtures = get_relevant_fixtures( player, team_name=to_fpl_team(team_name))[:number_of_fixtures] post_template = open(f"{dirname}/../comment_template.md").read() table_header = ( f"# {player_name.title()} vs. {team_name.title()} (last " f"{len(fixtures)} fixtures)") table_body = player_vs_team_table(fixtures) return post_template.format(comment_header=table_header, comment_body=table_body)
def versus_team_handler(self, player_name, team_name, number_of_fixtures): """Function for handling player vs. team comment.""" # Find most relevant player using text search players = self.database.players.find( { "$text": { "$search": player_name } }, { "score": { "$meta": "textScore" } }).sort([("score", { "$meta": "textScore" })]) try: player = list(players.limit(1))[0] except IndexError: logger.error(f"Player {player_name} could not be found!") return if not number_of_fixtures: number_of_fixtures = len(player["understat_history"]) fixture_count = 0 relevant_fixtures = [] team_name = to_fpl_team(team_name) for fixture in player["understat_history"]: if fixture_count >= int(number_of_fixtures): break if (team_name != fixture["h_team"].lower() and team_name != fixture["a_team"].lower()): continue fixture_count += 1 relevant_fixtures.append(fixture) player_vs_team_table = self.player_vs_team_table(relevant_fixtures) return player_vs_team_table
def comment_handler(self, comment): """Generic comment handler.""" match = re.search(versus_pattern, comment.body.lower()) if not match: return player_name = match.group(1).lower().strip() opponent_name = match.group(2).lower().replace(".", "").strip() number = match.group(3) if number: number = int(number) if to_fpl_team(opponent_name) in fpl_team_names: reply_text = self.versus_team_handler(player_name, opponent_name, number) else: reply_text = self.versus_player_handler(player_name, opponent_name, number) if reply_text: comment.reply(reply_text) self.add_comment_to_database(comment)