flask๋ฅผ ํ์ฉํด ๋ฐฑ์ค๋ ๊ฐ๋ฐ
from flask import Flask, request, jsonify, render_template
import mysql.connector
import requests
app = Flask(__name__)
# MySQL ์ฐ๊ฒฐ ์ค์
def get_db_connection():
return mysql.connector.connect(
host="localhost",
user='root',
password='pw',
database='riot_game_db'
)
# ๋ผ์ด์ API URL ์ค์
RIOT_API_KEY = "my_api"
RIOT_API_URL = "https://asia.api.riotgames.com"
# ๋ฉ์ธ ํ์ด์ง - ์ ์ ๊ฒ์ ํผ์ ๋ณด์ฌ์ค
@app.route('/')
def index():
return render_template('index.html')
# ๋๋ค์ ๊ฒ์ ๋ฐ ๊ฒ์ ์ ๋ณด ์กฐํ ํจ์
@app.route('/search', methods=['POST'])
def search_user():
summoner_name = request.form.get('summoner_name')
if not summoner_name:
return render_template('index.html', error="๋๋ค์์ ์
๋ ฅํด์ฃผ์ธ์")
# MySQL ์ฐ๊ฒฐ
connection = get_db_connection()
cursor = connection.cursor(dictionary=True)
try:
# 1. duplicated_GM_users ํ
์ด๋ธ์์ puuid ์กฐํ
cursor.execute("SELECT puuid FROM duplicated_GM_users WHERE summoner_name = %s", (summoner_name,))
result = cursor.fetchone()
if result:
puuid = result['puuid']
# 2. ๋ผ์ด์ API๋ฅผ ์ฌ์ฉํ์ฌ ์ต๊ทผ 5ํ์ ๊ฒ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
match_ids = get_recent_matches(puuid, 5)
if not match_ids:
return render_template('index.html', error="๊ฒ์ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
# 3. ๋งค์น๋ณ ์์ธ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๊ธฐ
games_info = []
for match_id in match_ids:
match_info = get_match_info(match_id)
if match_info:
games_info.append(match_info)
# DB ์ฐ๊ฒฐ ํด์
cursor.close()
connection.close()
# 4. ๊ฒฐ๊ณผ๋ฅผ HTML ํ์ด์ง์ ์ ๋ฌ
return render_template('results.html', games_info=games_info, summoner_name=summoner_name)
else:
cursor.close()
connection.close()
return render_template('index.html', error="์ ์ ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค")
except mysql.connector.Error as err:
cursor.close()
connection.close()
return render_template('index.html', error=f"MySQL ์ค๋ฅ ๋ฐ์: {err}")
except Exception as e:
cursor.close()
connection.close()
return render_template('index.html', error=f"์ค๋ฅ ๋ฐ์: {e}")
# ์ต๊ทผ ๋งค์น ID๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์
def get_recent_matches(puuid, count):
url = f"{RIOT_API_URL}/lol/match/v5/matches/by-puuid/{puuid}/ids?start=0&count={count}&api_key={RIOT_API_KEY}"
try:
response = requests.get(url)
response.raise_for_status() # ์ํ ์ฝ๋๊ฐ 200์ด ์๋๋ฉด ์์ธ ๋ฐ์
if response.status_code == 200:
return response.json() # ๋งค์น ID ๋ฆฌ์คํธ ๋ฐํ
elif response.status_code == 404:
print("๋งค์น ID๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
return []
elif response.status_code == 429:
print("API ์์ฒญ ์ ํ์ด ์ด๊ณผ๋์์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํ์ธ์.")
return []
else:
print(f"์ ์ ์๋ ์ค๋ฅ ๋ฐ์: ์ํ ์ฝ๋ {response.status_code}")
return []
except requests.exceptions.RequestException as e:
print(f"API ์์ฒญ ์ค๋ฅ: {e}")
return []
# ํน์ ๋งค์น์ ๊ฒ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์
def get_match_info(match_id):
url = f"{RIOT_API_URL}/lol/match/v5/matches/{match_id}?api_key={RIOT_API_KEY}"
try:
response = requests.get(url)
response.raise_for_status() # ์ํ ์ฝ๋๊ฐ 200์ด ์๋๋ฉด ์์ธ ๋ฐ์
if response.status_code == 200:
match_data = response.json()
return extract_player_data(match_data)
else:
print(f"๋งค์น ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ ์คํจ, ์ํ ์ฝ๋: {response.status_code}")
return None
except requests.exceptions.RequestException as e:
print(f"API ์์ฒญ ์ค๋ฅ: {e}")
return None
# ํ๋ ์ด์ด ์ ๋ณด ์ถ์ถ (๊ฒ์ ์ ๋ณด์์ ํ์ํ ๋ฐ์ดํฐ๋ง ๊ฐ์ ธ์ค๊ธฐ)
def extract_player_data(match_data):
for participant in match_data['info']['participants']:
player_info = {
'kills': participant['kills'],
'deaths': participant['deaths'],
'gold_earned': participant['goldEarned'],
'wards_placed': participant['wardsPlaced'],
'wards_bought': participant['visionWardsBoughtInGame'],
'css': participant['totalMinionsKilled'],
'assists': participant['assists'],
'kda': participant['challenges']['kda'] if 'challenges' in participant else None,
'teamPosition': participant['teamPosition']
}
return player_info
if __name__ == '__main__':
app.run(debug=True)
์ฝ๋์ ๋ํ ์ค๋ช ์ ์ด์ ๊ธ(7)์ ๋์์์ํ ๋ ํ์ธํด์ฃผ์๋ฉด ๋ฉ๋๋ค!
๊ฒ์ํ ํ์ด์ง HTML ์์ฑ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Game Stats Search</title>
</head>
<body>
<h1>์ํ์ฌ ๋๋ค์์ผ๋ก ๊ฒ์ ์ ๋ณด ๊ฒ์</h1>
<form action="/search" method="post">
<label for="summoner_name">๋๋ค์:</label>
<input type="text" id="summoner_name" name="summoner_name" required>
<button type="submit">๊ฒ์</button>
</form>
{% if error %}
<p style="color:red;">{{ error }}</p>
{% endif %}
</body>
</html>
๊ฒ์๊ฒฐ๊ณผ ํ์ด์ง HTML ์์ฑ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ summoner_name }}์ ์ต๊ทผ 5ํ ๊ฒ์ ์ ๋ณด</title>
</head>
<body>
<h1>{{ summoner_name }}์ ์ต๊ทผ 5ํ ๊ฒ์ ์ ๋ณด</h1>
{% if games_info %}
<table border="1">
<thead>
<tr>
<th>ํฌ</th>
<th>๋ฐ์ค</th>
<th>๊ณจ๋ ํ๋</th>
<th>์๋ ๋ฐฐ์น</th>
<th>์๋ ๊ตฌ๋งค</th>
<th>CS</th>
<th>์ด์์คํธ</th>
<th>KDA</th>
<th>ํ ํฌ์ง์
</th>
</tr>
</thead>
<tbody>
{% for game in games_info %}
<tr>
<td>{{ game.kills }}</td>
<td>{{ game.deaths }}</td>
<td>{{ game.gold_earned }}</td>
<td>{{ game.wards_placed }}</td>
<td>{{ game.wards_bought }}</td>
<td>{{ game.css }}</td>
<td>{{ game.assists }}</td>
<td>{{ game.kda }}</td>
<td>{{ game.teamPosition }}</td>
</tr>
{% endfor %} <!-- ๋ซํ -->
</tbody>
</table>
{% else %}
<p>๊ฒ์ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.</p>
{% endif %}
<a href="/">๋ค๋ฅธ ์ ์ ๊ฒ์</a>
</body>
</html>
์์ฑ๋ ์ฌ์ดํธ์ ์ ์ ๋๋ค์ ๊ฒ์ํ๊ธฐ
ํ์ด์ปค๋์ ๋๋ค์์ผ๋ก ๊ฒ์ํด๋ดค์ต๋๋ค!
์ ๋์ต๋๋ค~!
์ด์ DB์ ์๋ ์๋ฃ๋ฅผ ํ๋ธ๋ก์ ์ฐ๋ํด์ ์๊ฐํ ํ ๋ค์ ์นํ์ด์ง์ ๊ตฌํํ๊ฒ ์ต๋๋ค!