def summoner_to_db_display(std_summoner_name, region='na', sum_id=None): # Pretty print heading for summoner print_header(u'Summoner Name: {}'.format(std_summoner_name), special_char='=') # First look for player in database if Player.objects.filter(std_summoner_name=std_summoner_name).exists(): req_player = Player.objects.get(std_summoner_name=std_summoner_name) secs_since_last_update = (timezone.now() - req_player.last_update ).total_seconds() if secs_since_last_update > 1800: match_list = api.get_match_list(region, req_player.summoner_id) req_player.last_update = timezone.now() req_player.save() match_id_list = [str(match['matchId']) for match in match_list['matches']] matches_to_db(match_id_list) match_display = Match.objects.filter(match_id__in=match_id_list) statset_display = StatSet.objects.filter(player=req_player) else: # Get player's last 15 matches from DB rel_statsets = StatSet.objects.filter(player=req_player) match_display = [statset.match for statset in rel_statsets] statset_display = rel_statsets # If player doesn't exist, make call to api and create player else: if sum_id is None: sum_dict = api.get_summoners_by_name(region, std_summoner_name) print sum_dict sum_id = sum_dict[std_summoner_name]['id'] match_list = api.get_match_list(region, sum_id) match_id_list = [str(match['matchId']) for match in match_list['matches']] matches_to_db(match_id_list) match_display = Match.objects.filter(match_id__in=match_id_list) # Check again if matches_to_db added player to DB if Player.objects.filter(std_summoner_name=std_summoner_name).exists(): req_player = Player.objects.get(std_summoner_name=std_summoner_name) else: # If sum_dict not yet declared, make API call sum_dict = api.get_summoners_by_id('na', sum_id) print sum_dict league_dict = api.get_solo_leagues( region=region, summoner_ids=sum_id) req_player = create_player(sum_id, sum_dict, league_dict, region) req_player.save() statset_display = StatSet.objects.filter(player=req_player) return statset_display
def match_to_db(match_id, region='na'): """ Makes 3 requests to API and adds the following to database, if they don't yet exist: -- 1 Match -- 1 MatchVersion -- 10 Players involved in match -- 10 Champs played in match (champion/role/league combo) -- 10 StatSets, 1 for each player involved in match -- ~10^2 BuildComponents, 1 for each item purchased in game Args: match_id: String representing match ID (from Riot API) region: String (2 letters) representing player's region (e.g. 'na') Returns: None API Calls: 1x get_match() 1x get_solo_leagues() 1x get_summoners_by_id() Database Changes: + 0-1 Match + 0-1 MatchVersion + 0-10 Player + 0-10 Champ + 0-10 StatSet + 0-500 BuildComponent Dependencies: ItemStatic RiotException api create_match create_player create_champ create_statset get_player_items create_build_component save_in_bulk, match_version_to_dd_version get_avg_solo_league print_header """ # Get match from Riot API try: match = api.get_match(region, match_id, include_timeline=True) except RiotException: print 'Match {id} not found!'.format(id=match_id) return # Get appropriate MatchVersion if it exists, otherwise create and save match_version = match['matchVersion'] if MatchVersion.objects.filter(match_version=match_version).exists(): mv = MatchVersion.objects.get(match_version=match_version) else: print 'Creating MatchVersion ({})'.format(match_version) mv = create_match_version(match_version, region) mv.save() # Get Data Dragon version from MatchVersion v = mv.dd_version # Get all ItemStatics that exist on Summoner's Rift (map_11) item_list = ItemStatic.objects.filter(map_11=True) # Create Match instance m = create_match(match) print_header('Match ID:{id}'.format(id=m.match_id)) print '\t Start time...\t\t\t\t\ttime={time}'.format( time=round(time.clock(),4)) if not m.is_in_db(): # Counts for displaying number of Players, Champs, Statsets and # BuildComponents created p_count = 0 c_count = 0 ss_count = 0 bc_count = 0 # Initialize lists for adding to DB in bulk champ_insert_list = [] statset_insert_list = [] player_insert_list = [] build_component_insert_list = [] # Get list of summoner IDS for league request player_id_list = [participant_id['player']['summonerId'] for participant_id in match['participantIdentities']] # Get average rank number for match, based on current rank of all # involved players league_dict = api.get_solo_leagues( region=match['region'].lower(), summoner_ids=str(player_id_list)[1:-1]) league_name = get_avg_solo_league(league_dict, player_id_list)[0] print league_name # Get player information for each player in match sum_dict = api.get_summoners_by_id('na', player_id_list) # Get timeline timeline = match['timeline'] print_header('Creating objects', margin=10) print '{0:>17} {1:>16} {2:>16} {3:>16}'.format( 'Players', 'Champs', 'StatSets', 'BuildComps' ) for participant in match['participants']: # Get summoner ID for player creation summoner_id = str(player_id_list[participant['participantId']-1]) print '\r{p: 17} {c: 16} {ss: 16} {bc: 16}'.format( p=p_count, c=c_count, ss=ss_count, bc=bc_count ), # Create Player instance and save to DB if it's not there yet p = create_player(summoner_id, sum_dict, league_dict, region) p_count += 1 print '\r{p: 17} {c: 16} {ss: 16} {bc: 16}'.format( p=p_count, c=c_count, ss=ss_count, bc=bc_count ), if not p in player_insert_list and not p.is_in_db(): player_insert_list.append(p) # Create Champ instance and save to DB if it's not there yet c = create_champ(participant, league_name, mv) c_count += 1 print '\r{p: 17} {c: 16} {ss: 16} {bc: 16}'.format( p=p_count, c=c_count, ss=ss_count, bc=bc_count ), if not c in champ_insert_list and not c.is_in_db(): champ_insert_list.append(c) # Create StatSet instance and save to DB if it's not there yet ss = create_statset(participant, timeline, c, p, m) ss_count += 1 print '\r{p: 17} {c: 16} {ss: 16} {bc: 16}'.format( p=p_count, c=c_count, ss=ss_count, bc=bc_count ), if not ss in statset_insert_list and not ss.is_in_db(): statset_insert_list.append(ss) participant_id = participant['participantId'] # Add all BuildComponents to insert list, to be saved to DB in # bulk later build = 0 build = get_player_items(participant_id, match) for component in build.build_history: bc = create_build_component(component, ss) bc_count += 1 print '\r{p: 17} {c: 16} {ss: 16} {bc: 16}'.format( p=p_count, c=c_count, ss=ss_count, bc=bc_count ), if not bc in build_component_insert_list: build_component_insert_list.append(bc) print '' print_header('Saving objects', margin=10) save_in_bulk(m, champ_insert_list, player_insert_list, statset_insert_list, build_component_insert_list) print_header(None, margin=10) print_header(None)