MlbPlayers update
Add initial support for /mlbplayers
This commit is contained in:
parent
aca7d6df23
commit
79b59bb376
@ -1,10 +1,13 @@
|
||||
import datetime
|
||||
import random
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response, Query
|
||||
from typing import Optional, List
|
||||
import logging
|
||||
import pydantic
|
||||
from pandas import DataFrame
|
||||
|
||||
from ..db_engine import db, MlbPlayer, model_to_dict, fn, chunked, query_to_csv
|
||||
from ..db_engine import db, MlbPlayer, Player, BattingCard, PitchingCard, model_to_dict, fn, chunked, query_to_csv
|
||||
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
|
||||
|
||||
logging.basicConfig(
|
||||
@ -22,17 +25,35 @@ router = APIRouter(
|
||||
class PlayerModel(pydantic.BaseModel):
|
||||
first_name: str
|
||||
last_name: str
|
||||
key_mlbam: int
|
||||
key_fangraphs: int = None
|
||||
key_bbref: str = None
|
||||
key_retro: str = None
|
||||
key_mlbam: int = None
|
||||
offense_col: int = None
|
||||
offense_col: int = random.randint(1, 3)
|
||||
|
||||
|
||||
class PlayerList(pydantic.BaseModel):
|
||||
players: List[PlayerModel]
|
||||
|
||||
|
||||
def update_card_urls(mlbplayer: MlbPlayer):
|
||||
logging.info(f'Updating cards for mlbplayer: {mlbplayer.first_name} {mlbplayer.last_name} ({mlbplayer.key_bbref})')
|
||||
now = datetime.datetime.now()
|
||||
c1_update = Player.update({
|
||||
Player.image: Player.image.name.split('?d=')[0] + f'?d={now.year}-{now.month}-{now.day}'
|
||||
}).where(Player.mlbplayer_id == mlbplayer.id)
|
||||
count = c1_update.execute()
|
||||
logging.info(f'Updated {count} image1s')
|
||||
|
||||
c2_update = Player.update({
|
||||
Player.image2: Player.image2.name.split('?d=')[0] + f'{now.year}-{now.month}-{now.day}'
|
||||
}).where((Player.mlbplayer_id == mlbplayer.id) & (Player.image2.is_null(False)))
|
||||
count2 = c2_update.execute()
|
||||
logging.info(f'Updated {count2} image2s')
|
||||
|
||||
return count + count2
|
||||
|
||||
|
||||
@router.get('')
|
||||
async def get_players(
|
||||
full_name: list = Query(default=None), first_name: list = Query(default=None),
|
||||
@ -47,17 +68,19 @@ async def get_players(
|
||||
fn.lower(MlbPlayer.first_name) + ' ' + fn.lower(MlbPlayer.last_name) << name_list
|
||||
)
|
||||
if first_name is not None:
|
||||
all_players = all_players.where(MlbPlayer.first_name << first_name)
|
||||
if first_name is not None:
|
||||
all_players = all_players.where(MlbPlayer.first_name << first_name)
|
||||
name_list = [x.lower() for x in first_name]
|
||||
all_players = all_players.where(fn.lower(MlbPlayer.first_name) << name_list)
|
||||
if last_name is not None:
|
||||
all_players = all_players.where(MlbPlayer.last_name << last_name)
|
||||
name_list = [x.lower() for x in last_name]
|
||||
all_players = all_players.where(fn.lower(MlbPlayer.last_name) << name_list)
|
||||
if key_fangraphs is not None:
|
||||
all_players = all_players.where(MlbPlayer.key_fangraphs << key_fangraphs)
|
||||
if key_bbref is not None:
|
||||
all_players = all_players.where(MlbPlayer.key_bbref << key_bbref)
|
||||
name_list = [x.lower() for x in key_bbref]
|
||||
all_players = all_players.where(fn.lower(MlbPlayer.key_bbref) << name_list)
|
||||
if key_retro is not None:
|
||||
all_players = all_players.where(MlbPlayer.key_retro << key_retro)
|
||||
name_list = [x.lower() for x in key_retro]
|
||||
all_players = all_players.where(fn.lower(MlbPlayer.key_retro) << name_list)
|
||||
if key_mlbam is not None:
|
||||
all_players = all_players.where(MlbPlayer.key_mlbam << key_mlbam)
|
||||
if offense_col is not None:
|
||||
@ -187,3 +210,79 @@ async def delete_player(player_id: int, token: str = Depends(oauth2_scheme)):
|
||||
raise HTTPException(status_code=200, detail=f'Player {player_id} has been deleted')
|
||||
else:
|
||||
raise HTTPException(status_code=500, detail=f'Player {player_id} was not deleted')
|
||||
|
||||
|
||||
@router.post('/update-cols')
|
||||
@router.post('/update-cols/{mlbplayer_id}')
|
||||
async def update_columns(mlbplayer_id: Optional[int] = None, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'Bad Token: {token}')
|
||||
db.close()
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail='You are not authorized to update mlb players. This event has been logged.'
|
||||
)
|
||||
|
||||
p_query = MlbPlayer.select()
|
||||
if mlbplayer_id is not None:
|
||||
p_query = p_query.where(MlbPlayer.id == mlbplayer_id)
|
||||
|
||||
total_count = 0
|
||||
for x in p_query:
|
||||
all_players = Player.select().where(Player.mlbplayer == x)
|
||||
bc_update = BattingCard.update({
|
||||
BattingCard.offense_col: x.offense_col,
|
||||
}).where((BattingCard.player << all_players) & (BattingCard.offense_col != x.offense_col))
|
||||
count = bc_update.execute()
|
||||
total_count += count
|
||||
logging.info(f'Updated {count} batting cards for {x.first_name} {x.last_name}')
|
||||
update_card_urls(x)
|
||||
|
||||
db.close()
|
||||
return f'Updated {total_count} batting cards'
|
||||
|
||||
|
||||
@router.post('/update-names')
|
||||
@router.post('/update-names/{mlbplayer_id}')
|
||||
async def update_names(mlbplayer_id: Optional[int] = None, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'Bad Token: {token}')
|
||||
db.close()
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail='You are not authorized to update mlb players. This event has been logged.'
|
||||
)
|
||||
|
||||
p_query = MlbPlayer.select()
|
||||
if mlbplayer_id is not None:
|
||||
p_query = p_query.where(MlbPlayer.id == mlbplayer_id)
|
||||
|
||||
total_count = 0
|
||||
for x in p_query:
|
||||
p_update = Player.update({
|
||||
Player.p_name: f'{x.first_name} {x.last_name}'
|
||||
}).where((Player.mlbplayer == x) & (Player.p_name != f'{x.first_name} {x.last_name}'))
|
||||
count = p_update.execute()
|
||||
total_count += count
|
||||
logging.info(f'Update {count} player records for {x.first_name} {x.last_name}')
|
||||
update_card_urls(x)
|
||||
|
||||
db.close()
|
||||
return f'Updated {total_count} names'
|
||||
|
||||
|
||||
# @router.post('/link-players')
|
||||
# async def post_players(token: str = Depends(oauth2_scheme)):
|
||||
# if not valid_token(token):
|
||||
# logging.warning(f'Bad Token: {token}')
|
||||
# db.close()
|
||||
# raise HTTPException(
|
||||
# status_code=401,
|
||||
# detail='You are not authorized to link mlb players. This event has been logged.'
|
||||
# )
|
||||
#
|
||||
# for x in Player.select().where(Player.mlbplayer.is_null()):
|
||||
# p_query = MlbPlayer.get_or_none(MlbPlayer.key_bbref == x.key_bbref)
|
||||
# if p_query is not None:
|
||||
# x.mlbplayer = p_query
|
||||
# x.save()
|
||||
|
||||
@ -14,7 +14,7 @@ from pandas import DataFrame
|
||||
|
||||
from ..card_creation import get_batter_card_data, get_pitcher_card_data
|
||||
from ..db_engine import db, Player, model_to_dict, fn, chunked, Paperdex, Cardset, Rarity, BattingCard, \
|
||||
BattingCardRatings, PitchingCard, PitchingCardRatings, CardPosition
|
||||
BattingCardRatings, PitchingCard, PitchingCardRatings, CardPosition, MlbPlayer
|
||||
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
|
||||
|
||||
logging.basicConfig(
|
||||
@ -58,6 +58,7 @@ class PlayerPydantic(pydantic.BaseModel):
|
||||
fangr_id: Optional[str] = None
|
||||
description: str
|
||||
quantity: Optional[int] = 999
|
||||
mlbplayer_id: Optional[int] = None
|
||||
|
||||
|
||||
class PlayerModel(pydantic.BaseModel):
|
||||
@ -73,7 +74,8 @@ async def get_players(
|
||||
has_vanity_card: Optional[bool] = None, strat_code: Optional[str] = None, bbref_id: Optional[str] = None,
|
||||
fangr_id: Optional[str] = None, inc_dex: Optional[bool] = True, in_desc: Optional[str] = None,
|
||||
flat: Optional[bool] = False, sort_by: Optional[str] = False, cardset_id_exclude: list = Query(default=None),
|
||||
limit: Optional[int] = None, csv: Optional[bool] = None, short_output: Optional[bool] = False):
|
||||
limit: Optional[int] = None, csv: Optional[bool] = None, short_output: Optional[bool] = False, mlbplayer_id: Optional[int] = None,
|
||||
inc_keys: Optional[bool] = False):
|
||||
all_players = Player.select()
|
||||
if all_players.count() == 0:
|
||||
db.close()
|
||||
@ -115,6 +117,8 @@ async def get_players(
|
||||
all_players = all_players.where(Player.bbref_id == bbref_id)
|
||||
if fangr_id is not None:
|
||||
all_players = all_players.where(Player.fangr_id == fangr_id)
|
||||
if mlbplayer_id is not None:
|
||||
all_players = all_players.where(Player.mlbplayer_id == mlbplayer_id)
|
||||
if in_desc is not None:
|
||||
all_players = all_players.where(fn.Lower(Player.description).contains(in_desc.lower()))
|
||||
|
||||
@ -199,6 +203,15 @@ async def get_players(
|
||||
for y in this_dex:
|
||||
this_record['paperdex']['paperdex'].append(model_to_dict(y, recurse=False))
|
||||
|
||||
if inc_keys and (flat or short_output):
|
||||
if this_record['mlbplayer'] is not None:
|
||||
this_mlb = MlbPlayer.get_by_id(this_record['mlbplayer'])
|
||||
this_record['key_mlbam'] = this_mlb.key_mlbam
|
||||
this_record['key_fangraphs'] = this_mlb.key_fangraphs
|
||||
this_record['key_bbref'] = this_mlb.key_bbref
|
||||
this_record['key_retro'] = this_mlb.key_retro
|
||||
this_record['offense_col'] = this_mlb.offense_col
|
||||
|
||||
return_val['players'].append(this_record)
|
||||
|
||||
# return_val['players'].append(model_to_dict(x, recurse=not flat))
|
||||
@ -467,7 +480,7 @@ async def v1_players_patch(
|
||||
player_id, name: Optional[str] = None, image: Optional[str] = None, image2: Optional[str] = None,
|
||||
mlbclub: Optional[str] = None, franchise: Optional[str] = None, cardset_id: Optional[int] = None,
|
||||
rarity_id: Optional[int] = None, pos_1: Optional[str] = None, pos_2: Optional[str] = None,
|
||||
pos_3: Optional[str] = None, pos_4: Optional[str] = None, pos_5: Optional[str] = None,
|
||||
pos_3: Optional[str] = None, pos_4: Optional[str] = None, pos_5: Optional[str] = None, mlbplayer_id: Optional[int] = None,
|
||||
pos_6: Optional[str] = None, pos_7: Optional[str] = None, pos_8: Optional[str] = None,
|
||||
headshot: Optional[str] = None, vanity_card: Optional[str] = None, strat_code: Optional[str] = None,
|
||||
bbref_id: Optional[str] = None, description: Optional[str] = None, cost: Optional[int] = None,
|
||||
@ -567,6 +580,8 @@ async def v1_players_patch(
|
||||
this_player.fangr_id = fangr_id
|
||||
if description is not None:
|
||||
this_player.description = description
|
||||
if mlbplayer_id is not None:
|
||||
this_player.mlbplayer_id = mlbplayer_id
|
||||
|
||||
if this_player.save() == 1:
|
||||
return_val = model_to_dict(this_player)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user