Add csv support for /players
This commit is contained in:
parent
8f53d83d9e
commit
1759932643
@ -1,10 +1,13 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
import logging
|
import logging
|
||||||
import pydantic
|
import pydantic
|
||||||
|
from pandas import DataFrame
|
||||||
|
|
||||||
from ..db_engine import db, Player, model_to_dict, chunked
|
from ..db_engine import db, Player, model_to_dict, chunked
|
||||||
from ..dependencies import oauth2_scheme, valid_token
|
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
|
||||||
|
|
||||||
|
logging.basicConfig(filename=LOG_DATA['filename'], format=LOG_DATA['format'], level=LOG_DATA['log_level'])
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
prefix='/api/v3/players',
|
prefix='/api/v3/players',
|
||||||
@ -44,7 +47,8 @@ class PlayerList(pydantic.BaseModel):
|
|||||||
@router.get('')
|
@router.get('')
|
||||||
async def get_players(
|
async def get_players(
|
||||||
season: Optional[int], team_id: list = Query(default=None), pos: list = Query(default=None),
|
season: Optional[int], team_id: list = Query(default=None), pos: list = Query(default=None),
|
||||||
is_injured: Optional[bool] = None, sort: Optional[str] = None):
|
is_injured: Optional[bool] = None, sort: Optional[str] = None, short_output: Optional[bool] = False,
|
||||||
|
csv: Optional[bool] = False):
|
||||||
all_players = Player.select_season(season)
|
all_players = Player.select_season(season)
|
||||||
|
|
||||||
if team_id is not None:
|
if team_id is not None:
|
||||||
@ -70,10 +74,36 @@ async def get_players(
|
|||||||
elif sort == 'name-desc':
|
elif sort == 'name-desc':
|
||||||
all_players = all_players.order_by(-Player.name)
|
all_players = all_players.order_by(-Player.name)
|
||||||
|
|
||||||
return_players = {
|
print(f'csv: {csv}')
|
||||||
'count': all_players.count(),
|
if csv:
|
||||||
'players': [model_to_dict(x, recurse=False) for x in all_players]
|
player_list = [
|
||||||
}
|
['name', 'wara', 'image', 'image2', 'team', 'season', 'pitcher_injury', 'pos_1', 'pos_2', 'pos_3',
|
||||||
|
'pos_4', 'pos_5', 'pos_6', 'pos_7', 'pos_8', 'last_game', 'last_game2', 'il_return', 'demotion_week',
|
||||||
|
'headshot', 'vanity_card', 'strat_code', 'bbref_id', 'injury_rating']
|
||||||
|
]
|
||||||
|
for line in all_players:
|
||||||
|
player_list.append(
|
||||||
|
[
|
||||||
|
line.name, line.wara, line.image, line.image2, line.team.abbrev, line.season, line.pitcher_injury,
|
||||||
|
line.pos_1, line.pos_2, line.pos_3, line.pos_4, line.pos_5, line.pos_6, line.pos_7, line.pos_8,
|
||||||
|
line.last_game, line.last_game2, line.il_return, line.demotion_week, line.headshot,
|
||||||
|
line.vanity_card, line.strat_code.replace(",", "-_-"), line.bbref_id, line.injury_rating
|
||||||
|
]
|
||||||
|
)
|
||||||
|
return_players = {
|
||||||
|
'count': all_players.count(),
|
||||||
|
'players': DataFrame(player_list).to_csv(header=False, index=False),
|
||||||
|
'csv': True
|
||||||
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
return Response(content=return_players['players'], media_type='text/csv')
|
||||||
|
|
||||||
|
else:
|
||||||
|
return_players = {
|
||||||
|
'count': all_players.count(),
|
||||||
|
'players': [model_to_dict(x, recurse=not short_output) for x in all_players]
|
||||||
|
}
|
||||||
db.close()
|
db.close()
|
||||||
return return_players
|
return return_players
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user