fcfffc / tracker.py
coollsd's picture
Update tracker.py
0d46421 verified
import requests
import time
import json
import random
import os
from discord_webhook import DiscordWebhook, DiscordEmbed
api_url = "https://fcclub.deno.dev/"
keepthescore_api = "https://keepthescore.com/api/yrnfhgqvjhcyp/score/"
motm_webhook_url = os.environ.get('motm_webhook_url')
tracker_webhook_url = os.environ.get('tracker_webhook_url')
headers = {
"User-Agent": "Mozilla/5.0",
"Accept": "application/json"
}
with open("stinkies.json", "r") as f:
custom_names = json.load(f)
with open("playersdiscord.json", "r") as f:
players_discord = json.load(f)
with open("motivationalquotes.json", "r") as f:
motivational_quotes = json.load(f)
with open("manofthematch.json", "r") as f:
motm_hall = json.load(f)
player_ids = {
"FrostedSnows": 49238511,
"daboss888": 49238506,
"Kimo_10x10": 49238510,
"jbb_jbz": 49290659,
"1M_ohW": 49238507,
"corysfatnyash": 49238508,
"Aventic5618": 49238509,
"FredzPlayz": 49290660
}
def fetch_match_data():
response = requests.get(api_url, headers=headers, timeout=10)
response.raise_for_status()
return response.json()[0]
def increment_score(player_id, increment=1):
data = {
"player_id": player_id,
"score": increment,
"operation": "increment"
}
response = requests.post(keepthescore_api, json=data)
response.raise_for_status()
print(f"Incremented score by {increment} for player ID {player_id}")
def send_discord_message(match_data):
clubs = match_data["clubs"]
rejection_fc = clubs["481259"]
opponent_id = next(club_id for club_id in clubs if club_id != "481259")
opponent_team = clubs[opponent_id]["details"]["name"]
rejection_score = rejection_fc["score"]
opponent_score = clubs[opponent_id]["score"]
match_score = f"{rejection_score} - {opponent_score}"
embed_color = 0
if rejection_fc["losses"] == "1":
embed_color = 16711680
elif rejection_fc["ties"] == "1":
embed_color = 2697513
elif rejection_fc["wins"] == "1":
embed_color = 65300
player_data_all_teams = match_data["players"]
man_of_match = next((p["playername"] for team in player_data_all_teams.values() for p in team.values() if p["mom"] == "1"), "none or probably an opponent in case this doesnt work")
match_timestamp = match_data["timestamp"]
formatted_timestamp = f"<t:{match_timestamp}:R>"
embed = DiscordEmbed(
title=match_score,
description=f"**Opponent:** {opponent_team}\n**Man of the Match:** {man_of_match}\n**Played:** {formatted_timestamp}\n\nPlayers:",
color=embed_color
)
embed.set_thumbnail(url=f"https://eafc24.content.easports.com/fifa/fltOnlineAssets/24B23FDE-7835-41C2-87A2-F453DFDB2E82/2024/fcweb/crests/256x256/l{clubs[opponent_id]['TEAM']}.png")
player_data_rejection_fc = player_data_all_teams["481259"]
sorted_players = sorted(player_data_rejection_fc.values(), key=lambda x: float(x["rating"]), reverse=True)
for player in sorted_players:
player_name = player["playername"]
custom_name = custom_names.get(player_name, player_name)
position = player["pos"]
rating = player["rating"]
goals = int(player["goals"])
assists = player["assists"]
increment = goals // 3
if increment > 0:
player_id = player_ids.get(player_name)
if player_id:
increment_score(player_id, increment)
red_card_icon = "πŸŸ₯ " if player["redcards"] == "1" else ""
player_display_name = f"{red_card_icon}{custom_name} ({position})"
embed.add_embed_field(
name=player_display_name,
value=f"__Rating: {rating}__\n{goals} goals\n{assists} assists",
inline=True
)
if player["mom"] == "1":
send_motm_message(player_name)
webhook = DiscordWebhook(url=tracker_webhook_url)
webhook.add_embed(embed)
webhook.execute()
print(f"posted match {match_data['matchId']} with the score {match_score}")
if rejection_fc["losses"] == "1":
send_motivational_message()
def send_motivational_message():
message = random.choice(motivational_quotes)
webhook = DiscordWebhook(url=tracker_webhook_url, content=message)
webhook.execute()
def send_motm_message(player_name):
discord_tag = players_discord.get(player_name, "player")
msg_gif = random.choice(motm_hall)
main_message = f"{discord_tag} is the MAN OF THE MATCH! πŸ†"
motm_webhook = DiscordWebhook(url=motm_webhook_url, content=main_message)
motm_webhook.execute()
gif_message = DiscordWebhook(url=motm_webhook_url, content=msg_gif)
gif_message.execute()
def monitor_matches():
last_match_id = None
while True:
try:
match_data = fetch_match_data()
match_id = match_data["matchId"]
if match_id != last_match_id:
send_discord_message(match_data)
last_match_id = match_id
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)
monitor_matches()