Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- import urllib.request, json, time
- """rs is the reddit spacing, so a single \n is enough to get an actual newline"""
- rs=" "
- def getChat(chaturl=None, player_filter=None):
- """creates a list of strings in the form Playername @ Time: Content"""
- #example for chaturl: https://api.opendota.com/api/matches/3092420917
- response = urllib.request.urlopen(chaturl)
- data = json.loads(response.read().decode())
- #If no Chat is found, the game is probably not parsed on opendota
- if data["chat"]==None:
- return ["No chat found for MatchID:"+str(matchid)+", check parsing status"]
- #get potential replay url
- replay_url = data["replay_url"]
- if replay_url==None:
- replay_url="No replay_url found."
- #filter the chat for one player only, if playername is defined
- player=player_filter
- #print("MatchID: "+str(matchid)+" and Replay link: "+replay_url)
- chat = data["chat"]
- matchid = data["match_id"]
- relist = ["MatchID: "+str(matchid)+" and replay link: "+replay_url+rs]
- #go thru all chat lines and add the formated name:time:text
- #player name will be a number, if anoymous
- for part in chat:
- if not part["unit"]==player and not player==None:
- continue
- relist.append(part["unit"]+" @ "+str(part["time"])+"s: "+part["key"]+rs)
- return relist
- def main():
- #filters chat by player, if set to True
- #the return value of personaname is used as a playername filter
- filterplayer = False
- #The player ID you want the chat from
- #example: https://www.opendota.com/players/84513788 <-the last digits
- playerid = str(17042107)
- #the amount matches to be parsed, be rational
- limit = str(5)
- #request player info to extract matches: https://docs.opendota.com/
- request = urllib.request.urlopen("https://api.opendota.com/api/players/"+playerid)
- player_data = json.loads(request.read().decode())
- profile = player_data['profile']
- #print(str(profile["personaname"])+" "+str(profile["name"]))
- #limit to 1 request per second @ opendota
- time.sleep(1)
- #request %limit amount of matches
- request = urllib.request.urlopen("https://api.opendota.com/api/players/"+playerid+"/matches?limit="+limit)
- match_data = json.loads(request.read().decode())
- #open output file for different combinations
- #format: %Player %Limit %fancy ending to show reddit combatibilty
- try:
- file=open(profile["personaname"]+"_"+limit+"_matches.red","w", encoding="utf-8")
- #iterate over all matches
- for info in match_data:
- match_id=str(info["match_id"])
- chaturl="https://api.opendota.com/api/matches/"+match_id+"/chat"
- if filterplayer:
- output=getChat(chaturl, profile["personaname"])
- else:
- output=getChat(chaturl, None)
- for text in output:
- file.write(text+"\n")
- #put in a empty line to pretty things up
- file.write("\n")
- #opendota wants 1 request per second i.e. 1 match request per second
- time.sleep(1)
- except Exception:
- raise
- finally:
- file.close()
- #access debug
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement