Riot API - Match
지금까지 우리의 최종 목표는 최상위 유저들의 게임 기록을 크롤링(crawling)하는 것입니다.
그런데 라이엇은 이러한 기능을 직접적으로 제공하지 않습니다. 최상위 유저들의 게임 기록을 가져오기 위해 우리는
1) 최상위 리그 (마스터, 그랜드마스터, 챌린져)의 리그 정보 중 소환사 ID 수집
2) 소환사 ID를 통해 계정 ID 수집
3) 계정 ID를 통해 각 소환사의 최근 매치 ID 수집
4) 최근 매치 ID를 통해 최근 매치 정보 수집
의 4단계를 거쳐야 합니다.
위에서 설명한 3번과 4번 과정을 진행해봅시다.
LolMaster 폴더 안에 match.py를 생성합니다.
먼저, 계정 ID를 이용해 최근 match list를 가져옵시다. 해당 API document는 다음과 같습니다.
https://developer.riotgames.com/apis#match-v4/GET_getMatchlist
Riot Developer Portal
developer.riotgames.com
최근 match list를 가져오는 API는 query를 다양하게 넣을 수 있습니다. 내가 원하는 챔피언ID(champion_id), 게임 종류(queue_id), 시즌(season), 검색을 시작할 시점(beginTime), 검색할 마지막 시점(endTime) 5종류나 되죠.
검색하기를 원하는 account_id와 위의 5종류의 query를 함수 argument로 받아오는 get_list_by_account 함수를 짭니다.
from typing import Union, List
from LolMaster.api import RiotURL
from pandas import Series, DataFrame, json_normalize
def get_list_by_account(account_ids: Union[str, Series],
champion_ids: Union[None, List[int]] = None,
queue_ids: Union[None, List[int]] = None,
seasons: Union[None, List[int]] = None,
begin_time: Union[None, int] = None,
end_time: Union[None, int] = None):
if champion_ids is None:
champion_ids = []
if queue_ids is None:
queue_ids = []
if seasons is None:
seasons = []
if type(account_ids) == str:
account_ids = Series(account_ids)
df = DataFrame()
for account_id in account_ids:
url = RiotURL('/lol/match/v4/matchlists/by-account/' + format(account_id))
url.set_query('champion', champion_ids) \
.set_query('queue', queue_ids) \
.set_query('season', seasons)
if begin_time is not None:
url.set_query('beginTime', str(begin_time))
if end_time is not None:
url.set_query('endTime', str(end_time))
res = url.request()
if res is not None:
df = df.append(json_normalize(res['matches']))
return df
참고로, begin_time과 end_time의 설정에 따라 해당 기간 동안 소환사 게임 기록이 없을 수도 있습니다. 이 때는 request()함수 내부에서 response code가 404가 되면서 None을 return하겠죠? 적절하게 처리해줍니다.
이제 우리는 챌린져 유저의 게임 ID 리스트를 가져올 수 있습니다. 이제 게임 ID를 통해 실제 매치 기록을 가져오는 get_match_by_match_id 함수를 짜봅시다.
def get_match_by_match_id(match_ids: Union[str, Series]):
if type(match_ids) == str:
match_ids = Series(match_ids)
match_ids.drop_duplicates(inplace=True)
df = DataFrame()
for match_id in match_ids:
url = RiotURL('/lol/match/v4/matches/' + format(match_id))
res = url.request()
if res is not None:
df = df.append(json_normalize(res))
return df
이제 우리는 모든 챌린저 유저가 최근에 플레이한 게임 세부 기록을 가져올 수 있습니다!
from LolMaster import league, summoner
challenger = league.get_summoner_challenger()
challenger_users = summoner.get_summoner_by_summoner_id(challenger['summonerId'])
match_ids = match.get_list_by_account(challenger_users['accountId'], begin_time=begin, end_time=end)
matches = match.get_match_by_match_id(match_ids['gameId'])
수고하셨습니다. 다음 글에서는 하루 한 번씩 주기적으로 상위권 게임을 크롤링하는 코드를 짜봅시다.