Advertisement
Guest User

Untitled

a guest
May 27th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import praw
  4. import sys
  5. import os
  6. from time import gmtime, strftime
  7. try:
  8.     from flair_list import flairs
  9. except ImportError as e:
  10.     print("Flairs file can't be accessed\n")
  11.     print(e)
  12.     sys.exit()
  13. except SyntaxError as e:
  14.     print("There is a syntax error in the flair list\n")
  15.     print(e)
  16.     sys.exit()
  17.  
  18. class FlairMachine:
  19.     USER_AGENT = ''
  20.  
  21.     USER_NAME = ''
  22.     PASSWD = ''
  23.  
  24.     SUBJECT = 'flair'
  25.  
  26.     TARGET_SUB = ''
  27.  
  28.     # Turn on output to log file in current directory - log.txt
  29.     LOGGING = True
  30.  
  31.     # Class variable to hold the PRAW instance
  32.     r = None
  33.  
  34.     # Class variable to hold the unread pms
  35.     pms = None
  36.     x = 1
  37.     while x < 10:
  38.         def init(self):
  39.             if self.LOGGING:
  40.                 os.chdir(os.path.dirname(os.path.abspath(__file__)))
  41.             self.login()
  42.  
  43.         def login(self):
  44.             self.r = praw.Reddit(user_agent=self.USER_AGENT)
  45.             self.r.login(self.USER_NAME, self.PASSWD)
  46.             self.fetch_pms()
  47.  
  48.         def fetch_pms(self):
  49.             self.pms = self.r.get_unread(limit=None)
  50.             if self.pms is not None:
  51.                 self.process_pms()
  52.  
  53.         def process_pms(self):
  54.             for pm in self.pms:
  55.                 if str(pm.subject) == self.SUBJECT:
  56.                     author = str(pm.author)  # Author of the PM
  57.                     content = str(pm.body)  # Content of the PM
  58.                     subreddit = self.r.get_subreddit(self.TARGET_SUB)
  59.                     if content in flairs:
  60.                         # Get the flair text that corresponds with the class name
  61.                         flair_text = str(flairs[content])
  62.                         subreddit.set_flair(author, flair_text, content)
  63.                         if self.LOGGING:
  64.                             self.log(author, content, flair_text)
  65.                     pm.mark_as_read()  # Mark processed PM as read
  66.  
  67.         def log(self, author, content, flair_text):
  68.             with open('log.txt', 'a') as logfile:
  69.                 time_now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
  70.                 log_text = 'Added: ' + author + ' : ' \
  71.                             + flair_text + ' : ' \
  72.                             + content + ' @ ' + time_now + '\n'
  73.                 logfile.write(log_text)
  74.            
  75. FlairMachine().init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement