class main: def __init__(self, _APIkey, _sumName, _matchNo, _region, _rankedType): # ==== Definitions self._gameType = '' if _rankedType == "solo": self._gameType = 'RANKED_TEAM_5x5' elif _rankedType == "ranked": self._gameType = 'RANKED_SOLO_x5' self._APIkey = _APIkey # API key for developers self._summoner_name = _sumName # Name of LoL Summoner self._watcherObj = RiotWatcher(self._APIkey) # riotwatcher data self._sumObj = self._watcherObj.get_summoner(name=self._summoner_name, region=_region) # Data on Summoner self._sID = self._sumObj['id'] # ID of Summoner self._match = _matchNo # ID of match to work with self._sumGames = self._watcherObj.get_match_history(summoner_id=self._sID, region=_region, ranked_queues=self._gameType) self._stats1 = json.dumps(self._watcherObj.get_match(self._match, region=_region, include_timeline='true')) self._parsed1 = json.loads(self._stats1) self._matchId = self._parsed1['matchId'] self._mapId = self._parsed1['mapId'] def getPeopleForMatch(self): _people = {} # ID | name for a in self._parsed1['participantIdentities']: _people[a['player']['summonerId']] = str(a['player']['summonerName']) return _people def getParticipantsForMatch(self): _participants = {} # number | ID for values in self._parsed1['participantIdentities']: _participants[values['participantId']] = values['player']['summonerId'] return _participants def getEventsPerPerson(self): _eventsPerPerson = {} for count in range(10): _eventsPerPerson[str(count + 1)] = [] for items in self._parsed1['timeline']['frames']: # ignores wards try: tempList = items['events'] for vals in tempList: #if "participantId" in str(vals): # _eventsPerPerson[str(vals['participantId'])] = _eventsPerPerson[str(vals['participantId'])] + [vals] if "killerId" in str(vals) and "BUILDING_KILL" not in str(vals): _eventsPerPerson[str(vals['killerId'])] = _eventsPerPerson[str(vals['killerId'])] + [vals] except KeyError: continue # print "skipped", str(items['participantFrames']) return _eventsPerPerson def getFramesPerPerson(self): _framesPerPerson = {} for count in range(10): _framesPerPerson[str(count + 1)] = [] for values in self._parsed1['timeline']['frames']: for count in range(1, 11): tempDict = dict(values['participantFrames'][str(count)]) _framesPerPerson[str(count)] = _framesPerPerson[str(count)] + [tempDict] return _framesPerPerson #for key in _eventsPerPerson: # print "Participant:", str(key) # for events in _eventsPerPerson[key]: # temp = json.dumps(events, indent=4, sort_keys=True) # print temp #match = 1960675310 #run = main('', "StirlingArcher69", match) #print "==== Match details for", str(match), "====" #print(json.dumps(run.getEventsPerPerson(), indent=4, sort_keys=True))
def main(): w = RiotWatcher(constants.riot_id) db = sqlite3.connect('matchdb') cursor = db.cursor() cursor.execute( '''SELECT DISTINCT match_id FROM match_by_tier ORDER BY match_id ASC''' ) if not os.path.exists("matches_silver"): os.makedirs("matches_silver") os.chdir("matches_silver"); for row in cursor: match_id = row[0] print(match_id) match_filename = get_match_filename(match_id) if (os.path.isfile(match_filename)): print("Skipping: {}".format(match_filename)) else: try: match_json = w.get_match(match_id, include_timeline=True) with open(match_filename, 'w') as f: f.write(json.dumps(match_json)) print("Writing: {}".format(match_filename)) except Exception as e: print("Failed: {0} with {1}".format( match_filename, e )) time.sleep(1.2)
def getMatch(matchList, server): key = readApiKey() w = RiotWatcher(key, server) try: match = w.get_match(matchList['matchId'], server) except Exception: pass return match
class CallCounter: """Class to control the rate of requests""" def __init__(self): """Set up the initial values for the class and read from the config file""" jsonConfig = open("./config.json").read() self.config = json.loads(jsonConfig) self.api = RiotWatcher(self.config["api-key"], default_region=self.config["region"]) self.canMakeRequest = True self.callCount = 10 def get_call_count(self): return self.callCount def decrease_call_count(self): self.callCount -= 1 if self.callCount == 0: self.canMakeRequest = False def reset_call_count(self): self.callCount = 10 self.canMakeRequest = True def get_match_details_from_id(self, match_id): """Gets match details for the given match_id. Returns a list of match details""" if self.canMakeRequest: time.sleep(1) print("Getting match details for game id: " + str(match_id)) md = self.api.get_match(match_id, include_timeline=True) print(md) return md def get_match_ids_for_team(self): """Gets the latest games for the team. Returns a list of strings with the game ids""" if self.canMakeRequest: time.sleep(1) print("Getting match history for team id " + self.config["team-id"]) return self.api.get_team(self.config["team-id"])
except Exception,e: print("An ERROR occurred when pulling match history data for summonerId {0}! {1}".format(current_summoner_id,e)) unpulled_summoners.insert(0, current_summoner_id) continue try: matchIdsToTimestamp = { match['matchId']:match['timestamp'] for match in match_history['matches'] } except KeyError,e: print("Some field you tried to access did not exist in the pulled summoner data: {0}".format(e)) continue pulled_summoners.append(current_summoner_id) for matchId, timestamp in matchIdsToTimestamp.items(): if matchId not in pulled_matches and timestamp > int(time.time()*1000) - 3*24*60*60000: try: wait_for_request_availability(w) match_data = w.get_match(matchId, include_timeline=False) participants_ids = [pIdentity['player']['summonerId'] for pIdentity in match_data['participantIdentities']] match_specific_pids = [pIdentity['participantId'] for pIdentity in match_data['participantIdentities']] participants_champion_played = {participants_ids[match_specific_pids.index(participant['participantId'])]:participant['championId'] for participant in match_data['participants']} except Exception, e: print("Error occured when trying to get match details: " + str(e)) continue pulled_matches[matchId] = saved_files try: wait_for_request_availability(w) participants_league_data = w.get_league(summoner_ids=participants_ids) except Exception, e: print("Error occured when trying to get league data: " + str(e)) continue
class DataStore: """ DataStore is the script that reads json repsonses from the League of Legends API and stores these responses to disk in the respective player's folder. """ def __init__(self, api, sumName, region): """ Initialisation function. :param api: The API key for the user, this is obtained through Riot's developer site. :param sumName: The name of the LoL Summoner that data will be pulled for. :param region: The region that the Summoner plays in. :return: None """ self.watcherOb = RiotWatcher(api) # LolStats # The match history is a temporary solution to get this done quick, # basically it just dumps a match history file every time instead of # adding on to the current one. :( self.origDir = os.getcwd() self._playerName = sumName self._playerRegion = region self._playerData = '' self._playerID = 0 self._playerHist = '' if self._playerRegion != '': self._playerData = self.watcherOb.get_summoner(name=self._playerName, region=self._playerRegion) self._playerID = int((self.watcherOb.get_summoner(name=self._playerName, region=self._playerRegion))['id']) self._playerHist = self.watcherOb.get_match_history(summoner_id=self._playerID, ranked_queues='RANKED_SOLO_5x5', region=self._playerRegion) else: self._playerData = self.watcherOb.get_summoner(name=self._playerName) self._playerID = int((self.watcherOb.get_summoner(name=self._playerName))['id']) self._playerHist = self.watcherOb.get_match_history(summoner_id=self._playerID, ranked_queues='RANKED_SOLO_5x5') #print self._playerHist self._playerMatches = [] for allVals in self._playerHist['matches']: self._playerMatches.append(allVals['matchId']) _dir = str(os.getcwd())+"/static/json/" # Dir creation def writer(_dir, _file, _data, _json): """ Helper function to write data to the disk. :param _dir: The directory to store the data. :param _file: The name to call the file that is created. :param _data: The data object to write to the file. :param _json: Flag to indicate if the data is JSON or not. :return: None """ writeFile = open(str(_dir)+"/"+str(_file), "w") if _json is True: writeFile.write(json.dumps(_data, indent=4)) else: writeFile.write(_data) writeFile.close() if not os.path.exists(_dir+str(self._playerID)): os.makedirs(str(_dir+str(self._playerID))) writer((str(_dir+str(self._playerID))), "_playerData.json", self._playerData, True) writer((str(_dir+str(self._playerID))), "_playerHist1.json", self._playerHist, True) else: if not os.path.isfile(str(_dir+str(self._playerID)+"/_playerData.json")): writer((str(_dir+str(self._playerID))), "_playerData.json", self._playerData, True) if not os.path.isfile(str(_dir+str(self._playerID)+"/_playerHist1.json")): writer((str(_dir+str(self._playerID))), "_playerHist1.json", self._playerHist, True) elif os.path.isfile(str(_dir+str(self._playerID)+"/_playerHist1.json")): os.chdir(_dir+str(self._playerID)) temp = glob.glob("_playerHist*") inc = int(re.search(r'\d+', str(temp[-1])).group()) + 1 writer((str(_dir+str(self._playerID))), "_playerHist"+str(inc)+".json", self._playerHist, True) if filecmp.cmp("_playerHist"+str(inc-1)+".json", "_playerHist"+str(inc)+".json") is True: os.remove("_playerHist"+str(inc)+".json") os.chdir(self.origDir) if not os.path.exists(_dir+str(self._playerID)+"/matchData"): os.makedirs(_dir+str(self._playerID)+"/matchData") os.chdir(_dir+str(self._playerID)) for i in glob.glob("_playerHist*"): temp = json.load(open(i, 'r')) for vals in temp['matches']: if not os.path.isfile(str(_dir+str(self._playerID)+"/matchData/"+str(vals['matchId'])+".json")): writer(str(_dir+str(self._playerID)+"/matchData/"), str(vals['matchId'])+".json", self.watcherOb.get_match(match_id=vals['matchId'], include_timeline=True), True) os.chdir(self.origDir) def getPlayerID(self): return self._playerID
def main(): # gets key from file api_key = get_file("api.key") ## init RiotWatcher rw = RiotWatcher(api_key.strip()) # location of the matchIDs if you want AP_ITEM_DATASET change to # the appropriate directory dir_location = "../info/BILGEWATER_DATASET/BILGEWATER" files_dir = os.listdir(dir_location) for file in files_dir: # For every file that doesn't end in _info.json loop through it # expecting a list of MatchIDs and then preform API calls to get the match info # and then write to a file with ending "_info.json" if "_info.json" not in file: new_file_name = file.replace(".json","_info.json") print "Generating %s"%(new_file_name) print_file = open(dir_location + "/" + new_file_name ,"w+") # opens the file and reads contents file_info = get_file(dir_location + "/" + file) # turns the string representation of a list of MatchIDs # into an actual list of MatchIDs match_list = ast.literal_eval(file_info) # variables to display progress in processing MatchIDs count = 1 match_len = len(match_list) # Begins writing the output file with beginning of the list print_file.write("[") # For every MatchId in the MatchList for matchID in match_list: # Sets default value for match_info match_info = {} # used to gauge 429 responses later on gotitbool = False # Loops until gotitbool is found while not gotitbool: try: # checks if queue is clear if not waits until it is wait(rw) # attempts to get the match information given MatchID match_info = rw.get_match(matchID, region=file.replace(".json","").lower(), include_timeline=True) # if it got this far it successful and gotitbool is set to True gotitbool = True except LoLException as e: # This trips if an LoLException is raised from RiotWatcher print e.error # If this is not a 429 error then it can't wait a time span # to find a solution to it if e.error not in [error_429, error_503, error_500]: # a 400, 401, 404 error print "error from server: %s"%(e.error) return # Prints out all the applicable headers used for debugging for header in e.response.headers: if header not in ['access-control-allow-headers','content-encoding','transfer-encoding','x-newrelic-app-data','server','connection','cache-control','date','access-control-allow-origin','access-control-allow-methods','content-type','content-length']: print "headers: %s"%(header) if 'Retry-After' in e.response.headers: # If the client receives a Rate Limit Exceeded response the client # should process this response and halt future API calls for the duration, # in seconds, indicated by the Retry-After header time.sleep(int(e.response.headers['Retry-After'])) else: # Else if no Retry-After header wait a reasonable time (1sec) # and then try agian if e.error in [error_500,error_503]: time.sleep(30) else: time.sleep(1) except Exception as e: # An error occured that was not anticipated print str(e) return # Dumps the json information into the output file print_file.write(str(json.dumps(match_info))) # If not the end of the file adds a ",\n" which is # needed to properly process the file later if count != match_len: print_file.write(",\n") # Prints progress in processing the file print "%s: %s/%s"%(str(file), count, match_len) # Moves counter up by 1 count += 1 # closing off the list of matches print_file.write("]") print_file.close()
import json from riotwatcher import RiotWatcher jsonConfig = open("./config.json").read() config = json.loads(jsonConfig) api = RiotWatcher(config["api-key"], default_region=config["region"]) match_id = 2483005284 details = api.get_match(match_id, include_timeline=True) # def split_match_details(details): participantIdentities = details["participantIdentities"] participants = details["participants"] del details["participantIdentities"] del details["participants"] print(details) print(participantIdentities) print(participants)
match['matchId']: match['timestamp'] for match in match_history['matches'] } except KeyError, e: print( "Some field you tried to access did not exist in the pulled summoner data: {0}" .format(e)) continue pulled_summoners.append(current_summoner_id) for matchId, timestamp in matchIdsToTimestamp.items(): if matchId not in pulled_matches and timestamp > int( time.time() * 1000) - 3 * 24 * 60 * 60000: try: wait_for_request_availability(w) match_data = w.get_match(matchId, include_timeline=False) participants_ids = [ pIdentity['player']['summonerId'] for pIdentity in match_data['participantIdentities'] ] match_specific_pids = [ pIdentity['participantId'] for pIdentity in match_data['participantIdentities'] ] participants_champion_played = { participants_ids[match_specific_pids.index( participant['participantId'])]: participant['championId'] for participant in match_data['participants'] } except Exception, e:
return self.connection[self.db][self.collection].find_one(doc) watcher = RiotWatcher(key) def wait_for_request(): while not watcher.can_make_request(): time.sleep(0.1) for patch in ["5.11", "5.14"]: for region in ["BR","EUNE","EUW","KR","LAN","LAS","NA","OCE","RU","TR"]: print "REGION: ", region data = open(patch + "/RANKED_SOLO/" + region + ".json") matches = json.load(data) collection = MongoDBSafe(CHALLENGE_DB, patch + "_" + region, url=URL) region_var = region.lower() if collection.connection[collection.db][collection.collection].count() == len(matches): matches = [] for match in matches: print(match) if not collection.get({"_id":match}): wait_for_request() print("querying match") try: match_data = watcher.get_match(match, region=region_var, include_timeline=True) # add in _id field match_data["_id"] = match_data["matchId"] collection.push(match_data) except LoLException as error: print "ERROR: ", error.error
w = RiotWatcher("RGAPI-0559ced4-bb1a-4b85-a086-bdb9098f3e14") success_sample = 0 k = 100000 p = ["", "", "", "", "", "", "", "", "", ""] dat = open("new_game_data_2.txt", "a") header = "matchID, matchType, matchMode, queueType, playerID, teamID, Winner, championID, lane, role, spell_1, " \ "spell_2, tier, K, D, A, dam, dToC, lvl, damToken, heal, minionKill, neutralMinionsKilled, " \ "goldEarned, totalTimeCrowdControlDealt, gameLength" + "\n" dat.write(header) while True: print("This is the " + str(k) + " try\n") try: g = w.get_match(2778000000-k, region=KOREA) except riotwatcher.riotwatcher.LoLException as error: if error.error == "Game data not found": print(error.error) k += 1 continue elif error.error == "Too many requests": print(error.error) time.sleep(8) continue if g["queueType"] != "TEAM_BUILDER_RANKED_SOLO": print("This is not a rank game") k += 1 continue success_sample += 1 k += 1
while not watcher.can_make_request(): time.sleep(0.1) for patch in ["5.11", "5.14"]: for region in [ "BR", "EUNE", "EUW", "KR", "LAN", "LAS", "NA", "OCE", "RU", "TR" ]: print "REGION: ", region data = open(patch + "/RANKED_SOLO/" + region + ".json") matches = json.load(data) collection = MongoDBSafe(CHALLENGE_DB, patch + "_" + region, url=URL) region_var = region.lower() if collection.connection[collection.db][ collection.collection].count() == len(matches): matches = [] for match in matches: print(match) if not collection.get({"_id": match}): wait_for_request() print("querying match") try: match_data = watcher.get_match(match, region=region_var, include_timeline=True) # add in _id field match_data["_id"] = match_data["matchId"] collection.push(match_data) except LoLException as error: print "ERROR: ", error.error