본문 바로가기

api

RIOT API 사용하기

728x90

riot api를 사용하기 위해서는 당연하게도 riot 홈페이지에 회원가입이 돼있어야 한다.

 

https://developer.riotgames.com/

 

Riot Developer Portal

About the Riot Games API With this site we hope to provide the League of Legends developer community with access to game data in a secure and reliable way. This is just part of our ongoing effort to respond to players' and developers' requests for data and

developer.riotgames.com

위 링크에서 register product를 클릭한다.

 

그러면 위와 같은 폼이 나오는데 production key는 공개 서비스를 제공할 때 받는 api key이다

나는 개인 공부할 때 사용할거라 personal api key를 선택하였다.

 

폼에 맞게 작성해주면 발급이 가능하다.

다른 글들 보면 일주일에서 2주까지 걸린다고 하는 분들도 있는데 난 바로 됐다.

 

이러면 키 발급이 가능하다. 24시간동안

 

24시간이 지나면 regenrate를 통해 재발급을 받으면 된다.

 

키를 통해서 간단히 롤토체스 티어와 승패를 검색해보겠다.

 

많은 api들이 있는데 TFT 적혀져있는것이 롤토체스 관련이다.

https://dk-kang.tistory.com/entry/Riot-API-%EC%A2%85%EB%A5%98

 

Riot API 살펴보기

Riot 에서 기본적으로 제공하는 API Key는 27가지가 있습니다. (2021년 7월 기준) (API 공식 문서 : https://developer.riotgames.com/apis) 그 중 LOL과 관련된 API는 15가지이고, 리그오브룬테라 5가지, 롤토체스 3가

dk-kang.tistory.com

이 글을 참고하면 자세히 알 수 있다.

여기 클릭 후 

닉네임을 검색하면 밑에 코드들이 나온다.

id를 복사해서 

여기 클릭 후 붙여넣기 해주면 

티어와 승패수가 나온다.

import json
import requests

api_key = key

print("1: 플레이어 검색")
selectnum = input("번호를 입력해주세요: ")

if selectnum == "1":
    name = input("소환사의 닉네임을 입력해주세요: ")
    URL = "https://kr.api.riotgames.com/tft/summoner/v1/summoners/by-name/"+name
    res = requests.get(URL, headers={"X-Riot-Token": api_key})
    if res.status_code == 200:
        #코드가 200일때
        resobj = json.loads(res.text)
        URL = "https://kr.api.riotgames.com/tft/league/v1/entries/by-summoner/"+resobj["id"]
        
        res = requests.get(URL, headers={"X-Riot-Token": api_key})
        rankinfo = json.loads(res.text)
        print("소환사 이름: "+name)
        for i in rankinfo:
            print(f'티어: {i["tier"]} {i["rank"]}')
            print(f'승: {i["wins"]}판, 패: {i["losses"]}판')
                
    else:
        print("소환사가 존재하지 않습니다")

 

+ 최근 한 경기에 대한 경기 정보도 알고싶어서 추가했다.

 

import json
import requests

api_key = key

print("1: 플레이어 검색")
selectnum = input("번호를 입력해주세요: ")

if selectnum == "1":
    name = input("소환사의 닉네임을 입력해주세요: ")
    URL = "https://kr.api.riotgames.com/tft/summoner/v1/summoners/by-name/"+name
    res = requests.get(URL, headers={"X-Riot-Token": api_key})
    if res.status_code == 200:
        #코드가 200일때
        resobj = json.loads(res.text)
        URL = "https://kr.api.riotgames.com/tft/league/v1/entries/by-summoner/"+resobj["id"]        
        res = requests.get(URL, headers={"X-Riot-Token": api_key})       
        rankinfo = json.loads(res.text)
        print("소환사 이름: "+name)
        for i in rankinfo:
            print(f'티어: {i["tier"]} {i["rank"]}')
            print(f'승: {i["wins"]}판, 패: {i["losses"]}판')
            
            
        URL = "https://asia.api.riotgames.com/tft/match/v1/matches/by-puuid/"+resobj["puuid"]+'/ids?start=0&count=1&api_key='+key
        res = requests.get(URL, headers={"X-Riot-Token": api_key})
        rankinfo = json.loads(res.text)
        for i in rankinfo:
            URL = 'https://asia.api.riotgames.com/tft/match/v1/matches/'+i+'?api_key='+key
            res = requests.get(URL, headers={"X-Riot-Token": api_key})
            
            data = res.json()
            rank = [None,None,None,None,None,None,None,None]
            
            
            for j in range(0,8):       #모든 유닛 가져오는 반복문 1
                p = data["info"]['participants'][j]['placement'] 
                part_rank = []         # 다음 유닛을 넣을 공간을 위해 리스트 초기화
                a = len(data["info"]['participants'][j]['units']) #한 유저의 유닛갯수
                                                            
                for k in range(0 ,a):  # k번째 유닛 가져오는거(모든 유닛을 하나씩 가지고 오는 작업)
                    
                    chm = data["info"]['participants'][j]['units'][k]['character_id']
                    real_chm = chm.replace('TFT7_','')
                    part_rank.append(real_chm)
                    if len(part_rank) == a:
                        rank[p-1] = part_rank
                        
                        
                        game_ppid = data["info"]['participants'][j]['puuid'] # 닉네임 찾는 작업
                        user_URL = 'https://kr.api.riotgames.com/tft/summoner/v1/summoners/by-puuid/'+game_ppid+'?api_key='+key
                        user_res = requests.get(user_URL, headers={"X-Riot-Token": api_key})
                        user_data = user_res.json()
                        part_rank.insert(0,user_data['name'])                        
            rr = 0
            sss = 1  #순위
            for r in rank:
                print(rank[rr][0],' ',sss,'등:' ,r[1:])
                sss += 1
                rr += 1
    else:
        print("소환사가 존재하지 않습니다")

사실상 위 코드의 활용버전이고 배열의 데이터 가져오는 부분만 어찌 잘하면 쉽게 가져올 수 있다.

 

728x90