Last active
August 29, 2015 14:21
-
-
Save EliteMasterEric/e3b11135f5300f249afb to your computer and use it in GitHub Desktop.
Reddit PRAW Flair Statistics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| import time | |
| import random | |
| import os | |
| import requests.exceptions | |
| import re | |
| import praw | |
| from traceback import format_exc | |
| ''' | |
| @author: EliteMasterEric | |
| @note: Utilise access | |
| ''' | |
| reddit_moderator_username = 'username' | |
| reddit_moderator_password = 'password' | |
| reddit_subreddit_name = 'subreddit' | |
| reddit_statistics_page_name = "flair_statistics" | |
| reddit_user_agent_moderator = "/u/"+reddit_moderator_username+'s_API_ModAgent' | |
| def praw_access_flairs(reddit_subreddit_name, r): | |
| #Get possible choices. | |
| reddit_flair_possible_list = r.get_flair_choices(reddit_subreddit_name)["choices"] | |
| #Getthe | |
| reddit_flair_actual_list = r.get_flair_list(reddit_subreddit_name,limit=None) | |
| #Record actual flairs | |
| reddit_flair_current_choices = {} | |
| reddit_flair_total_users = 0; | |
| reddit_flair_text_users = 0; | |
| for i in reddit_flair_possible_list: | |
| print("Flair choice: "+i["flair_css_class"]) | |
| #[6:] excludes the "flair-" | |
| reddit_flair_current_choices[str(i["flair_css_class"])[6:]] = 0 | |
| #Add a flair that does not show in the list. | |
| #reddit_flair_current_choices["flairname"] = 0 | |
| for item in reddit_flair_actual_list: | |
| reddit_flair_total_users += 1 | |
| if(not (item["flair_text"] == None or item["flair_text"] == "")): | |
| reddit_flair_text_users += 1 | |
| if(item["flair_css_class"] == None): | |
| print(item["user"]+" chose flair: None") | |
| else: | |
| #This is a fix for a problem where some people had old or outdated flairs. | |
| try: | |
| i = reddit_flair_current_choices[item["flair_css_class"]] | |
| except KeyError: | |
| r.set_flair(reddit_subreddit_name, item["user"], "", "") | |
| print(item["user"]+"'s flair reset.") | |
| else: | |
| if(reddit_flair_current_choices[item["flair_css_class"]] == None): | |
| r.set_flair(reddit_subreddit_name, item["user"], "", "") | |
| #print(item["user"]+" chose flair: None") | |
| #All the above is fixes for old or broken flairs. | |
| else: | |
| #Logging made the script take longer. | |
| #print(item["user"]+" chose flair: "+item["flair_css_class"]) | |
| reddit_flair_current_choices[item["flair_css_class"]] += 1 | |
| print("Flair counting done.") | |
| #Sort the list of actual flairs in order by how many people use them. operator.itemgetter(1) is the equivalent of item[1] | |
| #Get the current time. | |
| datetime_current = datetime.datetime.now() | |
| datetime_current_string = datetime_current.strftime("%c") | |
| #Write to the wiki. | |
| reddit_wiki_contents = "#Flair Statistics" | |
| reddit_wiki_contents += "\r\n\r\n" | |
| reddit_wiki_contents += "A list of flairs and how many users use them." | |
| reddit_wiki_contents += "\r\n\r\n" | |
| reddit_wiki_contents += "This page updates regularly. Last Updated: **"+ str(datetime.datetime.now().strftime("%c"))+"**" | |
| reddit_wiki_contents += "\r\n\r\n" | |
| reddit_wiki_contents += "Script created by /u/EliteMasterEric using a PRAW bot." | |
| reddit_wiki_contents += "\r\n\r\n" | |
| reddit_wiki_contents += "Click the **Number of Users** header to sort by the number of users using each flair." | |
| reddit_wiki_contents += "\r\n\r\n" | |
| reddit_wiki_contents += "Flair | Flair Name | Number of Users" | |
| reddit_wiki_contents += "\r\n" | |
| reddit_wiki_contents += "------|------------|----------------\r\n" | |
| for index, item in reddit_flair_current_choices.items(): | |
| #Replace any digets with blanks | |
| name = re.sub("\d+", "", index) | |
| reddit_wiki_contents += "[](#su-{0})|{1}|{2}\r\n".format(name,name,item) | |
| reddit_wiki_contents += "\r\n\r\n" | |
| reddit_wiki_contents += str((reddit_flair_text_users/reddit_flair_total_users)*100) | |
| reddit_wiki_contents += "% of users have flair text. There are " | |
| reddit_wiki_contents += str(reddit_flair_total_users) + " users, and " | |
| reddit_wiki_contents += str(reddit_flair_text_users) + " have flair text." | |
| r.edit_wiki_page(reddit_subreddit_name,reddit_statistics_page_name,reddit_wiki_contents,reason="Statistics update "+str(datetime.datetime.now())) | |
| print("Statistics wiki page updated : "+str(datetime.datetime.now())); | |
| if __name__ == '__main__': | |
| print("Creating flair statistics...") | |
| try: | |
| #Login and authenticate as a moderator. | |
| r = praw.Reddit(user_agent=reddit_user_agent_moderator) | |
| r.login(reddit_moderator_username, reddit_moderator_password) | |
| #Update the wiki page for flair statistics | |
| praw_access_flairs(reddit_subreddit_name, r) | |
| except Exception as error: | |
| #Ensure exceptions are written to the log file. | |
| print(format_exc()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment