리그 오브 레전드 게임에서도 API를 제공합니다.

사실 op.gg라는 거의 완벽한 사이트가 있어 굳이 내가 라이엇에서 제공하는 API를 사용하여 나의 전적이나 나와 함께 플레이한 유저들의 정보를 굳이 볼 필요는 없을 수 있지만 재미로 만들어 보았습니다.

 

API는 HTTP의 Request 요청을 통하여 서버로부터 관련 정보를 가져오고 별도의 requests 라는 파이썬 모듈을 이용하면 쉽게 요청과 응답을 통해 정보를 받을 수 있습니다. 이번 작업 덕분에 HTTP의 서버 요청(GET, POST)과 응답(RESPONSE) 개념도 이해하게 되었습니다. 이전에 SPOTIFY의 API 역시 마찬가지였지만 별도의 모듈이 있어 그 땐 이렇게 깊게 생각해 보진 않았었네요.ㅎ

 

일단 라이엇의 개발자 포탈 페이지에 접속합니다.

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

그리고 로그인을 하면 각종 동의 문구들에 확인을 누르면 다음과 같이 API Key를 생성하는 화면이 나옵니다.

하지만 그전에 REGISTER PRODUCT 버튼을 눌러 등록을 합니다. (PRIVATE PERSONAL API로 합시다.)

 

주목할 점은 1초에 20번의 requests만 가능하고 2분에 100번의 requests만 가능하다는 점입니다. 물론 회사 입장에서는 서버의 과부하를 막기 위한 방편이겠지요. 참고로 op.gg역시 초기 데이터는 위 api를 이용하지만 결국 기존의 데이터는 모두 자신들의 서버에 저장하여 속도 보장과 과부하를 방지한다고 합니다.

 

그리고 API KEY를 생성했으면 본격적으로 서버에 정보를 요청할 수 있습니다.

페이지 위의 APIS메뉴로 이동합니다.

 

일단 가장 Simple하게 확인 할 수 있는 내 정보를 한번 조회해 보겠습니다.

APIS중에서 SUMMONER-V4로 이동하면 총 네개의 API가 제공되는데 가장 간단하게 소환사명으로 내 정보를 확인해 보겠습니다.

 

위의 사진과 같이 소환사 명으로 알 수 있는 자료는 accountId 부터 죽 있습니다만 여기서 얻은 자료를 이용하여 다른 API에서 각종 정보를 얻는다고 볼 수 있습니다.

 

이제 본격적으로 파이썬을 통하여 정보를 요청하고 받아보기로 하겠습니다.

 

1. 라이브러리(모듈) import

 

앞서 말했듯이 위 API는 HTTP 기반의 모듈이고 이에 파이썬의 requests 모듈이 필수적으로 쓰입니다.

그래서 requests 모듈을 불러오고 추가로 urllib의 parse 모듈을 불러옵니다.

urllib의 parse 모듈은 한글을 아스키 코드로 변환할 때 쓰이는 모듈로 한글 소환사명은 꼭 진행해야 하는 절차입니다.

그리고 추가로 time 모듈을 불러옵니다. 이는 위에서 언급한대로 정해진 시간에 불러올 수 있는 api의 회수가 제한되어 있어 어쩔 수 없이 딜레이를 주기 위합입니다...ㅠㅠ

 

2. api키 및 request header 값에 대한 변수 설정

 

리그 오브 레전드 서버에 정보를 request하기 위해서 request와 함께 request header를 설정하여 유효한 요청이 되게 합니다. 모두 동일하지만 각 API마다 하단에 필요한 request_header가 있으니 그대로 복사 붙여넣기 하시면 되시고 다만 "X-Riot-Token" 의 값이 내가 부여받은 하루만 유효한 API 키이기 때문에 직접 붙여넣기 하시거나 따로 변수를 만들어 넣어도 됩니다.

 

3. Requests 모듈을 이용하여 현재 나의 팀원의 최근 10경기 승률을 보기

 

일단 빈 리스트를 만들고 나를 제외한 총 4명의 팀원이기 때문에 4번 반복 for 문을 돌리고 parse.quote()를 이용하여 한글 소환사명인 경우에는 아스키 코드로 바꾸어 줍니다.

그리고 requests.get("요청 주소", headers = request_headers)을 이용하여 소환사 정보를 가져옵니다. 그리고 받아온 정보는 json형태이기 때문에 .json()을 이용하여 파이썬의 딕셔너리 형태로 바꾸어 줍니다.

(서버통신에서 API로 가져온 정보는 대부분 json형식인듯 합니다.)

 

받아온 딕셔너리 형태의 소환사 정보 중에서 AccountId 정보만을 빼 내어 [AccountId],

다시 AccountId로 플레이어의 전체 matchlists를 불러오기 API를 사용합니다. 

그리고 matchlists 정보에서 matchId를 빼 냅니다. ["matches"][n]["gameId"] (n은 각 게임의 순번입니다.)

그리고 빼낸  matchId로 match 불러오기 API를 사용합니다.

(결국 필요한 정보를 찾기 위하여 3개의 API를 연속으로 사용해야 합니다.)

그리고 match 정보에서 최종 승패 여부를 확인 합니다. ["participants"][i]["stats"]["win"]

하지만 match 정보에서는 1명의 플레이어가 아닌 10명의 플레이어에 대한 승패 여부가 모두 나오기 때문에

["participantIdentities"][i]["player"]["summonerName"]로 정확히 원하는 소환사의 승패 여부를 확인합니다.

(위의 i가 플레이어의 순서니 i값은 동일해야 합니다.

 

이런식으로 원하는 정보를 얻는데 위의 작업만도 44번의 API를 요청하고 속도가 그렇게 빠르다곤 할 수 없습니다..

또 계속 시행착오를 해서 그런지 API 요청 제한에도 계속 걸렸습니다. 그리고 무엇보다도 이미 OP.GG라는 훌륭한 사이트가 있어 이정도 속도라면 굳이 나만의 확인 프로그램을 만들어 사용할 필요가 없겠습니다...가 저의 결론입니다.^^;

 

아무튼 작년 언젠가부터 해보려고 맘만 먹다가 막상 하고 나니 생각보다 오래 걸리지도 않고 어렵지 않았습니다.

 

 

아래는 전체 코드입니다.(API Key는 어차피 이미 만료된 키라 공개합니다.^^;)

 

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
import requests
from urllib import parse
import time
 
api_key = "RGAPI-6840792e-23cc-4695-afbc-b4955709159a"
request_headers = {
    "User-Agent""Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36",
    "Accept-Language""en-US,en;q=0.9,ko-KR;q=0.8,ko;q=0.7",
    "Accept-Charset""application/x-www-form-urlencoded; charset=UTF-8",
    "Origin""https://developer.riotgames.com",
    "X-Riot-Token""RGAPI-6840792e-23cc-4695-afbc-b4955709159a"
}
 
def check_my_team(*args):
    current_players = []
    for n in range(04):
        name = args[n]
        print(name)
        encoded_name = parse.quote(name)
        current_players.append(encoded_name)
        summoner_account_id = requests.get("https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/" + encoded_name, headers=request_headers).json()["accountId"]
        win = 0
        for n in range(010):
            get_latest_match_id = requests.get("https://kr.api.riotgames.com/lol/match/v4/matchlists/by-account/" + summoner_account_id, headers=request_headers).json()["matches"][n]["gameId"]
            get_match_info = requests.get("https://kr.api.riotgames.com/lol/match/v4/matches/" + str(get_latest_match_id), headers=request_headers).json()
            for i in range(010):
                if get_match_info["participantIdentities"][i]["player"]["summonerName"== name:
                    if get_match_info["participants"][i]["stats"]["win"== True:
                        win += 1
            time.sleep(1)
        print(win)
        time.sleep(2)
            
        latest_players = []
        for n in range(010):
            get_latest_match_id = requests.get("https://kr.api.riotgames.com/lol/match/v4/matchlists/by-account/" + summoner_account_id, headers=request_headers).json()["matches"][0]["gameId"]
            get_match_info = requests.get("https://kr.api.riotgames.com/lol/match/v4/matches/" + str(get_latest_match_id), headers=request_headers).json()
            latest_players.append(get_match_info["participantIdentities"][n]["player"]["summonerName"])
            time.sleep(1)
        print(latest_players)
        if len(set(current_players) & set(latest_players)) > 1:
            print(set(current_players) & set(latest_players))
        else:
            print("듀오가 없습니다")
        time.sleep(2)
 
check_my_team("내 팀원1","내 팀원2","내 팀원3""내 팀원4")
 
def check_members():
    print("시작")
    live_game = requests.get("https://kr.api.riotgames.com/lol/spectator/v4/active-games/by-summoner/E1AfcxGWK9MaVl7tn6uMcdFNLEFKh3gx3cAEhrm4Wq0iDQ", headers=request_headers)
    live_game = live_game.json()
    current_players = []
    print("진행중")
    print(live_game)
    for n in range(010):
        print("??")
        name = parse.quote(live_game["participants"][n]["summonerName"])
        print(name)
        current_players.append(name)
        summoner_account_id = requests.get("https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/" + name, headers=request_headers).json()["accountId"]
        get_latest_match_id = requests.get("https://kr.api.riotgames.com/lol/match/v4/matchlists/by-account/" + summoner_account_id, headers=request_headers).json()["matches"][0]["gameId"]
        get_match_info = requests.get("https://kr.api.riotgames.com/lol/match/v4/matches/" + str(get_latest_match_id), headers=request_headers).json()
        latest_players = []
        for n in range(010):
            latest_players.append(get_match_info["participantIdentities"][n]["player"]["summonerName"])
            if len(set(current_players) & set(latest_players)) > 1:
                print(set(current_players) & set(latest_players))
        time.sleep(2)
 
check_members()
 
 
 
 
cs

 

+ Recent posts