Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # code written by /u/wogbaby and /u/zkr31
- from datetime import datetime
- from time import sleep
- import praw
- import re
- r = praw.Reddit(client_id='YOUR APP CLIENT ID',
- client_secret='YOUR APP CLIENT SECRET',
- username='YOUR REDDIT USERNAME',
- password='YOUR REDDIT PASSWORD',
- user_agent='python3:FindARedditStats:v1 (by /u/YOUR REDDIT USERNAME)')
- findareddit = r.subreddit('findareddit')
- def main():
- while True:
- try:
- update()
- sleep(30 * 60) # wait for 30 minutes
- except Exception as e: # print all exceptions instead of crashing
- print('\t' + str(e))
- sleep(60)
- continue
- def update():
- print('\ngathering statistics...')
- past_day_timestamp = datetime.utcnow().timestamp() - 86400 # current time - number of seconds in 24 hours
- new_posts = findareddit.new(limit=None)
- stats = {'foundflair': 0, 'unansweredflair': 0, 'unflaired': 0}
- for p in new_posts:
- if p.created_utc < past_day_timestamp:
- break # don't look further back than 24 hours
- if p.link_flair_css_class in stats.keys():
- stats[p.link_flair_css_class] += 1
- else:
- stats['unflaired'] += 1
- # get percentages
- total = sum(stats.values())
- found = round((stats['foundflair'] / total) * 100)
- unanswered = round((stats['unansweredflair'] / total) * 100)
- unflaired = round((stats['unflaired'] / total) * 100)
- print('updating sidebar...')
- sidebar_text = findareddit.mod.settings()['description'] # get the sidebar text
- regex = re.compile('^\#\#\#\s.*', re.MULTILINE)
- statistics_text = ('\n'.join(re.findall(regex, sidebar_text))) # find the line starting with ###
- new_statistics = '### LAST 24 HOURS: FOUND: {0}% UNANSWERED: {1}% WAITING ON OP: {2}% ({3} TOTAL)'
- sidebar_text = sidebar_text.replace(statistics_text, new_statistics.format(found, unanswered, unflaired, total))
- findareddit.mod.update(description=sidebar_text) # update sidebar with new statistics
- print('done')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement