-
-
Save ChangedNameTo/ceddb0e404386f3596af to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
import praw | |
import sys | |
import os | |
from time import gmtime, strftime | |
try: | |
from flair_list import flairs | |
except ImportError as e: | |
print("Flairs file can't be accessed\n") | |
print(e) | |
sys.exit() | |
except SyntaxError as e: | |
print("There is a syntax error in the flair list\n") | |
print(e) | |
sys.exit() | |
class FlairBot: | |
# User agent that PRAW uses to identify your bot to reddit | |
USER_AGENT = 'ubuntu:thealpacalypse.me:v1 (run by /u/tehalpacalypse)' | |
# User name/password of the account that the bot will run through | |
USER_NAME = '' | |
PASSWD = '' | |
# Blacklist for users abusing the flair system | |
BLACKLIST = ['sampleuser', 'sampleUSER2'] | |
""" The SUBJECT will be the default subject of your PMs | |
when you create the URLs, eg. | |
reddit.com/message/compose/?to=some_user&subject=flair&message=some_message | |
PMs require a subject, but it's also a simple way of identifying | |
PMs that are directed towards the flairs and not just a general PM""" | |
SUBJECT = 'flair' | |
# TARGET_SUB is the name of the subreddit without the leading /r/ | |
TARGET_SUB = 'worldpowers' | |
# Turn on output to log file in current directory - log.txt | |
LOGGING = True | |
# Class variable to hold the PRAW instance | |
r = None | |
# Class variable to hold the unread pms | |
pms = None | |
def init(self): | |
print("Get 1") | |
if self.LOGGING: | |
os.chdir(os.path.dirname(os.path.abspath(__file__))) | |
self.login() | |
def login(self): | |
""" Log in to the account being used for the bot """ | |
print("Get 2") | |
try: | |
self.r = praw.Reddit(user_agent=self.USER_AGENT) | |
self.r.login(self.USER_NAME, self.PASSWD) | |
self.fetch_pms() | |
except: | |
raise | |
def fetch_pms(self): | |
""" Get a listing of all unread PMs for the user account """ | |
print("Get 3") | |
self.pms = self.r.get_unread(limit=None) | |
if self.pms is not None: | |
self.process_pms() | |
def process_pms(self): | |
print("Get 4") | |
i = 0 | |
for pm in self.pms: | |
i = i + 1 | |
print("PM #", i) | |
if str(pm.subject) == self.SUBJECT: | |
print("Get 4.1") | |
author = str(pm.author) # Author of the PM | |
if author.lower() in (user.lower() for user in self.BLACKLIST): | |
print("Get 4.2") | |
continue | |
content = str(pm.body) # Content of the PM | |
index = content.find(":") | |
if index != -1: | |
print("Get 4.3") | |
continue | |
newflair = content[:index].strip()# Substrings the PM to get flair only | |
subreddit = self.r.get_subreddit(self.TARGET_SUB) | |
print("Get 4.4") | |
if newflair in flairs: | |
print("Get 4.5") | |
# Get the flair text that corresponds with the class name | |
flair_text = content[index + 1:].strip() | |
subreddit.set_flair(author, flair_text, newflair) | |
if self.LOGGING: | |
print("Get 4.6") | |
self.log(author, content, flair_text, newflair) | |
pm.mark_as_read() # Mark processed PM as read | |
def log(self, author, content, flair_text, newflair): | |
print("Get 5") | |
with open('log.txt', 'a', encoding="UTF-8") as logfile: | |
time_now = strftime("%Y-%m-%d %H:%M:%S", gmtime()) | |
log_text = 'Added: ' + author + ' : ' \ | |
+ flair_text + ' : ' \ | |
+ newflair + ' @ ' + time_now + ' EST\n' | |
logfile.write(log_text) | |
FlairBot().init() |
This file contains 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
Get 1 | |
Get 2 | |
Get 3 | |
Get 4 | |
PM # 1 | |
Get 4.1 | |
Get 4.3 | |
PM # 2 | |
Get 4.1 | |
Get 4.3 | |
PM # 3 | |
Get 4.1 | |
Get 4.3 | |
PM # 4 | |
Get 4.1 | |
Get 4.3 | |
PM # 5 | |
Get 4.1 | |
Get 4.3 | |
PM # 6 | |
Get 4.1 | |
Get 4.3 | |
PM # 7 | |
Get 4.1 | |
Get 4.3 | |
PM # 8 | |
Get 4.1 | |
Get 4.3 | |
PM # 9 | |
Get 4.1 | |
Get 4.3 | |
PM # 10 | |
Get 4.1 | |
Get 4.3 | |
PM # 11 | |
Get 4.1 | |
Get 4.3 | |
PM # 12 | |
Get 4.1 | |
Get 4.3 | |
PM # 13 | |
Get 4.1 | |
Get 4.3 | |
PM # 14 | |
Get 4.1 | |
Get 4.3 | |
PM # 15 | |
Get 4.1 | |
Get 4.3 | |
PM # 16 | |
Get 4.1 | |
Get 4.3 | |
PM # 17 | |
Get 4.1 | |
Get 4.3 | |
PM # 18 | |
Get 4.1 | |
Get 4.3 | |
PM # 19 | |
Get 4.1 | |
Get 4.3 | |
PM # 20 | |
Get 4.1 | |
Get 4.3 | |
PM # 21 | |
Get 4.1 | |
Get 4.3 | |
PM # 22 | |
Get 4.1 | |
Get 4.3 | |
PM # 23 | |
Get 4.1 | |
Get 4.3 | |
PM # 24 | |
Get 4.1 | |
Get 4.3 | |
PM # 25 | |
Get 4.1 | |
Get 4.3 | |
PM # 26 | |
Get 4.1 | |
Get 4.3 | |
PM # 27 | |
Get 4.1 | |
Get 4.3 | |
PM # 28 | |
Get 4.1 | |
Get 4.3 | |
PM # 29 | |
Get 4.1 | |
Get 4.3 | |
PM # 30 | |
Get 4.1 | |
Get 4.3 | |
PM # 31 | |
Get 4.1 | |
Get 4.3 | |
PM # 32 | |
Get 4.1 | |
Get 4.3 | |
PM # 33 | |
Get 4.1 | |
Get 4.3 | |
PM # 34 | |
Get 4.1 | |
Get 4.3 | |
PM # 35 | |
Get 4.1 | |
Get 4.3 | |
PM # 36 | |
Get 4.1 | |
Get 4.3 | |
PM # 37 | |
Get 4.1 | |
Get 4.3 | |
PM # 38 | |
Get 4.1 | |
Get 4.3 | |
PM # 39 | |
Get 4.1 | |
Get 4.3 | |
PM # 40 | |
Get 4.1 | |
Get 4.3 | |
PM # 41 | |
Get 4.1 | |
Get 4.3 | |
PM # 42 | |
Get 4.1 | |
Get 4.3 | |
PM # 43 | |
Get 4.1 | |
Get 4.3 | |
PM # 44 | |
Get 4.1 | |
Get 4.3 | |
PM # 45 | |
Get 4.1 | |
Get 4.3 | |
PM # 46 | |
Get 4.1 | |
Get 4.3 | |
PM # 47 | |
Get 4.1 | |
Get 4.3 | |
PM # 48 | |
Get 4.1 | |
Get 4.3 | |
PM # 49 | |
Get 4.1 | |
Get 4.3 | |
PM # 50 | |
Get 4.1 | |
Get 4.3 | |
PM # 51 | |
Get 4.1 | |
Get 4.3 | |
PM # 52 | |
Get 4.1 | |
Get 4.3 | |
PM # 53 | |
Get 4.1 | |
Get 4.3 | |
PM # 54 | |
Get 4.1 | |
Get 4.3 | |
PM # 55 | |
Get 4.1 | |
Get 4.3 | |
PM # 56 | |
Get 4.1 | |
Get 4.3 | |
PM # 57 | |
Get 4.1 | |
Get 4.3 | |
PM # 58 | |
Get 4.1 | |
Get 4.3 | |
PM # 59 | |
Get 4.1 | |
Get 4.3 | |
PM # 60 | |
Get 4.1 | |
Get 4.3 | |
PM # 61 | |
Get 4.1 | |
Get 4.3 | |
PM # 62 | |
Get 4.1 | |
Get 4.3 | |
PM # 63 | |
Get 4.1 | |
Get 4.3 | |
PM # 64 | |
Get 4.1 | |
Get 4.3 | |
PM # 65 | |
Get 4.1 | |
Get 4.3 | |
PM # 66 | |
Get 4.1 | |
Get 4.3 | |
PM # 67 | |
Get 4.1 | |
Get 4.3 | |
PM # 68 | |
Get 4.1 | |
Get 4.3 | |
PM # 69 | |
Get 4.1 | |
Get 4.3 | |
PM # 70 | |
Get 4.1 | |
Get 4.3 | |
PM # 71 | |
Get 4.1 | |
Get 4.3 | |
PM # 72 | |
Get 4.1 | |
Get 4.3 | |
PM # 73 | |
Get 4.1 | |
Get 4.3 | |
PM # 74 | |
Get 4.1 | |
Get 4.3 | |
PM # 75 | |
Get 4.1 | |
Get 4.3 | |
PM # 76 | |
Get 4.1 | |
Get 4.3 | |
PM # 77 | |
Get 4.1 | |
Get 4.3 | |
PM # 78 | |
Get 4.1 | |
Get 4.3 | |
PM # 79 | |
Get 4.1 | |
Get 4.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment