import urllib import ast import json import csv import time apiKey="65c22b10-1dc3-49a8-b679-3d9c0bc0c198" totcounts ={'NA':0,'BRONZE':0,'SILVER':0,'GOLD':0,'PLATINUM':0,'DIAMOND':0} allcounts ={'NA':0,'BRONZE':0,'SILVER':0,'GOLD':0,'PLATINUM':0,'DIAMOND':0} def getPlayerInfo(sname): tier="NA" rank="NA" wins=0 pts=0 pinfo=getPlayerId(sname) sid=pinfo[0] lvl=pinfo[1] comment=pinfo[2] print "\t %-10s %-20s is level %2d" % (sid, sname, lvl), if sid=='Not found': restOfLine = " " else: #print "/%s/entry?api_key=%s" % sid,apiKey f = urllib.urlopen("https://prod.api.pvp.net/api/lol/na/v2.3/league/by-summoner/%s/entry?api_key=%s" % (sid,apiKey) ) # https://prod.api.pvp.net/api/lol/na/v2.3/league/by-summoner/25964984/entry?api_key=65c22b10-1dc3-49a8-b679-3d9c0bc0c198 urlline = f.read() f.close #print '[',l[101:114],']' if urlline[101:114]=="404 Not Found": restOfLine = " not in solo rank data" else: jdict=json.loads(urlline) for ds in jdict: # a summoner can be not ranked in solo but in team, then riot does not have sid in this data #print "####",ds if ds['queueType']=="RANKED_SOLO_5x5": tier=ds['tier'] rank=ds['rank'] wins=ds['wins'] pts =ds['leaguePoints'] #break #print " " restOfLine = "%-10s %-3s with %4d Rwins and %4d points" % (tier,rank,wins,pts) print restOfLine,comment return [ sname, tier, rank ,lvl, pts ] def getPlayerId(sname): warn='' try: f = urllib.urlopen("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/by-name/%s?api_key=65c22b10-1dc3-49a8-b679-3d9c0bc0c198" % sname ) tline = f.read() f.close except: tline="<html>" warn = 'team may have been dropped' if tline[0:6]=="<html>" : pid="Not found" lvl=0 else: jdict = ast.literal_eval(tline) #JSON string looks like dictionary but needs conversion csname=sname.lower() csname=''.join(csname.split()) kname=jdict.keys()[0] if kname!=csname: warn = "%s may have changed summoner name to %s" % (sname,kname) csname=kname pid= str(jdict[csname]['id']) #Riot wants mixed case in, but outputs all lower lvl =jdict[csname]['summonerLevel'] return [pid,lvl,warn] def getTeamInfo(playerlist): print "--------------------------------------------------------------------------------------------------------" print "TEAM", playerlist[0] tiers={'NA':0,'BRONZE':100,'SILVER':1000,'GOLD':2000,'PLATINUM':3000,'DIAMOND':4000} ranks={'NA':0,'I':500,'II':400,'III':300,'IV':200,'V':100} pcnt=1 players={} for i in range(1,len(playerlist)): if playerlist[i] != 'BLANK_ENTRY': playerInfo = getPlayerInfo(playerlist[i]) psum = tiers[playerInfo[1]] + ranks[playerInfo[2]]+int(playerInfo[3])+int(playerInfo[4]) players[playerInfo[0]]=psum totcounts[playerInfo[1]]+=1 allcounts[playerInfo[1]]+=1 time.sleep(3) return players def htmlTableToList(htab): #coverts 2D html table into a list of lists longlist=htab.split("><") #take advantage of tag symetry need to solve for spaces between carrots tab=[] for item in longlist: if item[0:2]=='tr': tab.append([]) #add row to table, will err if no first td before initial tr if item[0:2]=='td': cstr=[] for ch in item: # gets content where: ...>content<... ,if no > , gets all chars if ch =='<': break #end of content in i cstr.append(ch) if ch =='>': cstr=[] #start of content in I after this char j tab[-1].append("".join(cstr)) #add cell to latest row return tab #main=========================================================================================== UNIT tests print getPlayerId('zeshoo') #success print getPlayerId('dfgdfsdfggdfg') #fail print getTeamInfo(['idadar','zeshoo','S4 Loda'])
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question