Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #######################################################
- #parsehtml - Parses html from websites, specifically srl
- #July 2011
- #
- # - Use the methods to get info on the game variable.
- #######################################################
- import urllib
- import string
- import array
- import excel
- #The Game that you want to look up (eventually will be a variable set in a bot cmd)
- GAME = "smw"
- def changeGame(var):
- if (getGameName(var) == "DNE"):
- return "false"
- else:
- global GAME
- GAME = var
- return "true"
- ###################################EXCEL FUNCTIONS##################
- def createExcel():
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.speedrunslive.com/gamelist/game.php?game=" + GAME)
- htmlSource = sock.read()
- sock.close()
- indexes = 0
- goallist = []
- while (indexes > -1):
- #Get the tracked goals put them into list!
- indexes = htmlSource.find("raceFeedStatus\"><tr>")
- print "index = " + str(indexes)
- if (indexes==-1):
- break
- indexes = indexes + 36
- print "index = " + str(indexes)
- tempgoal = ""
- tempgoal = getWord(htmlSource,indexes,"<")
- print tempgoal
- goallist.append(tempgoal)
- endof = len(htmlSource)
- htmlSource = htmlSource[indexes:endof]
- print goallist
- excel.writeGoals(goallist)
- def updateRaces():
- #The Game that you want to look up (eventually will be a variable set in a bot cmd)
- smwlist = []
- i = 1000
- while i<4100:
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://speedrunslive.com/races/race.php?race=" + str(i))
- htmlSource = sock.read()
- sock.close()
- itthere = htmlSource.find("Super Mario World")
- if itthere > 0:
- smwlist.append(i)
- htmlSource = " "
- i = i + 1
- print smwlist
- #####################################END EXCEL##############################
- ####
- #Internal functions
- #
- ####
- #returns characters from source. Starting from index 'where' to character 'char'
- def getWord(source,where,char):
- letter = ' '
- temp = ' '
- while letter != char:
- print "getWord - while '" + letter + "' != '" + char + "'"
- print "looking at index: " + str(where)
- letter = source[where]
- temp = temp + letter
- where = where + 1
- if where+1 >= len(source):
- return "*NO MORE*"
- temp = temp[0:len(temp) - 1]
- where = 0
- return temp
- #Returns the text for a certain race
- def getRaceText(race):
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.speedrunslive.com/gamelist/game.php?game=" + GAME)
- htmlSource = sock.read()
- sock.close()
- htmlSource = htmlSource.lower()
- race = race.lower()
- begin = htmlSource.find("<th colspan=\"3\">" + race + "</th>")
- length = len(htmlSource)
- end = htmlSource.find("</table>", begin, length)
- tempracetext = htmlSource[begin:end]
- if tempracetext == '':
- return -1
- return tempracetext
- #Returns the text for top 10 players (if 10 players do not exist - should put in some code to only grab 9,8,7,6,5,4 etc..)
- def getPlayersText(amt):
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.speedrunslive.com/gamelist/game.php?game=" + GAME)
- htmlSource = sock.read()
- sock.close()
- begin = htmlSource.find("<col class=\"playersListRating\">")
- length = len(htmlSource)
- counter = 1
- end = htmlSource.find("<td>" + str((amt+1)) + "</td>", begin, length)
- tempracetext = htmlSource[begin:end]
- print tempracetext
- return tempracetext
- def getAllPlayers():
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.speedrunslive.com/gamelist/game.php?game=" + GAME)
- htmlSource = sock.read()
- sock.close()
- begin = htmlSource.find("<col class=\"playersListRating\">")
- length = len(htmlSource)
- counter = 1
- end = htmlSource.find("</div>", begin, length)
- tempracetext = htmlSource[begin:end]
- print tempracetext
- return tempracetext
- def createGameList(var):
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.speedrunslive.com/gamelist")
- htmlSource2 = sock.read()
- sock.close()
- index = htmlSource2.find("<a href=\"game.php?game=")
- thegamelist = []
- i = 0
- prevpointdiff = []
- prevpoint = 0
- postpointdiff = []
- postpoint = 0
- gamepointdiff = []
- gamepoint = 0
- #if your main game isn't #1 then create list till you find it
- while i < 100:
- #position to the next game
- index = htmlSource2.find("<a href=\"game.php?game=",index)
- index = index+23
- tempgame = getWord(htmlSource2,index,"\"").strip()
- index = index + (len(tempgame))
- index = htmlSource2.find("<td title=\"",index)
- index = index+11
- temppoints = getWord(htmlSource2,index,"p").strip()
- print "temppoint =" + str(temppoints)
- thegamelist.append([tempgame,temppoints])
- #if game is #1
- if (i==0 and tempgame == GAME):
- i = i+1
- prevpoint = 1
- #position to the next game
- index = htmlSource2.find("<a href=\"game.php?game=",index)
- index = index+23
- tempgame = getWord(htmlSource2,index,"\"").strip()
- index = index + (len(tempgame))
- index = htmlSource2.find("<td title=\"",index)
- index = index+11
- temppoints = getWord(htmlSource2,index,"p").strip()
- print "temppoint =" + str(temppoints)
- thegamelist.append([tempgame,temppoints])
- #need to pass the game then add after game and before game (your game #1)
- #find your game and the game after your game
- if (prevpoint == 1 and postpoint == 0):
- pointdiff = 0
- pointdiff = int(thegamelist[i-1][1]) - int(temppoints.strip())
- postpointdiff = [i+1,thegamelist[i][0],pointdiff]
- postpoint = 1
- gamepointdiff = [i,thegamelist[i-1][0],thegamelist[i-1][1]]
- if (i==1):
- return [gamepointdiff,postpointdiff]
- #find the game before your game (if it is not first)
- if (tempgame == GAME and i <> 0):
- pointdiff = 0
- pointdiff = int(thegamelist[i-1][1]) - int(temppoints.strip())
- prevpointdiff = [i,thegamelist[i-1][0],pointdiff]
- prevpoint = 1
- i = i+1
- print thegamelist
- print "PREV POINT DIFF::"
- print prevpointdiff
- print "POST POINT DIFF::"
- print postpointdiff
- return [gamepointdiff,prevpointdiff,postpointdiff]
- def playerExists(racer):
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.speedrunslive.com/gamelist/game.php?game=" + GAME)
- htmlSource = sock.read()
- sock.close()
- astring = getAllPlayers()
- astring = htmlSource.lower()
- player = astring.find("<a href=\"/profiles/?player=" + racer.lower())
- if player == -1:
- return 'false'
- else:
- return 'true'
- #Really Shitty code that returns the rank of a player. There has got to be a better way to do this.
- def getPlayerRank(racer):
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.speedrunslive.com/gamelist/game.php?game=" + GAME)
- htmlSource = sock.read()
- sock.close()
- astring = getAllPlayers()
- astring = htmlSource.lower()
- player = astring.find("<a href=\"/profiles/?player=" + racer)
- if player == -1:
- return -1
- player = player - 9
- while htmlSource[player] <> '#':
- player = player - 1
- numb = ''
- player = player + 1
- while htmlSource[player] <> '<':
- numb = numb + htmlSource[player]
- player = player + 1
- return numb
- # OUTSIDE FUNCTIONS - only these should be called from outside
- #
- #
- #input:shorthand version of game name
- #returns: full version of game name
- def getGameName(var):
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.speedrunslive.com/gamelist")
- htmlSource2 = sock.read()
- sock.close()
- index = htmlSource2.find("<a href=\"game.php?game=" + var)
- if (index == -1):
- return "DNE"
- else:
- index = index+25+len(var)
- tempgame = getWord(htmlSource2,index,"<").strip()
- return tempgame
- #Return the Top 5 for a certain race in an array
- def topFive(race):
- #getGamePage()
- race_lower = race.lower()
- print race + " IS THE RACE!!! "
- beginstring = ""
- beginstring = getRaceText(race_lower)
- #print beginstring
- #If the race DNE then let the user know!
- if beginstring == "":
- return "DNE" #in smwBot if we return this - he will display a nice message.
- index = 0
- counter = 0
- racearray = list()
- stringracearray = ""
- while counter < 5:
- index = beginstring.find("<a href=\"/profiles/?player=")
- if index == -1:
- break
- index = index + 27
- tempracer = (getWord(beginstring,index,"\""))
- if (tempracer == "*NO MORE*"):
- break
- tempnum = len(tempracer)
- index = index + (tempnum*2) + 13
- temptime = getWord(beginstring,index,"<")
- if (temptime == "*NO MORE*"):
- break
- #for now just output to a string
- stringracearray = stringracearray + str(counter+1) + ")" + tempracer + "-" + temptime + " | "
- endof = len(beginstring)
- beginstring = beginstring[index:endof]
- counter = counter + 1
- print stringracearray
- #Since smwBot doesn't like arrays looks like we have to convert to string.. for now
- return stringracearray
- def rank(name):
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.speedrunslive.com/gamelist/game.php?game=" + GAME)
- htmlSource = sock.read()
- sock.close()
- #get game name
- gamename = getGameName(GAME)
- name = name.lower()
- playerRank = -1
- playerRank = getPlayerRank(name)
- playerRank = int(playerRank)
- if (playerRank > 0 and playerRank <6) or name == 'fuzzey_ninja':
- #get random chuck noris fact and replace name with racer
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.funnyconcept.com/")
- chuckSource = sock.read()
- sock.close()
- index = chuckSource.find("<div class=\"jokebox\">")
- fact = getWord(chuckSource,index+23,"<")
- fact = fact.replace("Chuck Norris", name)
- print "You are Ranked #" + str(playerRank)
- fact = ' '.join(fact.split())
- print fact
- returnText = ["You are Ranked #" + str(playerRank) + " in "+gamename+"!", fact.strip()]
- return returnText
- elif playerRank >= 6 and playerRank <=10:
- returnText = ["You are Ranked #" + str(playerRank) + " in "+gamename+"!", "You are in the top 10, not too Shabby!"]
- return returnText
- elif playerRank >= 11 and playerRank <=14:
- returnText = ["You are Ranked #" + str(playerRank) + " in "+gamename+"!", "Ehh you are alright. I guess. (not really)"]
- return returnText
- elif playerRank > 14 or name == 'trakof':
- sock = urllib.urlopen("http://insultgenerator.net/")
- insultSource = sock.read()
- sock.close()
- index = insultSource.find("<h1 class=\"insult\">")
- tempy = getWord(insultSource,index,",")
- lent = len(tempy)
- insulty = getWord(insultSource,index+lent,'<')
- fact = "Nice rank," + insulty
- #print "You are Ranked #" + str(playerRank)
- #print fact
- returnText = ["You are Ranked #" + str(playerRank) + " in "+gamename+"!", fact.strip()]
- return returnText
- #if player dne dont bother getting a message
- else:
- returnText = ["You have never raced "+gamename+"!!", "Time to Start!! http://www.smwwiki.com"]
- return returnText
- def blechy():
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://www.speedrunslive.com/gamelist/game.php?game=" + GAME)
- htmlSource = sock.read()
- sock.close()
- #get random chuck noris fact and replace name with racer
- #import the srl game page into htmlSource string
- sock = urllib.urlopen("http://toykeeper.net/programs/mad/compliments")
- compSource = sock.read()
- sock.close()
- index = compSource.find("<h3 class=\"blurb_title_1\">")
- comp = getWord(compSource,index+26,"<")
- comp = ' '.join(comp.split())
- return comp
- #Return the Top 5 for a certain race in an array
- def topTen():
- gamename = getGameName(GAME)
- print GAME + " IS THE GAME!!! "
- beginstring = ""
- beginstring = getPlayersText(10) #~~~~~~Ten is the amount of players to return make variable?
- #print beginstring
- #If the race DNE then let the user know!
- if beginstring == "":
- return "DNE" #in smwBot if we return this - he will display a nice message.
- #Check to see if there is less than 10 racers!
- indexs = beginstring.find("Players: <strong>")
- indexs = indexs + 17
- numracers = int(getWord(beginstring,indexs,"<"))
- numberofracers = 10
- if (numracers <10):
- numberofracers = numracers
- index = 0
- counter = 0
- racearray = list()
- stringracearray = ""
- while counter < numberofracers: #~~~~~~Ten is the amount of players to return (make variable)? Or could make when index is greater than length of text.
- index = beginstring.find("<a href=\"/profiles/?player=")
- tempracer = (getWord(beginstring,index+27,"\""))
- tempnum = len(tempracer)
- index = index + (tempnum*2) + 37
- temppoints = getWord(beginstring,index,"<")
- temppoints = temppoints.strip()
- #print tempracer + " - " + temppoints
- #print "THE INDEX IS: "
- #print index
- #######put string in next spot in list
- #######racearray.append([tempracer,temptime]) ###not doing this yet because smwBot doesn't like to print arrays
- #for now just output to a string
- stringracearray = stringracearray + str(counter+1) + ": " + tempracer + " - " + temppoints + " | "
- endof = len(beginstring)
- beginstring = beginstring[index:endof]
- counter = counter + 1
- #print stringracearray
- #Since smwBot doesn't like arrays looks like we have to convert to string.. for now
- return [gamename,stringracearray]
- #A function that shows our rank AND
- #shows where we are in comparison to:
- # 1) Game above
- # 2) Game below
- # (if 1 or 2 DNE) 3) game passed as variable
- def gameRank(var):
- #get game name
- gamename = getGameName(GAME)
- ranklist = []
- ranklist = createGameList(var)
- print ranklist
- if (len(ranklist[0]) ==0):
- return "DNE"
- elif (len(ranklist) == 2):
- returnlist=[]
- returnlist.append(gamename+" is Ranked #" + str(ranklist[0][0]) + " with " + str(ranklist[0][2]) + " points!")
- returnlist.append("It is " + str(ranklist[1][2]) + " point(s) ahead #" + str(ranklist[1][0]) + " " + getGameName(str(ranklist[1][1])))
- return returnlist
- else:
- returnlist=[]
- returnlist.append(gamename+" is Ranked #" + str(ranklist[0][0]) + " with " + str(ranklist[0][2]) + " points!")
- returnlist.append("It is " + str(ranklist[1][2]) + " point(s) behind #" + str(ranklist[1][0]) + " " + getGameName(str(ranklist[1][1])))
- returnlist.append("It is " + str(ranklist[2][2]) + " point(s) ahead #" + str(ranklist[2][0]) + " " + getGameName(str(ranklist[2][1])))
- return returnlist
- ########THINGS TO RUN
- #print getRaceText("ANY%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement