def convertRotowireList(rotoWireList,urlList): playerIDDict = ReadWriteFiles.readPlayerIDMap() #print("\noriginal list") #print(rotoWireList) returnedList = [] for i in range(0,len(rotoWireList)): rotoname = rotoWireList[i] #takes care of cases where name matches #and when first and last name both match -> regardless of Jr. suffix, middle name if(rotoname in playerIDDict.keys() or (HumanName(rotoname).first + " " + HumanName(rotoname).last) in playerIDDict.keys()): returnedList.append(rotoname) else: if(rotoname[1] == "."): tempLst = [] for playerName in playerIDDict.keys(): if(HumanName(playerName).first == HumanName(rotoname).first and playerName[0] == rotoname[0]): tempLst.append(playerName) if len(tempLst) == 1: returnedList.append(tempLst[0]) else: (html.fromstring(requests.get(urlList[i]).content)).xpath("//h1[position()=1]/text()")[0] else: for playerName in playerIDDict.keys(): if(HumanName(playerName).first == HumanName(rotoname).first and HumanName(playerName).last == HumanName(rotoname).last): returnedList.append(playerName) # convertedList = [x if (x in playerIDDict.keys()) else [k for (k,v) in playerIDDict.items() if (x[1] == "." and x.split(" ")[1] in k and x[0] == k[0]) or x in k] for x in rotoWireList] # print(returnedList) # convertedList = [x[0] if not type(x[0]) == list else x[0][0] if len(x[0])==0 else (html.fromstring(requests.get(x[1]).content)).xpath("//h1[position()=1]/text()")[0] for x in zip(convertedList,urlList)] #[(html.fromstring(requests.get(x).content)).xpath("//h1[position()=1]/text()")[0] for x in starterFullURLList] #print("\nconverted list") #print(convertedList) return returnedList
def playerid_to_playerName(playerid): playerIDDict = ReadWriteFiles.readPlayerIDMap() if(playerid in playerIDDict): return playerIDDict[playerid] else: #print(str(playerid)) playerPage = requests.get("http://espn.go.com/nba/player/_/id/" + str(playerid)) playerTree = html.fromstring(playerPage.content) #print(playerid) try: [playerName] = playerTree.xpath("//div[@class='mod-content']/*/h1/text() | //div[@class='mod-content']/h1/text()") except (IndexError): print("Player " + playerid + " causes error") playerIDDict[playerid] = playerName playerIDDict[playerName] = playerid ReadWriteFiles.writePlayerIDDict(playerIDDict) return playerName
def playername_to_id(playername): playerIDDict = ReadWriteFiles.readPlayerIDMap() return playerIDDict[playername]