def get_or_create(cls, account_id, club='', is_admin=0, club_id=''): print('get_or_create') req = cls.objects.filter(account_id=account_id) if not req: api = get_doAPI() req2 = api.get_player_summaries(account_id) players = req2.get("players", []) if players: player = players[0] player['account_id'] = account_id insert_plyers = { 'account_id': account_id, 'steamid': player.get('steamid', ''), 'lastlogoff': player.get('lastlogoff', ''), 'avatar': player.get('avatar', ''), 'avatarmedium': player.get('avatarmedium', ''), 'avatarfull': player.get('avatarfull', ''), 'club': club, 'is_admin': is_admin } if not club: insert_plyers.pop('club') result = cls.objects.create(**insert_plyers) print(result) ## 发送到DA player['club_id'] = club_id player['is_admin'] = is_admin # 待考虑的roll back data_handler.fast_write(da_config.DB_LOGIC, player, 'players') ## req = cls.objects.filter(account_id=account_id) return req
def update_data(cls): api = get_doAPI() req = api.get_heroes() heroes = req.get("heroes", []) for i_hero in heroes: id_h = i_hero.get("id", 0) req2 = cls.objects.update_or_create(id=id_h, defaults=i_hero) print(req2)
def update_data(cls, league_id): print('start update league matches', league_id) if league_id: api = get_doAPI() start_at_match_id = '' recheck_count = 0 while True: req_api = api.get_match_history( league_id=league_id, start_at_match_id=start_at_match_id) if req_api.get('status', 0): matches = req_api.get('matches', []) for match in matches: time.sleep(2) match_id = match.get('match_id', '') if match_id: players = match.get('players') match['players'] = json.dumps(players) # add league league_set = League.objects.filter( leagueid=league_id) league = league_set[0] if league_set else None match['league'] = league # lobby type instance num_lobby = match.get('lobby_type', -1) lobby = LobbyType.objects.filter( status=int(num_lobby)) match['lobby_type'] = lobby[0] # club 增加 radiant_team_id = match.pop('radiant_team_id') dire_team_id = match.pop('dire_team_id') team1 = Club.objects.filter( club_id=int(radiant_team_id)) team2 = Club.objects.filter( club_id=int(dire_team_id)) if not (team1 and team2): team1 = Club.add_one(radiant_team_id)[0] team2 = Club.add_one(dire_team_id)[0] if team1: match['radiant_team'] = team1 if isinstance( team1, Club) else team1[0] if team2: match['dire_team'] = team2 if isinstance( team2, Club) else team2[0] req = cls.objects.update_or_create( match_id=match_id, defaults=match) print(req) MatchToPlayer.create_one_data(req[0], players) this_num = len(matches) total_results = req_api.get('total_results', 0) results_remaining = req_api.get('results_remaining', 0) start_at_match_id = matches[-1].get('match_id') if results_remaining == 0: print('update match done') break else: recheck_count += 1 if recheck_count > 3: raise ValueError('api connect failed')
def add_one(cls, team_id): print('addd') api = get_doAPI() req = api.get_team_info_by_team_id(start_at_team_id=team_id, teams_requested=1) result = req print(req) if result.get('status', 0): team_list = result.get('teams', []) if team_list: team = team_list[0] country = pycountry.countries.get(alpha_2=str( team.get('country_code', '')).upper()) if team.get( 'country_code', '') else '' country = country.name if country else '' update_content = { 'club_id': team_id, 'name': team.get('name'), 'tag': team.get('tag', ''), 'time_created': team.get('time_created', 0), 'calibration_games_remaining': team.get('calibration_games_remaining', 0), 'logo': team.get('logo', 0), 'logo_sponsor': team.get('logo_sponsor', 0), 'country': country, 'url': team.get('url', ''), 'games_played': team.get('games_played', ''), } ### 创建队员如果没有 print('update_content', update_content) req = cls.objects.update_or_create(name=team.get('name'), defaults=update_content) print('req__', req) # result_team = cls.objects.filter(club_id=team_id) # update team players players_ids, admin_id = get_player_from_team(team) Players.get_or_create(admin_id, club=req[0], is_admin=1, club_id=team_id) for account_id in players_ids: Players.get_or_create(account_id, club=req[0], club_id=team_id) return req
def update_league(cls): api = get_doAPI() L_list = api.get_league_listing() leagues = L_list['leagues'] if L_list else [] for i_league in leagues: leagueid = i_league.get('leagueid', '') if leagueid: print('leagueid', leagueid) req = cls.objects.update_or_create(leagueid=leagueid, defaults=i_league) print(req) else: print(i_league)