Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. import praw
  2. import re
  3. import time
  4. import sqlite3
  5.  
  6.  
  7. reddit = praw.Reddit(username='RedditFlairBot',password='!tr1cky!p455w0rd',client_id='oK6sZNphQSCtiA',client_secret='VJs4iBAmLkxDW7e3BILFzNto4cM',user_agent='FlairBot')
  8.  
  9. subreddits = ['DataIsBeautiful','MusicMemes']
  10.  
  11. data = sqlite3.connect('dataisbeautiful.db')
  12. cur = data.cursor()
  13. cur.execute('CREATE TABLE IF NOT EXISTS stickied(id)')
  14. data.commit()
  15.  
  16.  
  17. class Sticky(object):
  18.  
  19.     def __init__(self, submission):
  20.         self.submission = submission
  21.         self.comments = submission.comments
  22.         self.author = str(submission.author)
  23.         self.subreddit = submission.subreddit
  24.         self.query = 'SELECT * FROM stickied WHERE id=?'
  25.         self.entry = 'INSERT INTO stickied VALUES(?)'
  26.  
  27.     def check_database(self, comment_id):
  28.         cur.execute(self.query, [comment_id])
  29.         if cur.fetchone():
  30.             return True
  31.         else:
  32.             return False
  33.  
  34.     def submit_to_database(self, comment_id):
  35.         cur.execute(self.entry, [comment_id])
  36.         data.commit()
  37.  
  38.     def sticky(self):
  39.         for comment in self.comments:
  40.             if str(comment.author) == self.author and self.check_database(comment.id) is False and re.search('source',comment.body.lower()):
  41.                 print('Found a source')
  42.                 reply = 'To encourage participation in threads marked `[OC]`, the poster has provided you with information regarding **where or how they got the data** (source) and **the tool used to generate the visual** (tools) for this `[OC]` post. To ensure this information isn\'t buried, we have stickied this link below for your convenience:\n\n[Author\'s source]({})\n\nWe hope the provided link assists you in having an informed discussion in this thread, or inspires you to remix this data. For more information, please read the sidebar.'.format(comment.permalink())
  43.                 (self.submission.reply(reply)).mod.distinguish(sticky=True)
  44.                 print('Stickied comment')
  45.                 self.submit_to_database(comment.id)
  46.  
  47. class Flair(object):
  48.  
  49.     def __init__(self, submission, subreddit):
  50.         self.submission = submission
  51.         self.author = submission.author
  52.         self.subreddit = subreddit
  53.         self.special_flairs = ['mod','b','contrib','s','practitioner','AMAGuest','researcher']
  54.         self.set_flair()
  55.  
  56.     def __flair__(self):
  57.         count = 0
  58.         print('Checking /u/{}'.format(self.author))
  59.         try:
  60.             for post in reddit.subreddit(self.subreddit).search(str(self.author), limit=1000):
  61.                 if post.approved_by is not None and re.search('([\[\(\{]([Oo][Cc])[\]\}\)])',str(post.title)):
  62.                     count += 1
  63.             return(count)
  64.         except Exception as e:
  65.             pass
  66.  
  67.     def set_flair(self):
  68.         count = self.__flair__()
  69.         try:
  70.             if count > 0 and count  < 100:
  71.                 if str(self.submission.author_flair_css_class) not in self.special_flairs and re.sub('\s+','',str(self.submission.author_flair_text)) != 'OC:{}'.format(count):
  72.                     reddit.subreddit(str(self.subreddit)).flair.set(redditor=str(self.author),text='OC: {}'.format(count), css_class = 'blank')
  73.                     print('Flairing /u/{}'.format(self.author))
  74.             elif count > 100:
  75.                 if str(self.submission.author_flair_css_class) not in self.special_flairs and re.sub('\s+','',str(self.submission.author_flair_text)) != 'OC:{}'.format(count):
  76.                     reddit.subreddit(str(self.subreddit)).flair.set(redditor=str(self.author),text='OC: {}'.format(count), css_class = 'ocmaker')
  77.                     print('Flairing OCMAKER')
  78.             else:
  79.                 pass
  80.         except Exception as e:
  81.             print(e)
  82.  
  83.  
  84. def main():
  85.     while True:
  86.         for subreddit in subreddits:
  87.             for submission in reddit.subreddit(subreddit).hot(limit=100):
  88.                 Sticky(submission).sticky()
  89.                 Flair(submission, subreddit)
  90.         time.sleep(60*10)
  91.  
  92. if __name__ == '__main__':
  93.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement