Advertisement
PapstJL4U

Stalk the chat of dota players

Apr 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import urllib.request, json, time
  3. """rs is the reddit spacing, so a single \n is enough to get an actual newline"""
  4. rs="    "
  5. def getChat(chaturl=None, player_filter=None):
  6.     """creates a list of strings in the form Playername @ Time: Content"""
  7.     #example for chaturl: https://api.opendota.com/api/matches/3092420917
  8.     response = urllib.request.urlopen(chaturl)
  9.     data = json.loads(response.read().decode())
  10.  
  11.     #If no Chat is found, the game is probably not parsed on opendota
  12.     if data["chat"]==None:
  13.         return ["No chat found for MatchID:"+str(matchid)+", check parsing status"]
  14.  
  15.     #get potential replay url
  16.     replay_url = data["replay_url"]
  17.     if replay_url==None:
  18.         replay_url="No replay_url found."
  19.  
  20.     #filter the chat for one player only, if playername is defined
  21.     player=player_filter
  22.     #print("MatchID: "+str(matchid)+" and Replay link: "+replay_url)
  23.     chat = data["chat"]
  24.     matchid = data["match_id"]
  25.     relist = ["MatchID: "+str(matchid)+" and replay link: "+replay_url+rs]
  26.  
  27.     #go thru all chat lines and add the formated name:time:text
  28.     #player name will be a number, if anoymous
  29.     for part in chat:
  30.         if not part["unit"]==player and not player==None:
  31.             continue
  32.         relist.append(part["unit"]+" @ "+str(part["time"])+"s: "+part["key"]+rs)
  33.     return relist
  34.  
  35. def main():
  36.    
  37.     #filters chat by player, if set to True
  38.     #the return value of personaname is used as a playername filter
  39.     filterplayer = False
  40.  
  41.     #The player ID you want the chat from
  42.     #example: https://www.opendota.com/players/84513788 <-the last digits
  43.     playerid = str(17042107)
  44.  
  45.     #the amount matches to be parsed, be rational
  46.     limit = str(5)
  47.  
  48.     #request player info to extract matches: https://docs.opendota.com/
  49.     request =  urllib.request.urlopen("https://api.opendota.com/api/players/"+playerid)
  50.     player_data = json.loads(request.read().decode())
  51.     profile = player_data['profile']
  52.     #print(str(profile["personaname"])+" "+str(profile["name"]))
  53.  
  54.     #limit to 1 request per second @ opendota
  55.     time.sleep(1)
  56.  
  57.     #request %limit amount of matches
  58.     request = urllib.request.urlopen("https://api.opendota.com/api/players/"+playerid+"/matches?limit="+limit)
  59.     match_data = json.loads(request.read().decode())
  60.  
  61.     #open output file for different combinations
  62.     #format: %Player %Limit %fancy ending to show reddit combatibilty
  63.     try:
  64.         file=open(profile["personaname"]+"_"+limit+"_matches.red","w", encoding="utf-8")
  65.  
  66.         #iterate over all matches
  67.         for info in match_data:
  68.             match_id=str(info["match_id"])
  69.             chaturl="https://api.opendota.com/api/matches/"+match_id+"/chat"
  70.  
  71.             if filterplayer:
  72.                 output=getChat(chaturl, profile["personaname"])
  73.             else:
  74.                 output=getChat(chaturl, None)
  75.             for text in output:
  76.                 file.write(text+"\n")
  77.  
  78.             #put in a empty line to pretty things up
  79.             file.write("\n")
  80.             #opendota wants 1 request per second i.e. 1 match request per second
  81.             time.sleep(1)
  82.            
  83.     except Exception:
  84.         raise
  85.     finally:
  86.         file.close()
  87.  
  88. #access debug
  89. if __name__ == '__main__':
  90.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement