Last active
August 29, 2015 14:26
-
-
Save 13steinj/aaeafb58e759eacc56c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, datetime | |
import praw, Oauth2Util | |
import sqlite3 | |
USERNAME = "" | |
SUBREDDIT = "" | |
try: | |
import botconfig | |
USERNAME = botconfig.USERNAME | |
SUBREDDIT = botconfig.SUBREDDIT | |
except ImportError: | |
pass | |
except NameError: | |
pass | |
USERNAME = USERNAME.replace("/u/", "") | |
USERNAME = USERNAME.replace("u/", "") | |
SUBREDDIT = SUBREDDIT.replace("/r/", "") | |
SUBREDDIT = SUBREDDIT.replace("r/", "") | |
USERAGENT = "This is a self use script to leave a comment on posts with no flair in /r/{0} by /u/13steinj in use by /u/{1}".format(SUBREDDIT, USERNAME) | |
MESSAGE = """ | |
Please apply a flair to your post. | |
*I am a bot, this action was performed automatically.* | |
--- | |
[Creator](https://www.reddit.com/u/13steinj) | In use by [/u/{0}](https://www.reddit.com/u/{0}) | |
""".format(USERNAME) | |
if USERNAME == "13steinj": | |
MESSAGE = MESSAGE.replace(" | In use by [/u/{0}](https://www.reddit.com/u/{0})".format(USERNAME), "") | |
BLANKFLAIR = "" | |
r = praw.Reddit(USERAGENT) | |
o = Oauth2Util.Oauth2Util(r) | |
sub = r.get_subreddit(SUBREDDIT) | |
DATABASE = sqlite3.connect('commentedon.db') | |
cur = DATABASE.cursor() | |
cur.execute('CREATE TABLE IF NOT EXISTS commentedposts(id TEXT)') | |
DATABASE.commit() | |
def main(): | |
o.refresh() | |
try: | |
posts = sub.get_new(limit=100) | |
for post in posts: | |
now = datetime.datetime.now(datetime.timezone.utc).timestamp() | |
if now - post.created_utc < 240: | |
continue | |
if (post.link_flair_css == BLANKFLAIR or post.link_flair_text == BLANKFLAIR): | |
cur.execute('SELECT * FROM commentedposts WHERE ID=?', [post.id]) | |
if cur.fetchone() == None: | |
post.add_comment(MESSAGE) | |
cur.execute('INSERT INTO commentedposts VALUES(?)', [post.id]) | |
DATABASE.commit() | |
else: | |
pass | |
except KeyboardInterrupt: | |
pass | |
except (praw.errors.HTTPException(502), praw.errors.HTTPException(503), praw.errors.HTTPException(504), praw.errors.HTTPException(520)): | |
raise | |
except Exception as e: | |
if SUBREDDIT != "13steinj": | |
r.send_message(recipient="/r/13steinj", subject="FlairPostNotifier for {0} has broken.".format(SUBREDDIT), message=e) | |
r.send_message(recipient="/r/{0}".format(SUBREDDIT), subject="FlairPostNotifier has broken.", message="Please Investigate. \n{0}".format(e)) | |
raise | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment