Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chatbot/chatbot/botfunctions.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
164 lines (133 sloc)
9.55 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import secretkeys | |
from xmlrpc.client import Boolean | |
import requests | |
## gets summoner id or puuid by name | |
def get_summoner_id(summoner, puuid=Boolean): | |
## takes 2 arguments, summoner name and a bool, returns either the puuid or id which we need to other API calls | |
url = f'https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/{summoner}?api_key={secretkeys.riot_api_key}' | |
response = requests.get(url).json() | |
if puuid == False: | |
return response['id'] | |
elif puuid == True: | |
return response['puuid'] | |
def get_champion_id_by_name(champion_name): | |
url = f'http://ddragon.leagueoflegends.com/cdn/12.20.1/data/en_US/champion.json' | |
get_champion_info = requests.get(url) | |
data = get_champion_info.json()["data"] | |
champion_id = data[champion_name]["key"] | |
wrong_name_error = "Wrong name" | |
if champion_id == 0: | |
return wrong_name_error | |
else: | |
return champion_id | |
## gets champion name by id | |
def get_champion_name_by_id(id): | |
## takes in an id as argument and returns the champion's name | |
url = f'http://ddragon.leagueoflegends.com/cdn/12.20.1/data/en_US/champion.json' | |
response = requests.get(url).json() | |
datadragon = response['data'] | |
for champion in datadragon: | |
if str(id) == datadragon[champion]['key']: | |
return champion | |
## gets top five most played champions | |
def get_top5(summoner): | |
summoner = get_summoner_id(summoner,False) | |
url = f'https://euw1.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-summoner/{summoner}?api_key={secretkeys.riot_api_key}' | |
response = requests.get(url).json() | |
most_played = [] | |
for champion in response[0:5]: | |
champion_name = get_champion_name_by_id(champion['championId']) | |
most_played.append(champion_name) | |
return most_played | |
## gets last 10 games can adjust for up to last 100 games | |
def get_match_history(summoner): | |
puuid = get_summoner_id(summoner,True) | |
url = f'https://europe.api.riotgames.com/lol/match/v5/matches/by-puuid/{puuid}/ids?start=0&count=20&api_key={secretkeys.riot_api_key}' | |
match_history = requests.get(url).json() | |
return match_history | |
def get_winrate(summoner): | |
matches = get_match_history(summoner) | |
champ_wr = {} | |
for match in matches: | |
url = f'https://europe.api.riotgames.com/lol/match/v5/matches/{match}?api_key={secretkeys.riot_api_key}' | |
response = requests.get(url).json() | |
players = response['info']['participants'] | |
for player in players: | |
if summoner == player['summonerName']: | |
game_won = player['win'] | |
champion = player['championName'] | |
break | |
if champion in champ_wr: | |
champ_wr[champion][0] += 1 | |
else: | |
champ_wr[champion] = [1,0,0] | |
if game_won: | |
champ_wr[champion][1] += 1 | |
else: | |
champ_wr[champion][2] += 1 | |
return champ_wr | |
get_winrate('tibijczykzvesti') | |
## gets ranked stats | |
def get_summoner_stats(summoner): | |
summoner = get_summoner_id(summoner,False) | |
url =f'https://euw1.api.riotgames.com/lol/league/v4/entries/by-summoner/{summoner}?api_key={secretkeys.riot_api_key}' | |
response = requests.get(url).json() | |
data = response[0] | |
tier = data['tier'] | |
rank = data['rank'] | |
league_points = str(data['leaguePoints']) + 'LP' | |
winrate = str(round(data['wins']/(data['wins']+data['losses'])*100,2))+'%' | |
streak = data['hotStreak'] | |
ranked_stats = [tier,rank,league_points,winrate] | |
if streak == True: | |
ranked_stats.append(streak) | |
return (ranked_stats) | |
def getMatchListByAccountId(accountId): | |
print("Running Service: getMatchListByAccountId...") | |
url = 'https://na1.api.riotgames.com/lol/match/v3/matchlists/by-account/%s?api_key=%s' % (accountId, secretkeys.riot_api_key) | |
response = requests.get(url) | |
return response.json() | |
def getMatchByGameId(gameId): | |
print("Running Service: getMatchByGameId...") | |
url = 'https://na1.api.riotgames.com/lol/match/v3/matches/%s?api_key=%s' % (gameId, secretkeys.riot_api_key) | |
response = requests.get(url) | |
return response.json() | |
def getParticipantStats(participantId, game): | |
print("Getting Participant Stats...") | |
return game['participants'][participantId] | |
def sortStats(participantStats): | |
try: | |
stats = {'championId':participantStats['championId'],'participantId':participantStats['participantId'],'win':participantStats['stats']['win'],'item0':participantStats['stats']['item0'],'item1':participantStats['stats']['item1'],'item2':participantStats['stats']['item2'],'item3':participantStats['stats']['item3'], | |
'item4':participantStats['stats']['item4'],'item5':participantStats['stats']['item5'],'item6':participantStats['stats']['item6'],'kills':participantStats['stats']['kills'], 'deaths':participantStats['stats']['deaths'], | |
'assists':participantStats['stats']['assists'],'longestTimeSpentLiving':participantStats['stats']['longestTimeSpentLiving'],'totalDamageDealt':participantStats['stats']['longestTimeSpentLiving'], | |
'magicDamageDealt':participantStats['stats']['magicDamageDealt'],'physicalDamageDealt':participantStats['stats']['physicalDamageDealt'],'trueDamageDealt':participantStats['stats']['trueDamageDealt'], | |
'totalDamageDealtToChampions':participantStats['stats']['totalDamageDealtToChampions'],'magicDamageDealtToChampions':participantStats['stats']['magicDamageDealtToChampions'],'physicalDamageDealtToChampions':participantStats['stats']['physicalDamageDealtToChampions'], | |
'trueDamageDealtToChampions':participantStats['stats']['trueDamageDealtToChampions'],'totalHeal':participantStats['stats']['totalHeal'],'damageSelfMitigated':participantStats['stats']['damageSelfMitigated'], | |
'damageDealtToObjectives':participantStats['stats']['damageDealtToObjectives'],'damageDealtToTurrets':participantStats['stats']['damageDealtToTurrets'],'visionScore':participantStats['stats']['visionScore'], | |
'timeCCingOthers':participantStats['stats']['timeCCingOthers'],'totalDamageTaken':participantStats['stats']['totalDamageTaken'],'magicalDamageTaken':participantStats['stats']['magicalDamageTaken'], | |
'physicalDamageTaken':participantStats['stats']['physicalDamageTaken'],'goldEarned':participantStats['stats']['goldEarned'],'goldSpent':participantStats['stats']['goldSpent'],'totalMinionsKilled':participantStats['stats']['totalMinionsKilled'], | |
'neutralMinionsKilled':participantStats['stats']['neutralMinionsKilled'],'visionWardsBoughtInGame':participantStats['stats']['visionWardsBoughtInGame'],'wardsPlaced':participantStats['stats']['wardsPlaced'],'wardsKilled':participantStats['stats']['wardsKilled']} | |
return stats; | |
#Error handling for assumed non-summoner's rift game mode | |
except KeyError as error: | |
print('KeyError has occured because - ', str(error), ' was not found in the JSON to be sorted.') | |
print('Trying new parse for assumed non-summoner\'s rift game mode') | |
stats = {'championId':participantStats['championId'],'participantId':participantStats['participantId'],'win':participantStats['stats']['win'],'item0':participantStats['stats']['item0'],'item1':participantStats['stats']['item1'],'item2':participantStats['stats']['item2'],'item3':participantStats['stats']['item3'], | |
'item4':participantStats['stats']['item4'],'item5':participantStats['stats']['item5'],'item6':participantStats['stats']['item6'],'kills':participantStats['stats']['kills'], 'deaths':participantStats['stats']['deaths'], | |
'assists':participantStats['stats']['assists'],'longestTimeSpentLiving':participantStats['stats']['longestTimeSpentLiving'],'totalDamageDealt':participantStats['stats']['longestTimeSpentLiving'], | |
'magicDamageDealt':participantStats['stats']['magicDamageDealt'],'physicalDamageDealt':participantStats['stats']['physicalDamageDealt'],'trueDamageDealt':participantStats['stats']['trueDamageDealt'], | |
'totalDamageDealtToChampions':participantStats['stats']['totalDamageDealtToChampions'],'magicDamageDealtToChampions':participantStats['stats']['magicDamageDealtToChampions'],'physicalDamageDealtToChampions':participantStats['stats']['physicalDamageDealtToChampions'], | |
'trueDamageDealtToChampions':participantStats['stats']['trueDamageDealtToChampions'],'totalHeal':participantStats['stats']['totalHeal'],'damageSelfMitigated':participantStats['stats']['damageSelfMitigated'], | |
'damageDealtToObjectives':participantStats['stats']['damageDealtToObjectives'],'damageDealtToTurrets':participantStats['stats']['damageDealtToTurrets'],'visionScore':participantStats['stats']['visionScore'], | |
'timeCCingOthers':participantStats['stats']['timeCCingOthers'],'totalDamageTaken':participantStats['stats']['totalDamageTaken'],'magicalDamageTaken':participantStats['stats']['magicalDamageTaken'], | |
'physicalDamageTaken':participantStats['stats']['physicalDamageTaken'],'goldEarned':participantStats['stats']['goldEarned'],'goldSpent':participantStats['stats']['goldSpent'],'totalMinionsKilled':participantStats['stats']['totalMinionsKilled'], | |
'neutralMinionsKilled':participantStats['stats']['neutralMinionsKilled'],'visionWardsBoughtInGame':participantStats['stats']['visionWardsBoughtInGame']} | |
return stats | |
def getChampionMastery(summoner_id,champion_name): | |
print('Running service: getChampionMastery...') | |
champion_id = champion_name | |
url = f'https://euw1.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-summoner/{summoner_id}/by-champion/{champion_id}?api_key={secretkeys.riot_api_key}' | |
response = requests.get(url) | |
data = response.json() | |
print(data) | |
return data |