Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- CSGO_STATS_BOT Source Code
- BETA v0.12 7/29/2014
- Created by: Quack Lord
- How it works:
- The bot scrapes HLTV.org for certain entries about various players in
- a post or when summoned with +/u/CSGO_STATS_BOT. It then saves the ID
- of every post/comment to a text file so it will not comment to the
- same one twice, preventing spam. Since HLTV.org doesn't neatly create
- a list of players for me, I had to do it myself with update_Playerlist().
- The rest of how it works is explained in comments.
- I am going to give a brief explanation of what everything does rather
- than how I do it because I don't have the time to do that. If you have
- any question on HOW or WHY I did something, pm me @ /u/CSGO_STATS_BOT.
- Feel free to use/modify this code in anyway you wish except for the
- reproducing my bot. (i.e I don't want 2 of my bot running around) If
- you come up with a major change, features, etc that you add to this bot
- please send me the source code and I will add it in and give you credit
- where deserved.
- '''
- import sys
- import time
- import praw
- from time import gmtime, strftime
- import urllib.request
- from pprint import pprint
- version = "BETA v0.21"
- #Creates a list of every player in HLTV.org by going through every ID 1-8410 to
- #check if the name is not N/A and they have played more than 1 map. If their name
- #is N/A or they haven't played a map, it adds a line N/A in the text file, otherwise
- #it adds their name. This will be used later.
- def update_Playerlist():
- i = file_len("PlayerIDs.txt")
- for b in range (1, 841-int(i/10)):
- with open("PlayerIDs.txt","a") as f:
- for c in range (1, 11):
- i = i+1
- url = "http://www.hltv.org/?pageid=173&playerid="+str(i)+"&eventid=0&gameid=2"
- uf = urllib.request.urlopen(url)
- text = str(uf.read())
- index = text.index("Player stats: ")+14
- name = text[index:].split(" ")[0]
- index = text.index("Maps played")+126
- maps = int(text[index:index+1])
- if name != "N/A" and maps > 0:
- f.write(name+"\n")
- print(str(i)+"- " + name+":"+str(maps))
- else:
- f.write("N/A\n")
- print(str(i)+"- " +"N/A")
- #Opens the text file created by update_Playerlist() and adds every name to a list,
- #the reason I had to have the N/A in the text file was so that here it would keep
- #the player's ID associated with them.
- def getPlayers():
- players = []
- with open("PlayerIDs.txt") as f:
- for line in f:
- players.append(line.replace("\n",""))
- return players
- #Simply gets the number of lines in a file.
- def file_len(fname):
- with open(fname) as f:
- for i, l in enumerate(f):
- pass
- return i + 1
- #Modified bubble sort I used to sort the players by ranking. I had to have 3 arrays:
- #Player names: Stored the name of every player
- #Player Ratings: Stored the rating of every player
- #Indexes: created in the sort to keep track of where every player moved to.
- def bubble_sort(arr1,arr2):
- indexes = []
- for i in range(0, len(arr1)):
- indexes.append(i)
- unsorted = True
- while unsorted:
- unsorted = False
- for i in range(1,len(arr1)):
- if arr2[i] > arr2[i-1]:
- temp = arr2[i]
- arr2[i] = arr2[i-1]
- arr2[i-1] = temp
- temp = arr1[i]
- arr1[i] = arr1[i-1]
- arr1[i-1] = temp
- temp = indexes[i]
- indexes[i] = indexes[i-1]
- indexes[i-1] = temp
- unsorted = True
- return indexes
- #Gets the player infor for every player in list inPost. The data points it gathers
- #are K/D ratio, Rounds played, Average Kills, Team, HLTV rating, profile URL, Team URL.
- #I used a VERY crude way of scraping the site for the data I needed. I can't go into
- #detail without dragging on forever, so PM if you want to know the HOW/WHY behind it.
- #Then it starts to put together the response with a lot of formatting and the data found.
- #It calls the bubble_sort method to determine the order of the people in the post.
- def get_player_info(inPost):
- Reply = "Player Name | Primary Team | K/D Ratio | Kills per round | More\n"+":----------|:------------|:---------|:---------------|:-----:\n"
- KDs = []
- Rounds = []
- AverageKills = []
- Team = []
- TeamURLs = []
- Ratings = []
- URLs = []
- for i in range (0, len(inPost)):
- url = "http://www.hltv.org/?pageid=173&playerid="+str(inPost[i].split("|")[0])+"&eventid=0&gameid=2"
- uf = urllib.request.urlopen(url)
- text = str(uf.read())
- URLs.append(url)
- index = text.index("K/D Ratio")+124
- index2 = text[index:].index("</div>")
- KDs.append(text[index:index+index2])
- index = text.index("Rounds played")+128
- index2 = text[index:].index("</div>")
- Rounds.append(text[index:index+index2])
- index = text.index("Average kills per round")+138
- index2 = text[index:].index("</div>")
- AverageKills.append(text[index:index+index2])
- index = text.index("Rating <a href")+259
- index2 = text[index:].index("</div>")
- Ratings.append(float(text[index:index+index2]))
- index = text.index("Primary team:")+129
- index = index + text[index:].index(">")+1
- index2 = text[index:].index("</a>")
- Team.append(text[index:index+index2])
- index = text.index("Primary team:")+138
- index2 = text[index:].index("\"")
- TeamURLs.append("http://www.hltv.org"+text[index:index+index2].replace("&","&"))
- if len(inPost) > 0:
- indexes=bubble_sort(inPost,Ratings)
- for i in range(0, len(inPost)):
- # print(str(Ratings[i]) + " " + inPost[i])
- Reply = Reply + inPost[i].split("|")[1] + "(["+str(Ratings[i])+"](http://www.hltv.org/?pageid=242)) | [" + Team[indexes[i]] + "]("+TeamURLs[indexes[i]]+") | " + KDs[indexes[i]] + " | " + AverageKills[indexes[i]] + " | [HLTV](" + URLs[indexes[i]] + ")\n"
- Reply = Reply + "^Players ^ordered ^by ^rating ^on ^HLTV.org \n\n**NOTE:** *Some players may not appear because I have no stats for them on HLTV, I am looking for more websites to gather data from.*\n\n**DISCLAIMER** *You should not use these stats to solely determine your bet, please do more research before you bet as this bot is not responsible for your bets*\n\n Questions? Comments? Improvements? Let me know [on this bot's thread!](http://www.reddit.com/r/csgobetting/comments/2byr6w/player_stats_bot_for_this_subreddit/)\n \n \n Moderators, Please remove (or message me about) any spam created by this bot (SORRY!) and message me with any bugs you notice, thanks! \n*Current Version: " + version + "* [source](http://pastebin.com/WC52XYeK)"
- return Reply
- #Stores comment ID to a text file so it doesn't double post.
- def store_comment_ID(comment):
- with open("CommentIDs.txt", "a") as f:
- f.write(comment.id+"\n")
- #Stores post ID to a text file so it doesn't double post.
- def store_submission_ID(submission):
- with open("SubmissionIDs.txt", "a") as f:
- f.write(submission.id+"\n")
- #Checks to see if the supplied comment ID has been replied to yet.
- def check_comment_ID(cid):
- with open("CommentIDs.txt") as f:
- for line in f:
- if cid in line:
- return False
- return True
- #Checks to see if the supplied post ID has been replied to yet.
- def check_submission_ID(sid):
- with open("SubmissionIDs.txt") as f:
- for line in f:
- if sid in line:
- return False
- return True
- #Main method:
- #Logs into reddit and gets the newest 20 posts to check through.
- #First it will check for posts with the flair 'Match' so that it only
- #goes to matches that haven't been finished yet. It checks the selftext
- #for names and then saves them to a list and send them up to get_player_info()
- #Next it goes to comments, searching any of the newest 20 posts for the
- #+/u/CSGO_STATS_BOT summon. Once it finds the summon, it will search the
- #same way it does the post. Then both post the replies.
- #update_Playerlist()
- r = praw.Reddit('Counter Strike:Global Offensive player stats bot version: ' + version)
- r.login("USERNAME","PASSWORD")
- already_done = set()
- players = getPlayers()
- print ("Bot logged in...")
- while True:
- subreddit = r.get_subreddit('csgobetting')
- subs = [];
- subs.append(r.get_submission(submission_id="2byr6w"))
- for submission in subreddit.get_new(limit=10):
- subs.append(submission)
- for submission in subs:
- inPost = []
- #Each Post
- if submission.link_flair_text == "Match" and check_submission_ID(submission.id):
- text = submission.selftext.replace(","," ").lower().split()
- for i in range (0, len(players)):
- player = players[i].lower()
- if player in text and players[i] != "N/A" and players[i] not in inPost:
- print("Found "+players[i]+"(#"+str(i+1)+") in the post!")
- inPost.append(str(i+1)+"|"+players[i])
- try:
- submission.add_comment(get_player_info(inPost))
- store_submission_ID(submission)
- except:
- print("Error! not posting(post) " + submission.id)
- pass
- #Each comment
- submission.replace_more_comments(limit=25, threshold=1)
- flat_comments = praw.helpers.flatten_tree(submission.comments)
- for comment in flat_comments:
- inComment = []
- text = comment.body.replace(","," ").lower().split()
- if "+/u/csgo_stats_bot" in text and check_comment_ID(comment.id):
- for i in range (0, len(players)):
- player = players[i].lower()
- if player in text and players[i] != "N/A" and players[i] not in inComment:
- print("Found "+players[i]+"(#"+str(i+1)+") in a comment!")
- inComment.append(str(i+1)+"|"+players[i])
- if len(inComment) > 0:
- comment.reply(get_player_info(inComment))
- store_comment_ID(comment)
- print("Sleeping for 1 minutes. Time of sleep: " + strftime("%m-%d-%Y %H:%M:%S"))
- time.sleep(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement