-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
120 lines (100 loc) · 4.1 KB
/
utils.py
File metadata and controls
120 lines (100 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import json
from datetime import timedelta
from json.decoder import JSONDecodeError
import requests
class MessageConverter:
def __init__(self):
self.season_icon = self.get_season_icon()
self.__human_to_machine_strings = {
"🔲 Epic": "epic",
"🟦 PSN": "psn",
"🟩 Xbox": "xbl",
"🍃 Lifetime": "lifetime",
f"{self.season_icon} Season": "season",
"🔢 Everything": "overall",
"1️⃣ Solo": "solo",
"2️⃣ Duo": "duo",
"4️⃣ Squad": "squad",
"🔐 Limited modes": "ltm",
}
self.__machine_to_human_strings = {
v: k for k, v in self.__human_to_machine_strings.items()
}
def get_season_icon(self) -> str:
try:
return json.loads(requests.get("https://pastebin.com/raw/HNxPB0zp").text)[
"time_window"
]
except JSONDecodeError:
return "❌"
def human_to_machine(self, human_str) -> str:
return (
self.__human_to_machine_strings[human_str]
if human_str in self.__human_to_machine_strings.keys()
else "Undefined"
)
def machine_to_human(self, machine_str) -> str:
return (
self.__machine_to_human_strings[machine_str]
if machine_str in self.__machine_to_human_strings.keys()
else "Undefined"
)
def prepare_result_msg(
username,
account_type="epic",
time_window="lifetime",
match_type="overall",
api_key=None,
) -> str:
data = json.loads(
requests.get(
"https://fortnite-api.com/v2/stats/br/v2"
f"?name={username}&accountType={account_type}&timeWindow={time_window}",
headers={"Authorization": api_key},
).text
)
# Sanitize input
username = username.replace("_", "\_").capitalize()
if data["status"] == 403:
rv = f"User *{username}* stats are not public 😔"
elif data["status"] != 200:
rv = f"User *{username}* not found on *{account_type.upper()}* platform! 🤷🏼♂️🔍"
else:
rv = f"👤 *Username*: {username}\n"
rv += f"⭐️ *Battle pass*: {data['data']['battlePass']['level']}\n\n"
# Shortcut
stats = data["data"]["stats"]["all"][match_type]
rv += f"⚔️ *{match_type.capitalize()}* ⚔️\n\n"
if stats is not None:
rv += f"📈 *Score*: {stats['score']}\n"
if match_type == "overall":
rv += f"👑 *Wins*: {stats['wins']}\n"
rv += f"🥇 *Top 3*: {stats['top3']}\n"
rv += f"🥈 *Top 5*: {stats['top5']}\n"
rv += f"🥉 *Top 6*: {stats['top6']}\n"
rv += f"🎖 *Top 10*: {stats['top10']}\n"
rv += f"🎖 *Top 12*: {stats['top12']}\n"
rv += f"🎖 *Top 25*: {stats['top25']}\n\n"
elif match_type == "solo":
rv += f"🥇 *Wins*: {stats['wins']}\n"
rv += f"🥈 *Top 10*: {stats['top10']}\n"
rv += f"🥉 *Top 25*: {stats['top25']}\n\n"
elif match_type == "duo":
rv += f"🥇 *Wins*: {stats['wins']}\n"
rv += f"🥈 *Top 5*: {stats['top5']}\n"
rv += f"🥉 *Top 12*: {stats['top12']}\n\n"
elif match_type == "squad":
rv += f"🥇 *Wins*: {stats['wins']}\n"
rv += f"🥈 *Top 3*: {stats['top3']}\n"
rv += f"🥉 *Top 6*: {stats['top6']}\n\n"
elif match_type == "ltm":
rv += f"🥇 *Wins*: {stats['wins']}\n"
rv += f"🏆 *Win rate*: {stats['winRate']}%\n"
rv += f"▶️ *Matches*: {stats['matches']}\n\n"
rv += f"💪🏻 *Kills*: {stats['kills']}\n"
rv += f"💀 *Deaths*: {stats['deaths']}\n"
rv += f"🧑🚀 *K/D ratio*: {stats['kd']}\n\n"
rv += f"🕒 *Time played*: {str(timedelta(minutes=stats['minutesPlayed']))[:-3]}"
else:
rv += "*No data found for this game type!*"
return rv