Advertisement
Guest User

/r/requestabot

a guest
Feb 7th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. # code written by /u/wogbaby and /u/zkr31
  2.  
  3. from datetime import datetime
  4. from time import sleep
  5. import praw
  6. import re
  7.  
  8. r = praw.Reddit(client_id='YOUR APP CLIENT ID',
  9.                 client_secret='YOUR APP CLIENT SECRET',
  10.                 username='YOUR REDDIT USERNAME',
  11.                 password='YOUR REDDIT PASSWORD',
  12.                 user_agent='python3:FindARedditStats:v1 (by /u/YOUR REDDIT USERNAME)')
  13.  
  14. findareddit = r.subreddit('findareddit')
  15.  
  16.  
  17. def main():
  18.     while True:
  19.         try:
  20.             update()
  21.             sleep(30 * 60)  # wait for 30 minutes
  22.         except Exception as e:  # print all exceptions instead of crashing
  23.             print('\t' + str(e))
  24.             sleep(60)
  25.             continue
  26.  
  27.  
  28. def update():
  29.     print('\ngathering statistics...')
  30.     past_day_timestamp = datetime.utcnow().timestamp() - 86400  # current time - number of seconds in 24 hours
  31.     new_posts = findareddit.new(limit=None)
  32.  
  33.     stats = {'foundflair': 0, 'unansweredflair': 0, 'unflaired': 0}
  34.     for p in new_posts:
  35.         if p.created_utc < past_day_timestamp:
  36.             break  # don't look further back than 24 hours
  37.  
  38.         if p.link_flair_css_class in stats.keys():
  39.             stats[p.link_flair_css_class] += 1
  40.         else:
  41.             stats['unflaired'] += 1
  42.  
  43.     # get percentages
  44.     total = sum(stats.values())
  45.     found = round((stats['foundflair'] / total) * 100)
  46.     unanswered = round((stats['unansweredflair'] / total) * 100)
  47.     unflaired = round((stats['unflaired'] / total) * 100)
  48.  
  49.     print('updating sidebar...')
  50.     sidebar_text = findareddit.mod.settings()['description']  # get the sidebar text
  51.     regex = re.compile('^\#\#\#\s.*', re.MULTILINE)
  52.     statistics_text = ('\n'.join(re.findall(regex, sidebar_text)))  # find the line starting with ###
  53.     new_statistics = '### LAST 24 HOURS: FOUND: {0}% UNANSWERED: {1}% WAITING ON OP: {2}% ({3} TOTAL)'
  54.     sidebar_text = sidebar_text.replace(statistics_text, new_statistics.format(found, unanswered, unflaired, total))
  55.     findareddit.mod.update(description=sidebar_text)  # update sidebar with new statistics
  56.  
  57.     print('done')
  58.  
  59.  
  60. if __name__ == '__main__':
  61.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement