def test_xref(self): q = """SELECT nbacom_player_id, display_first_last FROM players WHERE nbacom_player_id IN (SELECT DISTINCT nbacom_player_id FROM player_gamelogs WHERE game_date > '2014-10-01') """ players = {p['display_first_last']:p['nbacom_player_id'] for p in self.db.select_dict(q)} dkp = list(csv_to_dict('/home/sansbacon/dkxref.csv')) for idx, p in enumerate(dkp): nm = match_player(p['source_player_name'], players.keys()) pid = players.get(nm) if pid: dkp[idx]['nbacom_player_id'] = pid self.db._insert_dict(dkp[idx], 'player_xref') else: logging.info('no match for {}'.format(p))
def update_player_xref(self): ''' Adds missing players to player_xref table and updates dfs_salaries afterwards ''' self.db.update(update_dfs_salaries_ids()) missing = self.db.select_dict( missing_salaries_ids(source='fantasylabs')) if missing: nbapq = """SELECT nbacom_player_id as id, display_first_last as n FROM players""" nbadict = {} nbacount = defaultdict(int) for p in self.db.select_dict(nbapq): nbadict[p['id']] = p['n'] nbacount[p['n']] += 1 # loop through missing players # filter out players with duplicate names - need to manually resolve those # then look for direct match where name is not duplicated # then try to match using names insq = """INSERT INTO player_xref (nbacom_player_id, source, source_player_id, source_player_name) VALUES ({}, 'fantasylabs', {}, '{}');""" for p in missing: if nbacount[p['n']] > 1: logging.error('need to manually resolve {}'.format(p)) continue match = [k for k, v in listitems(nbadict) if v == p['n']] if match: self.db.update(insq.format(match[0], p['id'], p['n'])) logging.debug('added to xref: {}'.format(p)) continue match = [ k for k, v in listitems(nbadict) if v == match_player( p['n'], list(nbadict.values()), threshold=.8) ] if match: self.db.update(insq.format(match[0], p['id'], p['n'])) logging.debug('added to xref: {}'.format(p)) else: logging.error('need to manually resolve {}'.format(p)) # now update dfs_salaries nbacom_player_id from player_xref self.db.update(update_dfs_salaries_ids()) else: logging.info('no missing ids in dfs_salaries')
def nbacom_xref(db, players): ''' Adds nbacom_player_id to list of player dict Args: db: NBAPostgres object players: list of dict Returns: players: list of dict ''' # recent players is a table view - has 2 columns nbap = nbacom_dict(db) logging.info(nbap) for idx, p in enumerate(players): name = match_player(p['source_player_name'], nbap.keys()) if name: players[idx]['nbacom_player_id'] = nbap.get(name) logging.info('matched {}'.format(p)) return players