-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
88 lines (78 loc) · 3.81 KB
/
Copy pathtest.py
File metadata and controls
88 lines (78 loc) · 3.81 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
import requests
import time
import asyncio
import aiohttp
import os
from dotenv import load_dotenv
from pymongo import MongoClient
load_dotenv()
api_key = os.getenv('API_KEY')
MONGODB_URL = os.getenv('MONGODB_URL')
headers = {
"Authorization": api_key,
}
client = MongoClient(MONGODB_URL) #connecting to mongodb instance
db = client['test'] #switches to the test database
data = db['users'] # then switches to the users collection, stores player name and match id on seperate documents
# testing api call times
# start = time.time()
# url = "https://api.henrikdev.xyz/valorant/v1/account/brutal/111"
# response = requests.get(url, headers=headers)
# end = time.time()
# print(end-start)
def getPuuid(name, tag):
url = f"https://api.henrikdev.xyz/valorant/v1/account/{name}/{tag}"
response = requests.get(url, headers=headers) #get request set to response
if(response.status_code == 200): # if it executes good then it returns the player id.
return response.json()['data']['puuid']
else:
return None
def getMatchid(region, name, tag):
url = f"https://api.henrikdev.xyz/valorant/v3/matches/{region}/{name}/{tag}?mode=competitive&size=1"
response = requests.get(url, headers=headers)
if(response.status_code == 404 or response.status_code == 404):
return 500
elif(response.status_code == 200):
return{'matchid': response.json()['data'][0]['metadata']['matchid'],
'remaining': int(response.headers.get('x-ratelimit-remaining')),
'reset': int(response.headers.get('x-ratelimit-reset'))
}
else:
return None
def latestMatchStats(region,name,tag):
url = f"https://api.henrikdev.xyz/valorant/v3/matches/{region}/{name}/{tag}?mode=competitive&size=1"
response = requests.get(url, headers=headers)
data = response.json()['data'][0]['players']
for player in data['all_players']: #iterates through each player profile in LATEST MATCH and checks for certain puuids
if((player['name'] == name) and (player['tag'] == tag)):
teamColor = player['team'].lower()
if(response.json()['data'][0]['teams'][teamColor]['has_won'] == True):
score = str(response.json()['data'][0]['teams'][teamColor]['rounds_won']) + "-" + str(response.json()['data'][0]['teams'][teamColor]['rounds_lost'])
outcome = "won"
else:
outcome = "lost"
score = str(response.json()['data'][0]['teams'][teamColor]['rounds_won']) + "-" + str(response.json()['data'][0]['teams'][teamColor]['rounds_lost'])
return{ # returns a dictionary, with all the stats of the latest match.
'agent': player['character'],
'agentpic': player['assets']['agent']['small'],
'rank': player['currenttier_patched'],
'map': response.json()['data'][0]['metadata']['map'],
'scorestat': player['stats']['score'],
'kills': player['stats']['kills'],
'deaths': player['stats']['deaths'],
'assists': player['stats']['assists'],
'kd': round((player['stats']['kills'] / player['stats']['deaths']),2),
'hs': (round((player['stats']['headshots'] / (player['stats']['bodyshots'] + player['stats']['headshots'] + player['stats']['legshots'])) * 100)),
'outcome': outcome,
'score': score
}
def addUser(name, tag, region):
matchID = getMatchid(region, name,tag)['matchid']
user = data.find_one({"name": name,'tag': tag})
if user != None: #case for duplicates
return ("already exists")
if(matchID == None):
return ("does not exist")
else:
data.insert_one({"name": name,"tag": tag,"matchID": matchID, "region": region})
return("sucessfully added")