[Python] Lol API 이용해보기
1. API 키 발급받기
API 키는 24시간 용으로 하루에 한번씩 발급받아야 한다. 아니면 개발용 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
2. API Document 보기
https://developer.riotgames.com/apis
Riot Developer Portal
developer.riotgames.com
API 도큐먼트는 여기서 볼수 있는데, 웹에서 바로 API 테스트가 가능해서 편하다. 발로란트, TFT도 검색 가능하다.
이렇게 하고 있다.
import requests
from urllib import parse
API_KEY = 'RGAPI-######-####-####-####-###########'
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": "{}".format(API_KEY)
}
BASE_URL = "https://kr.api.riotgames.com"
ASIA_URL = "https://asia.api.riotgames.com"
def get_request(url, region=None) :
if region == 'asia' :
request_base_url = ASIA_URL
else :
request_base_url = BASE_URL
result = requests.get(request_base_url + url,
headers=request_headers).json()
return result
일단 계획은
1.
https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/{}
{}안에다 소환사 이름을 넣고 요청하면 요청이 된다. 안되는 경우 특히 unautorized가 뜨는 경우 header 설정을 잘 했는지 봐야한다. header 안에 x-Riot-token 이라는 곳에 발급받은 API 키가 똑같이 들어간다.
1. API 키를 넣는 테이블 생성
2. 소환사 명, id, accountId, puuid를 저장하는 테이블 생성 후, 시간 마다 업데이트(닉변검사)
3. Match 저장(테이블이 커질것 같으니 이건 보류)
4. active-games 검사. Status 404 message : 'Data not found'가 나올 경우 현재 접속해 있지 않은거임.