import requests import time import json import os from discord_webhook import DiscordWebhook, DiscordEmbed proclubs_memberscareer_api = "https://fcleaderboard.deno.dev/" keepthescore_api = "https://keepthescore.com/api/yrnfhgqvjhcyp/board/" headers = { "User-Agent": "Mozilla/5.0", "Accept": "application/json" } discord_webhook_link2 = os.environ.get('discord_webhook_link2') with open("stinkies.json", "r") as f: custom_names = json.load(f) previous_stats = {} def fetch_player_stats(): response = requests.get(proclubs_memberscareer_api, headers=headers, timeout=10) response.raise_for_status() data = response.json() return data.get("members", []) def fetch_hat_tricks(): response = requests.get(keepthescore_api, timeout=10) response.raise_for_status() data = response.json() hat_tricks = {player["name"]: player["score"] for player in data["players"]} return hat_tricks def send_leaderboard(leaderboard): embed = DiscordEmbed( title=f"Leaderboard ", color=None ) for rank, player in enumerate(leaderboard, start=1): embed.add_embed_field( name=f"{rank}. {player['custom_name']}", value=f"__G/A: {player['total']}__\nGoals: {player['goals']}\nAssists: {player['assists']}\nHat-Tricks: {player['hat_tricks']}\nMatches: {player['matches']}\nMOTM: {player['man_of_the_match']}", inline=True ) webhook = DiscordWebhook(url=discord_webhook_link2) webhook.add_embed(embed) webhook.execute() print("posted leaderboard") def get_leaderboard(): player_stats = fetch_player_stats() hat_tricks = fetch_hat_tricks() leaderboard = [] for player in player_stats: name = player.get("name") goals = int(player.get("goals", 0)) assists = int(player.get("assists", 0)) total_goals_assists = goals + assists hat_tricks_count = hat_tricks.get(name, 0) matches = int(player.get("gamesPlayed", 0)) man_of_the_match = int(player.get("manOfTheMatch", 0)) custom_name = custom_names.get(name, name) leaderboard.append({ "name": name, "custom_name": custom_name, "goals": goals, "assists": assists, "total": total_goals_assists, "hat_tricks": hat_tricks_count, "matches": matches, "man_of_the_match": man_of_the_match }) leaderboard = sorted(leaderboard, key=lambda x: x["total"], reverse=True) return leaderboard def has_changes(current_stats): global previous_stats for player in current_stats: name = player["name"] goals = player["goals"] assists = player["assists"] if name not in previous_stats or previous_stats[name]["goals"] != goals or previous_stats[name]["assists"] != assists: return True previous_stats = {player["name"]: {"goals": player["goals"], "assists": player["assists"]} for player in current_stats} return False def imsurejbbolterwillenjoydissectingthisframebyframe(): global previous_stats while True: try: current_stats = get_leaderboard() if has_changes(current_stats): send_leaderboard(current_stats) previous_stats = {player["name"]: {"goals": player["goals"], "assists": player["assists"]} for player in current_stats} except requests.exceptions.RequestException as e: print(f"oh noes an error {e} retrying in 10 seconds") time.sleep(10) except Exception as e: print(f"my god not another error {e} oh well retrying in 10 seconds") time.sleep(10) time.sleep(60) imsurejbbolterwillenjoydissectingthisframebyframe()