· 6 years ago · Oct 16, 2019, 09:24 PM
1'''
2module that takes a given summoner name, scrapes thru match history, gathers data on start champion, then repeats for next champion.
3returns data in a json format
4'''
5
6
7
8import requests
9import time
10import random
11KEY = "<private key>"
12ctrTot = 0
13initTimeStamp = time.time()
14def scrapeSummoner(summName,ctr):
15 global ctrTot
16 global initTimeStamp
17 #get summoner unique ID
18 summID = requests.get('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/'+summName+'?api_key='+KEY).json()["accountId"]
19 ctrTot = ctrTot + 1
20 #get summoner match history
21 summHistory = requests.get("https://na1.api.riotgames.com/lol/match/v4/matchlists/by-account/"+summID+"?api_key="+KEY).json()["matches"]
22 ctrTot = ctrTot + 1
23 #get match ids from last 2 weeks
24 recentHistory = []
25 for x in summHistory:
26 if (x["timestamp"]+1209600000) > int(time.time()*1000):
27 recentHistory.append(x["gameId"])
28 #print(recentHistory)
29
30 ctra = 0
31 initMatches = []
32 for x in recentHistory:
33 y = requests.get("https://na1.api.riotgames.com/lol/match/v4/matches/"+str(x)+'?api_key='+KEY).json()
34 ctra = ctra + 1
35 ctrTot = ctrTot + 1
36 if(ctrTot>100 and time.time()>=initTimeStamp+120):
37 ctrTot = 0
38 print("sleeping ctrTot")
39 time.sleep(15)
40 initTimeStamp = time.time()
41
42 if((ctra>15 and time.time()>=initTimeStamp+20)):
43 ctra = 0
44 print("sleeping ctra")
45 time.sleep(2)
46 initTimeStamp = time.time()
47
48 initMatches.append(y)
49 #print(initMatches)
50
51 summonersListed = []
52 for x in initMatches:
53 #print(x)
54 for y in x["participantIdentities"]:
55 summonersListed.append(y["player"]["summonerName"])
56 summonersListed = list(dict.fromkeys(summonersListed))
57 #print("summonersListed",str(summonersListed))
58 randomSummoner = random.choice(summonersListed)
59 while(randomSummoner == summName):
60 randomSummoner = random.choice(summonersListed)
61 if(ctr>0):
62 return(scrapeSummoner(randomSummoner,ctr-1))
63 else:
64 return(summonersListed)
65
66print(scrapeSummoner("Well I Tried",3))
67#TODO rewrite api calls near initMatches to avoid ratelimit