Skip to content

Commit

Permalink
Feat: API 데이터 조회형식으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hoon committed Mar 18, 2024
1 parent 538dd99 commit d677244
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
25 changes: 25 additions & 0 deletions src/app/basketball_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from basketball_reference_web_scraper import client
from basketball_reference_web_scraper.data import OutputType
from datetime import date
import json

class BasketballStats:
def __init__(self, client, output_format, current_date):
self.client = client
self.output_format = output_format
self.current_date = current_date

def daily_player_stats(self):
daily_stats_raw = self.client.player_box_scores(
day=self.current_date.day,
month=self.current_date.month,
year=self.current_date.year,
output_type=self.output_format
)

return json.loads(daily_stats_raw)

if __name__ == "__main__":
stats_scraper = BasketballStats(client, OutputType.JSON, date.today())
daily_stats = stats_scraper.scrape_daily_player_stats()
print(daily_stats)
28 changes: 0 additions & 28 deletions src/app/data_scraper.py

This file was deleted.

23 changes: 14 additions & 9 deletions src/app/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flask import jsonify, Blueprint
from .data_scraper import DataScraper
from flask import request, jsonify, Blueprint
from .basketball_stats import BasketballStats
from basketball_reference_web_scraper import client
from basketball_reference_web_scraper.data import OutputType
from datetime import date
Expand All @@ -8,12 +8,17 @@


@bp.route('/')
def home():
return jsonify({'message': 'Hello, Flask!'})
async def welcome():
"""Endpoint to check if the API is running."""
return jsonify({'message': 'Welcome to the Basketball Stats API!'})

@bp.route('/data_collect', methods=['GET'])
async def bsketball_data_scrape():
data_scraper = DataScraper(client, OutputType.JSON, date.today())
data = data_scraper.data_scrap()
@bp.route('/basketball/stats', methods=['GET'])
async def get_daily_player_stats():
"""Endpoint to retrieve basketball player stats for a given date."""
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)
daily_stats = stats_scraper.daily_player_stats()

return jsonify(data)
return jsonify(daily_stats)

0 comments on commit d677244

Please sign in to comment.