import requests
import sqlite3
api_key = 'RGAPI-b219ef65-e867-46dc-9520-f7d8a5c5e7d6'
summoner_name = input("전적을 검색할 소환사 이름을 입력하세요: ")
conn = sqlite3.connect("lol.db")
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS summoners (
id TEXT PRIMARY KEY,
puuid TEXT,
name TEXT,
profile_icon_id INTEGER,
summoner_level INTEGER,
queue_type TEXT,
tier TEXT,
rank TEXT,
league_points INTEGER,
wins INTEGER,
losses INTEGER
)
''')
def get_summoner_info(summoner_name):
base_url = 'https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/'
url = f'{base_url}{summoner_name}?api_key={api_key}'
response = requests.get(url)
if response.status_code == 200:
summoner_info = response.json()
return summoner_info
else:
print('소환사 정보를 가져오는데 실패했습니다.')
return None
def get_summoner_rank(summoner_id):
base_url = f'https://kr.api.riotgames.com/lol/league/v4/entries/by-summoner/{summoner_id}?api_key={api_key}'
response = requests.get(base_url)
if response.status_code == 200:
summoner_rank = response.json()
return summoner_rank
else:
print('소환사 랭크 정보를 가져오는데 실패했습니다.')
return None
def save_summoner_info():
print(f'소환사 이름: {summoner_info["name"]}')
print(f'소환사 레벨: {summoner_info["summonerLevel"]}')
summoner_id = summoner_info['id']
summoner_puuid = summoner_info['puuid']
summoner_name = summoner_info["name"]
summoner_icon = summoner_info["profileIconId"]
summoner_level = summoner_info["summonerLevel"]
cursor.execute('SELECT * FROM summoners WHERE id=?', (summoner_id,))
exsining_data = cursor.fetchone()
if exsining_data:
return summoner_id
else:
cursor.execute('''
INSERT INTO summoners(
id,
puuid,
name,
profile_icon_id,
summoner_level
) VALUES(?, ?, ?, ?, ?)
''', (summoner_id, summoner_puuid, summoner_name, summoner_icon, summoner_level))
print("입력하신 소환사님의 정보를 검색중입니다...")
return summoner_id
def save_summoner_rank():
summoner_rank = get_summoner_rank(summoner_id)
for entry in summoner_rank:
queue_type = entry["queueType"]
tier = entry["tier"]
rank = entry["rank"]
league_points = entry["leaguePoints"]
wins = entry["wins"]
losses = entry["losses"]
cursor.execute('SELECT * FROM summoners WHERE id=?', (summoner_id,))
existing_data = cursor.fetchone()
if existing_data:
cursor.execute('''
UPDATE summoners
SET queue_type=?, tier=?, rank=?, league_points=?, wins=?, losses=?
WHERE id=?
''', (queue_type, tier, rank, league_points, wins, losses, summoner_id))
print(f'{queue_type} 랭크 정보가 업데이트되었습니다.')
else:
cursor.execute('''
INSERT INTO summoners (
queue_type
tier,
rank,
league_points,
wins,
losses
) VALUES (?, ?, ?, ?, ?, ?, ?)
''', (summoner_info["profileIconId"], queue_type, tier, rank, league_points, wins, losses))
print(f'{queue_type} 랭크 정보가 저장되었습니다.')
conn.commit()
summoner_info = get_summoner_info(summoner_name)
if summoner_info:
summoner_id = save_summoner_info()
if summoner_id:
save_summoner_rank()
conn.close()
else:
print('입력하신 소환사 정보를 찾을 수 없습니다.')