-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLoungeAPIFunctions.py
82 lines (73 loc) · 2.84 KB
/
LoungeAPIFunctions.py
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
from typing import List, Dict
code = None
loungeAPIURL = "https://www.mkwlounge.gg/api/wiimmfi.php"
import aiohttp
from datetime import datetime
import common
async def getOnlineData(full_url):
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=5)) as session:
async with session.get(full_url, ssl=common.sslcontext) as r:
if r.status == 200:
js = await r.json()
return js
def addFilter(url, filter_type, data):
result = url
result += "&" + filter_type + "="
result += ",".join(data)
#result = urllib.parse.quote(result)
return result
def parseData(data:List[Dict], loungeVerifiedOnly=True):
if data is None:
print("Bad request to 255MP's Wiimmfi API... Data was None.")
return None, None
if "error" in data:
print("Bad request to 255MP's Wiimmfi API... Error in data.")
return None, None
if "status" not in data or data["status"] != "success":
print("Bad request to 255MP's Wiimmfi API... Status didn't exist or wasn't success.")
return None, None
if "results" not in data or not isinstance(data["results"], list):
print("Bad request to 255MP's Wiimmfi API... Results didn't exist or weren't a list")
return None, None
data_results = data["results"]
id_lounge = {}
fc_id = {}
cur_time = datetime.now()
for player in data_results:
if loungeVerifiedOnly:
if player['lounge_verified']:
id_lounge[player['discord_user_id']] = player['name']
fc_id[player['fc']] = (player['discord_user_id'], cur_time)
else:
if player['verified']:
id_lounge[player['discord_user_id']] = player['name']
fc_id[player['fc']] = (player['discord_user_id'], cur_time)
return id_lounge, fc_id
async def getByDiscordIDs(discordIDs:List[str], loungeVerifiedOnly=True):
fullAPIURL = loungeAPIURL + "?code=" + code
fullURL = addFilter(fullAPIURL, "discord_user_ids", discordIDs)
data = None
try:
data = await getOnlineData(fullURL)
except Exception as e:
print(e)
pass
return parseData(data, loungeVerifiedOnly)
async def getByLoungeNames(loungeNames:List[str], loungeVerifiedOnly=True):
fullAPIURL = loungeAPIURL + "?code=" + code
fullURL = addFilter(fullAPIURL, "player_names", loungeNames)
data = None
try:
data = await getOnlineData(fullURL)
except Exception as e:
print(e)
return parseData(data, loungeVerifiedOnly)
async def getByFCs(FCs:List[str], loungeVerifiedOnly=True):
fullAPIURL = loungeAPIURL + "?code=" + code
fullURL = addFilter(fullAPIURL, "fcs", FCs)
data = None
try:
data = await getOnlineData(fullURL)
except Exception as e:
print(e)
return parseData(data, loungeVerifiedOnly)