Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import re
- import time
- import sqlite3
- reddit = praw.Reddit(username='RedditFlairBot',password='!tr1cky!p455w0rd',client_id='oK6sZNphQSCtiA',client_secret='VJs4iBAmLkxDW7e3BILFzNto4cM',user_agent='FlairBot')
- subreddits = ['DataIsBeautiful','MusicMemes']
- data = sqlite3.connect('dataisbeautiful.db')
- cur = data.cursor()
- cur.execute('CREATE TABLE IF NOT EXISTS stickied(id)')
- data.commit()
- class Sticky(object):
- def __init__(self, submission):
- self.submission = submission
- self.comments = submission.comments
- self.author = str(submission.author)
- self.subreddit = submission.subreddit
- self.query = 'SELECT * FROM stickied WHERE id=?'
- self.entry = 'INSERT INTO stickied VALUES(?)'
- def check_database(self, comment_id):
- cur.execute(self.query, [comment_id])
- if cur.fetchone():
- return True
- else:
- return False
- def submit_to_database(self, comment_id):
- cur.execute(self.entry, [comment_id])
- data.commit()
- def sticky(self):
- for comment in self.comments:
- if str(comment.author) == self.author and self.check_database(comment.id) is False and re.search('source',comment.body.lower()):
- print('Found a source')
- 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())
- (self.submission.reply(reply)).mod.distinguish(sticky=True)
- print('Stickied comment')
- self.submit_to_database(comment.id)
- class Flair(object):
- def __init__(self, submission, subreddit):
- self.submission = submission
- self.author = submission.author
- self.subreddit = subreddit
- self.special_flairs = ['mod','b','contrib','s','practitioner','AMAGuest','researcher']
- self.set_flair()
- def __flair__(self):
- count = 0
- print('Checking /u/{}'.format(self.author))
- try:
- for post in reddit.subreddit(self.subreddit).search(str(self.author), limit=1000):
- if post.approved_by is not None and re.search('([\[\(\{]([Oo][Cc])[\]\}\)])',str(post.title)):
- count += 1
- return(count)
- except Exception as e:
- pass
- def set_flair(self):
- count = self.__flair__()
- try:
- if count > 0 and count < 100:
- 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):
- reddit.subreddit(str(self.subreddit)).flair.set(redditor=str(self.author),text='OC: {}'.format(count), css_class = 'blank')
- print('Flairing /u/{}'.format(self.author))
- elif count > 100:
- 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):
- reddit.subreddit(str(self.subreddit)).flair.set(redditor=str(self.author),text='OC: {}'.format(count), css_class = 'ocmaker')
- print('Flairing OCMAKER')
- else:
- pass
- except Exception as e:
- print(e)
- def main():
- while True:
- for subreddit in subreddits:
- for submission in reddit.subreddit(subreddit).hot(limit=100):
- Sticky(submission).sticky()
- Flair(submission, subreddit)
- time.sleep(60*10)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement