Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 FlairMachine:
- USER_AGENT = ''
- USER_NAME = ''
- PASSWD = ''
- SUBJECT = 'flair'
- TARGET_SUB = ''
- # 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
- x = 1
- while x < 10:
- def init(self):
- if self.LOGGING:
- os.chdir(os.path.dirname(os.path.abspath(__file__)))
- self.login()
- def login(self):
- self.r = praw.Reddit(user_agent=self.USER_AGENT)
- self.r.login(self.USER_NAME, self.PASSWD)
- self.fetch_pms()
- def fetch_pms(self):
- self.pms = self.r.get_unread(limit=None)
- if self.pms is not None:
- self.process_pms()
- def process_pms(self):
- for pm in self.pms:
- if str(pm.subject) == self.SUBJECT:
- author = str(pm.author) # Author of the PM
- content = str(pm.body) # Content of the PM
- subreddit = self.r.get_subreddit(self.TARGET_SUB)
- if content in flairs:
- # Get the flair text that corresponds with the class name
- flair_text = str(flairs[content])
- subreddit.set_flair(author, flair_text, content)
- if self.LOGGING:
- self.log(author, content, flair_text)
- pm.mark_as_read() # Mark processed PM as read
- def log(self, author, content, flair_text):
- with open('log.txt', 'a') as logfile:
- time_now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
- log_text = 'Added: ' + author + ' : ' \
- + flair_text + ' : ' \
- + content + ' @ ' + time_now + '\n'
- logfile.write(log_text)
- FlairMachine().init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement