def setUp(self): if not self.api_key: with open(os.path.join(os.path.dirname(__file__), '../../api-key'),'rt') as f: self.api_key = f.read()[:36] baseriotapi.print_calls(False) baseriotapi.set_region('euw') baseriotapi.set_api_key(self.api_key)
def riot_request(region, args): # set region baseriotapi.set_region(region) # extract arguments request = args.get("request") key = args.get("key") match_id = args.get("match_id") summoner_id = args.get("summoner_id") summoner_ids = args.get("summoner_ids") # make request if request == 1: riot_response = baseriotapi.get_summoners_by_name(format_key(key)) elif request == 2: riot_response = baseriotapi.get_match_list(summoner_id, 0, 0, 0, 0, 0, QUEUE, SEASON) elif request == 3: riot_response = baseriotapi.get_match(match_id) elif request == 4: riot_response = baseriotapi.get_leagues_by_summoner(summoner_ids) elif request == 5: riot_response = baseriotapi.get_ranked_stats(summoner_id, SEASON) elif request == 6: riot_response = baseriotapi.get_summoner_runes(summoner_id) else: riot_response = None # return response return riot_response
def main(): parser = argparse.ArgumentParser() parser.add_argument("-key", type=str, required=True, help="API key to use for scraping") parser.add_argument("-region", type=str, required=True, help="API region to use for scraping") parser.add_argument("-db", type=str, required=False, help="Path to DB folder (default \"matches\")") parser.add_argument( "-chunk", type=int, required=False, help="Max number of matches per chunk file (default 50)") parser.add_argument("-ids", type=str, required=True, help="Path to match id file") args = parser.parse_args() baseriotapi.set_region(args.region) baseriotapi.set_api_key(args.key) baseriotapi.set_rate_limit(1000, 10) baseriotapi.print_calls(True) baseriotapi.get_match = auto_retry(baseriotapi.get_match) db = args.db if args.db else "matches" chunk = args.chunk if args.chunk else 50 scrape_matches(db, args.ids, chunk)
def setUp(self): if not self.api_key: with open(os.path.join(os.path.dirname(__file__), '../../api-key'), 'rt') as f: self.api_key = f.read()[:36] baseriotapi.print_calls(False) baseriotapi.set_region('euw') baseriotapi.set_api_key(self.api_key)
def setup_riot_api(conf): cassioepia = conf['cassiopeia'] baseriotapi.set_api_key(cassioepia['api_key']) baseriotapi.set_region(cassioepia['region']) limits = cassioepia.get('rate_limits', None) if limits is not None: if isinstance(limits[0], (list, tuple)): baseriotapi.set_rate_limits(*limits) else: baseriotapi.set_rate_limit(limits[0], limits[1]) baseriotapi.print_calls(cassioepia.get('print_calls', False))
def test_update_participants_min_tier(self): match_file = os.path.join(os.path.dirname(__file__), 'match_string.json') with open(match_file, 'rt') as f: match_string = f.read() match = MatchDetail(json.loads(match_string)) baseriotapi.set_region('na') result = TierSeed() min_tier = update_participants(result, match.participantIdentities, Tier.platinum) leagues = defaultdict(set) leagues[Tier.diamond] = {21589368} leagues[Tier.platinum] = {22668834, 41304754, 25957444, 30555923, 42652920, 507594, 22622940} for tier in Tier: self.assertEqual(leagues[tier], result[tier]) self.assertEqual(min_tier, Tier.gold)
def predict(summ_region, summ_name): # Set up libraries, load saved x x = np.load(open('tmp/trainedX_avg.npy', 'rb')) api.set_api_key(keys.riotapikey) api.set_region(summ_region) # TODO: Handle unicode characters in summoner names # Retrieve summoner summ_name_key = summ_name.lower().replace(' ', '') summ = api.get_summoners_by_name(summ_name)[summ_name_key] # Create template summoner w/ empty ratings db = Session() champ_dict = {} for champ in db.query(Champion): champ_dict[champ.champion_id] = 0 # Fill in template summoner w/ specified summoner champion points masteries = api.get_champion_masteries(summ.id) for m in masteries: champ_dict[m.championId] = m.championPoints y_raw = [champ_dict[champ_id] for champ_id in sorted(champ_dict.keys())] # Normalize summoner champion points y_raw = np.asarray(y_raw) y_std = np.std(y_raw) y = y_raw / y_std # Train theta for user lamb = .1 alpha = .0001 iterations = 2000 feature_count = len(x[0]) init_theta = np.random.random_sample((1, feature_count)) theta = train_theta(x, y, init_theta, lamb, alpha, iterations) # Create champ_id: champ_name dictionary champ_dict = [] for champ in db.query(Champion): champ_dict.append((champ.champion_id, champ.champion_name)) champ_dict = sorted(champ_dict, key=lambda x: x[0]) # Make predictions and print formatted results h = np.dot(theta, x.T) champ_ids, champ_names = zip(*champ_dict) predictions = zip(champ_names, h[0]) predictions = sorted(predictions, key=lambda x: x[1]) return predictions
def test_update_participants_min_tier(self): match_file = os.path.join(os.path.dirname(__file__), 'match_string.json') with open(match_file, 'rt') as f: match_string = f.read() match = MatchDetail(json.loads(match_string)) baseriotapi.set_region('na') result = TierSeed() min_tier = update_participants(result, match.participantIdentities, Tier.platinum) leagues = defaultdict(set) leagues[Tier.diamond] = {21589368} leagues[Tier.platinum] = { 22668834, 41304754, 25957444, 30555923, 42652920, 507594, 22622940 } for tier in Tier: self.assertEqual(leagues[tier], result[tier]) self.assertEqual(min_tier, Tier.gold)
def process_region(region): elastic = elasticsearch.Elasticsearch() baseriotapi.set_region(region) d = str(baseriotapi.get_challenger("RANKED_SOLO_5x5")) j = json.loads(d) wins, mostWins, lastWins, losses, mostLosses, lastLosses, count = 0, 0, 0, 0, 0, 0, 0 userMostWins, userMostLosses = "", "" for entry in j['entries']: wins += int(entry['wins']) if int(entry['wins']) > mostWins: mostWins = int(entry['wins']) userMostWins = entry['playerOrTeamName'] losses += int(entry['losses']) if int(entry['losses']) > mostLosses: mostLosses = int(entry['losses']) userMostLosses = entry['playerOrTeamName'] count += 1 try: avgWins = round(wins / count) lastWins = int(config.get(region, 'wins')) avgLosses = round(losses / count) lastLosses = int(config.get(region, 'losses')) config.set(region, 'wins', wins) config.set(region, 'losses', losses) config.set(region, 'avg_wins', avgWins) config.set(region, 'avg_losses', avgLosses) newWins = wins - lastWins newLosses = losses - lastLosses timestamp = datetime.utcnow() elastic.index(index='lol_challenger_solo_weekly', doc_type='weekly_played', body={"region": region, "wins": newWins, "most_wins": mostWins, "user_most_wins": userMostWins, "avg_wins": avgWins, "losses": newLosses, "most_losses": mostLosses, "user_most_losses": userMostLosses, "avg_losses": avgLosses, "timestamp": timestamp}) except: traceback.print_exc()
def generatecodes(request): #6365 is spring tournament ID baseriotapi.set_region("NA") weeks = 5 gamesperweek = 12 codecount = weeks * gamesperweek * 2 codes = baseriotapi.create_tournament_codes(6365, count=codecount) i = 0 weekcount = 0 for week in Week.objects.all(): savecode = TourneyCode() savecode.team1 = week.L0game1t1 savecode.team2 = week.L0game1t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 0 savecode.game = 1 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L0game1t1 savecode.team2 = week.L0game1t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 0 savecode.game = 1 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L0game2t1 savecode.team2 = week.L0game2t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 0 savecode.game = 2 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L0game2t1 savecode.team2 = week.L0game2t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 0 savecode.game = 2 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L0game3t1 savecode.team2 = week.L0game3t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 0 savecode.game = 3 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L0game3t1 savecode.team2 = week.L0game3t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 0 savecode.game = 3 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L1game1t1 savecode.team2 = week.L1game1t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 1 savecode.game = 1 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L1game1t1 savecode.team2 = week.L1game1t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 1 savecode.game = 1 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L1game2t1 savecode.team2 = week.L1game2t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 1 savecode.game = 2 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L1game2t1 savecode.team2 = week.L1game2t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 1 savecode.game = 2 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L1game3t1 savecode.team2 = week.L1game3t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 1 savecode.game = 3 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L1game3t1 savecode.team2 = week.L1game3t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 1 savecode.game = 3 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L2game1t1 savecode.team2 = week.L2game1t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 2 savecode.game = 1 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L2game1t1 savecode.team2 = week.L2game1t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 2 savecode.game = 1 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L2game2t1 savecode.team2 = week.L2game2t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 2 savecode.game = 2 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L2game2t1 savecode.team2 = week.L2game2t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 2 savecode.game = 2 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L2game3t1 savecode.team2 = week.L2game3t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 2 savecode.game = 3 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L2game3t1 savecode.team2 = week.L2game3t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 2 savecode.game = 3 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L3game1t1 savecode.team2 = week.L3game1t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 3 savecode.game = 1 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L3game1t1 savecode.team2 = week.L3game1t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 3 savecode.game = 1 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L3game2t1 savecode.team2 = week.L3game2t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 3 savecode.game = 2 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L3game2t1 savecode.team2 = week.L3game2t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 3 savecode.game = 2 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L3game3t1 savecode.team2 = week.L3game3t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 3 savecode.game = 3 savecode.rift = 0 savecode.save() i = i + 1 savecode = TourneyCode() savecode.team1 = week.L3game3t1 savecode.team2 = week.L3game3t2 savecode.code = codes[i] savecode.week = weekcount savecode.league = 3 savecode.game = 3 savecode.rift = 1 savecode.save() i = i + 1 savecode = TourneyCode() weekcount = weekcount + 1
app_id='d578fa0187', secret_token='e6f52c2d50feb7278e91aec9f516776db173c577', ) # Set API key # Lets cassi handle riot api rate limits. But does this work per instance of this app (if many ppl are using website at same time) or globally? is this the best place # for this, or should this be under the relevant url routes? riotapi.set_region('na') riotapi.print_calls(True) riotapi.set_api_key(API_KEY) riotapi.set_rate_limits((9, 10), (480, 600)) riotapi.set_load_policy(LoadPolicy.lazy) baseriotapi.set_region('na') baseriotapi.print_calls(True) baseriotapi.set_api_key(API_KEY) baseriotapi.set_rate_limits((9, 10), (480, 600)) def auto_retry(api_call_method): """ A decorator to automatically retry 500s (Service Unavailable) and skip 400s (Bad Request) or 404 (Not Found). """ def call_wrapper(*args, **kwargs): try: return api_call_method(*args, **kwargs) except APIError as error: # Try Again Once if error.error_code in [500]: try: print("Got a 500, trying again...")