Skip to content

Commit

Permalink
Feat: 팀 시즌 레코드 조회 기능(특정 연도 및 특정팀 조회가능)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoon committed Mar 18, 2024
1 parent 13f8e63 commit 591bb99
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
41 changes: 38 additions & 3 deletions src/app/basketball_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,50 @@ def specific_player_stats(self, player_name):
all_players_stats = self.daily_player_stats()
return [stat for stat in all_players_stats if player_name in stat['name']]


def specific_team_stats(self, team_name):
all_teams_stats = self.daily_team_stats()
return [stat for stat in all_teams_stats if team_name in stat['team']]

def season_standings(self):
standings_raw = self.client.standings(
season_end_year=self.current_date.year,
output_type=self.output_format
)
return json.loads(standings_raw)

def team_season_record(self, team_name):
standings = self.season_standings()
print(team_name)
for record in standings:
if team_name.lower() in record['team'].lower():
return record
return {}

if __name__ == "__main__":
stats_scraper = BasketballStats(client, OutputType.JSON, date.today())

# 일일 선수 통계
player_daily_stats = stats_scraper.daily_player_stats()
print("Player Stats:", player_daily_stats)
print("오늘의 선수 통계:", player_daily_stats)

# 일일 팀 통계
team_daily_stats = stats_scraper.daily_team_stats()
print("Team Stats:", team_daily_stats)
print("오늘의 팀 통계:", team_daily_stats)

# 특정 선수 통계
# 예시로 "LeBron James" 사용 (실제 이름은 데이터에 따라 다를 수 있음)
specific_player_stats = stats_scraper.specific_player_stats("LeBron James")
print("LeBron James의 통계:", specific_player_stats)

# 특정 팀 통계
# 예시로 "ATLANTA HAWKS" 사용 (실제 이름은 데이터에 따라 다를 수 있음)
specific_team_stats = stats_scraper.specific_team_stats("ATLANTA HAWKS")
print("ATLANTA HAWKS의 통계:", specific_team_stats)

# 시즌 순위
season_standings = stats_scraper.season_standings(2019)
print("2019 시즌 순위:", season_standings)

# 특정 팀의 시즌 기록
lakers_record = stats_scraper.team_season_record("ATLANTA HAWKS", 2019)
print("ATLANTA HAWKS 2019 기록:", lakers_record)
46 changes: 39 additions & 7 deletions src/app/routes.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from flask import request, jsonify, Blueprint
from .basketball_stats import BasketballStats
from .basketball_stats import BasketballStats # ExtendedBasketballStats로 변경 가능
from basketball_reference_web_scraper import client
from basketball_reference_web_scraper.data import OutputType
from datetime import date
import re

bp = Blueprint('main', __name__)


@bp.route('/')
async def welcome():
"""Endpoint to check if the API is running."""
return jsonify({'message': 'Welcome to the Basketball Stats API!'})
"""API 가동 상태 확인 엔드포인트."""
return jsonify({'message': 'Basketball Stats API에 오신 것을 환영합니다!'})

@bp.route('/basketball/player-stats', methods=['GET'])
async def get_player_stats():

query_date_str = request.args.get('date', date.today().isoformat())
player_name = request.args.get('player', None)
query_date = date.fromisoformat(query_date_str)
Expand All @@ -29,12 +28,45 @@ async def get_player_stats():
@bp.route('/basketball/team-stats', methods=['GET'])
async def get_team_stats():
query_date_str = request.args.get('date', date.today().isoformat())
team_name = request.args.get('team', None)
team_name = request.args.get('team', None)
query_date = date.fromisoformat(query_date_str)

stats_scraper = BasketballStats(client, OutputType.JSON, query_date)
if team_name:
stats = stats_scraper.specific_team_stats(team_name)
else:
stats = stats_scraper.daily_team_stats()
return jsonify(stats)
return jsonify(stats)

# 시즌 순위 조회 엔드포인트 추가
@bp.route('/basketball/season-standings', methods=['GET'])
async def get_season_standings():
query_date_str = request.args.get('date', date.today().isoformat())
query_date = date.fromisoformat(query_date_str)

stats_scraper = BasketballStats(client, OutputType.JSON, query_date)
standings = stats_scraper.season_standings()
return jsonify(standings)

# 특정 팀의 시즌 기록 조회 엔드포인트 추가
@bp.route('/basketball/team-season-record', methods=['GET'])
async def get_team_season_record():
query_date_str = request.args.get('date', date.today().isoformat())
team_name = request.args.get('team')

date_pattern = re.compile(r'^\d{4}-\d{2}-\d{2}$')
if not date_pattern.match(query_date_str):
return jsonify({'error': 'Date must be in YYYY-MM-DD format'}), 400

try:
query_date = date.fromisoformat(query_date_str)
except ValueError:
# 형식은 맞지만 유효하지 않은 날짜 예: 2022-02-30
return jsonify({'error': 'Invalid date'}), 400

if not team_name:
return jsonify({'error': 'Team name is required'}), 400

stats_scraper = BasketballStats(client, OutputType.JSON, query_date)
record = stats_scraper.team_season_record(team_name)
return jsonify(record)

0 comments on commit 591bb99

Please sign in to comment.