Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw, re, os
- from conf import *
- # Check that the file that contains our username exists
- if not os.path.isfile("conf.py"):
- print("Config Required!")
- exit(1)
- # Create the Reddit instance
- user_agent = ("SupremeRedditB0t 0.1")
- r = praw.Reddit(user_agent=user_agent)
- # and login
- r.login(REDDIT_USERNAME, REDDIT_PASS, disable_warning=True)
- # Have we run this code before? If not, create an empty list
- if not os.path.isfile("posts_replied_to.txt"):
- posts_replied_to = []
- # If we have run the code before, load the list of posts we have replied to
- else:
- # Read the file into a list and remove any empty values
- with open("posts_replied_to.txt", "r") as f:
- posts_replied_to = f.read()
- posts_replied_to = posts_replied_to.split("\n")
- posts_replied_to = list(set(posts_replied_to))
- # Get all posts from subreddit
- subreddit = r.get_subreddit('CodingHelp')
- for submission in subreddit.get_new():
- # If we haven't replied to this post before
- if submission.id not in posts_replied_to:
- # Check if has a flair
- if submission.link_flair_text == None:
- # Default suggested flair
- flair = "Random"
- # Detect any languages in post/title
- if re.search("HTML", submission.title, re.IGNORECASE) or re.search("HTML", submission.selftext, re.IGNORECASE):
- flair = "HTML"
- elif re.search("CSS", submission.title, re.IGNORECASE) or re.search("CSS", submission.selftext, re.IGNORECASE):
- flair = "CSS"
- elif re.search("Java", submission.title, re.IGNORECASE) or re.search("Java", submission.selftext, re.IGNORECASE):
- flair = "Java"
- elif re.search("Javascript", submission.title, re.IGNORECASE) or re.search("Javascript", submission.selftext, re.IGNORECASE):
- flair = "Javascript"
- elif re.search("PHP", submission.title, re.IGNORECASE) or re.search("PHP", submission.selftext, re.IGNORECASE):
- flair = "PHP"
- elif re.search("Code", submission.title, re.IGNORECASE) or re.search("Code", submission.selftext, re.IGNORECASE):
- flair = "Other Code"
- # Reply to the post with suggested flair
- submission.add_comment("Please Add A Flair To Your Post!\n\nSuggested Flair: ["+flair+"]")
- print("Bot replying to:", submission.title, "with flair", flair)
- # Store the current id into our list
- posts_replied_to.append(submission.id)
- # Write our updated list back to the file
- with open("posts_replied_to.txt", "w") as f:
- for post_id in posts_replied_to:
- f.write(post_id + "\n")
Advertisement
Add Comment
Please, Sign In to add comment