-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGoldstatsbot1.py
141 lines (76 loc) · 3.02 KB
/
Goldstatsbot1.py
1
import timeimport prawimport randomimport reimport bmemcached# Login in to Reddit and identifies the botlogin_info = [os.environ['REDDIT_USERNAME'], os.environ['REDDIT_PASSWORD']]username = login_info[0]password = login_info[1]r = praw.Reddit('goldstatsbot')r.login(username,password)# creates sets where the posts it has already counted are stored.already_done = set()#starts off the counts at 0count = 0c = 0#defines the summoning commandName = ['+/u/goldstatsbot']#Finds posts in /r/all/gilded that have been give gold and adds them to the countdef find_gild(): try: subreddit_comments = r.get_comments('all',gilded_only=True) for comment in subreddit_comments: is_gilded = comment.gilded if comment.id not in already_done: #finds new gilded posts, adds them to the count, then waits three seconds, per reddits API rules. new = int(is_gilded) global count count+=new print int(count) already_done.add(comment.id) time.sleep(3) break except urllib2.HTTPError, err: if err.code == 404: time.sleep(5) else: time.sleep(3) pass def find_cash(): #Finds the amount of USD made from gold in the past 24 hours global cash amount_bought = int(count)*4 def find_summons(): #Searches for summoning command subreddit_comment = r.get_comments('all', limit=1000) for comment in subreddit_comment: op_text = comment.body has_praw = any(string in op_text for string in Name) global count if comment.id not in already_done and has_praw: #replies with some stats, edit the text to include more info comment.reply('Thank you for calling me! \n\nUsers have bought ' + str(count) + ' pieces of Reddit Gold since midnight (EST). Go [here](http://www.reddit.com/gold/about) to learn more about Reddit Gold. \n\n^This ^bot ^is ^currently ^in ^testing, ^stats ^are ^approximate \n\n^I ^was ^made ^by ^/u/healdb ^through ^[bots4doge.com](http://bots4doge.tk)') already_done.add(comment.id) breakwhile True: #Runs the count program find_gild() find_summons() global count global c time.sleep(1) c+=3 #Resets the count every 24 hours and writes the data down if c%3600==0: obj = open('data.txt', 'a') print " The number of people who were given reddit gold in the past hour is- " + str(count) + " " find_cash() from datetime import datetime obj.write(str(datetime.now()) + ', ' + str(count) + '\n') obj.close() print 'The past hour has been counted and written in the text file. Starting on the next hour...' if c==86400: print 'The past 24 hours has been counted and written in the text file. Starting on the next 24 hours...' count = 0 c = 0