From 8f4c9d81a5c78f5f7fb86c1934ce31abd8290ad7 Mon Sep 17 00:00:00 2001 From: jwansek Date: Sun, 26 Jul 2020 22:00:04 +0100 Subject: started on MySQL development branch --- subreddit.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'subreddit.py') diff --git a/subreddit.py b/subreddit.py index 205995b..4dc0d2c 100755 --- a/subreddit.py +++ b/subreddit.py @@ -21,8 +21,6 @@ FREE_FLAIRS = CONFIG["free_flairs"] IMGUR = ImgurClient(**CONFIG["imgurapi"]) -db = Database() - logging.basicConfig( format = "[%(asctime)s] %(message)s", level = logging.INFO, @@ -381,18 +379,19 @@ def add_times_to_lambdas(): def format_monthly_leaderboard(): - leaderboard = db.get_lambda_leaderboard() - out = "**Username**|**Medal**|**Times Helped**|**Lambda**\n:-|:-|:-|:-\n" - for username, times_helped, λ in leaderboard: - out += "/u/%s|%1s|%s|%sλ\n" % (username, get_medal(λ)[:-1], times_helped, λ) - return out + "\nLast updated: %s" % get_time() + with Database() as db: + leaderboard = db.get_lambda_leaderboard() + out = "**Username**|**Medal**|**Times Helped**|**Lambda**\n:-|:-|:-|:-\n" + for username, times_helped, λ in leaderboard: + out += "/u/%s|%1s|%s|%sλ\n" % (username, get_medal(λ)[:-1], times_helped, λ) + return out + "\nLast updated: %s" % get_time() if __name__ == "__main__": - file = open("pid.txt", "w") - file.write(str(os.getpid())) - file.close() + file_ = open("pid.txt", "w") + file_.write(str(os.getpid())) + file_.close() display("\n####################\n[%s] RESTARTED\n####################\n" % get_time()) main() -- cgit v1.2.3 From 386b9d9a005178077c9f5c0206e1c6722b580c55 Mon Sep 17 00:00:00 2001 From: jwansek Date: Mon, 27 Jul 2020 12:12:12 +0100 Subject: added a bunch of with statements --- subreddit.py | 143 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 77 insertions(+), 66 deletions(-) (limited to 'subreddit.py') diff --git a/subreddit.py b/subreddit.py index 4dc0d2c..6a398f2 100755 --- a/subreddit.py +++ b/subreddit.py @@ -1,6 +1,6 @@ from imgurpython import ImgurClient from operator import itemgetter -from database import Database +import database import datetime import logging import ytapi @@ -133,9 +133,10 @@ def upload_image(path, date): def every_day(): display("Starting every day program...") display("Updating database statistics...") - db.update_stats() - display("Posting and updating wiki...") - update_tables(db.get_scores(), db.get_stats()) + with database.Database() as db: + db.update_stats() + display("Posting and updating wiki...") + update_tables(db.get_scores(), db.get_stats()) display("Formatting leaderboard...") leaderboard = format_monthly_leaderboard() display("Updating sidebar...") @@ -152,7 +153,8 @@ def every_day(): def handle_mylambda(comment): author = str(comment.author) - λ, links = db.get_lambda(author) + with database.Database() as db: + λ, links = db.get_lambda(author) if author in get_mods(): text = "/u/%s is a moderator, and therefore has ∞λ." % author else: @@ -177,30 +179,31 @@ def handle_givelambda(comment): submission = comment.submission parentauthour = str(comment.parent().author) op = str(comment.author) - if op == parentauthour: - text = "You cannot give yourself λ." - elif parentauthour == "SmallYTChannelBot": - text = "Please only give lambda to humans." - elif str(comment.author) in get_mods(): - text = "The moderator /u/%s has given /u/%s 1λ. /u/%s now has %iλ." % (str(comment.author), parentauthour, parentauthour, db.get_lambda(parentauthour)[0] + 1) - db.give_lambda(parentauthour, submission.permalink, timestamp = int(submission.created_utc)) - display(text) - elif submission.link_flair_text in FREE_FLAIRS: - text = "You cannot give lambda in free posts anymore." - elif op != str(submission.author): - text = "Only the OP can give λ." - elif db.user_given_lambda(parentauthour, str(submission.permalink)): - text = "You have already given /u/%s λ for this submission. Why not give λ to another user instead?" % parentauthour - else: - display("'/u/%s' has given '/u/%s' lambda!" % (op, parentauthour)) - text = "You have given /u/%s 1λ. /u/%s now has %iλ" % (parentauthour, parentauthour, db.get_lambda(parentauthour)[0] + 1) + with database.Database() as db: + if op == parentauthour: + text = "You cannot give yourself λ." + elif parentauthour == "SmallYTChannelBot": + text = "Please only give lambda to humans." + elif str(comment.author) in get_mods(): + text = "The moderator /u/%s has given /u/%s 1λ. /u/%s now has %iλ." % (str(comment.author), parentauthour, parentauthour, db.get_lambda(parentauthour)[0] + 1) + db.give_lambda(parentauthour, submission.permalink, timestamp = int(submission.created_utc)) + display(text) + elif submission.link_flair_text in FREE_FLAIRS: + text = "You cannot give lambda in free posts anymore." + elif op != str(submission.author): + text = "Only the OP can give λ." + elif db.user_given_lambda(parentauthour, str(submission.permalink)): + text = "You have already given /u/%s λ for this submission. Why not give λ to another user instead?" % parentauthour + else: + display("'/u/%s' has given '/u/%s' lambda!" % (op, parentauthour)) + text = "You have given /u/%s 1λ. /u/%s now has %iλ" % (parentauthour, parentauthour, db.get_lambda(parentauthour)[0] + 1) - #bonus lambda giving was removed - # if not db.link_in_db(submission.permalink) or not db.link_in_db(submission.permalink.replace("https://www.reddit.com", "")): - # db.give_lambda(parentauthour, submission.permalink, op) - # display("The OP received lambda too!") - # else: - db.give_lambda(parentauthour, submission.permalink, timestamp = int(submission.created_utc)) + #bonus lambda giving was removed + # if not db.link_in_db(submission.permalink) or not db.link_in_db(submission.permalink.replace("https://www.reddit.com", "")): + # db.give_lambda(parentauthour, submission.permalink, op) + # display("The OP received lambda too!") + # else: + db.give_lambda(parentauthour, submission.permalink, timestamp = int(submission.created_utc)) # update_users_flair_from_comment(comment) update_users_flair_from_comment(comment.parent()) @@ -214,7 +217,8 @@ def handle_takelambda(comment): reason = " ".join(splitted[3:]) text = "/u/%s has had %iλ taken away from them for the reason '%s'. /u/%s now has %iλ" % (user, toremove, reason, user, db.get_lambda(user)[0] - toremove) - db.change_lambda(user, -toremove) + with database.Database() as db: + db.change_lambda(user, -toremove) display("A moderator removed %i lambda from /u/%s for the reason '%s'" % (toremove, user, reason)) except Exception as e: display("{ERROR while removing λ} %s" % e) @@ -231,7 +235,8 @@ def handle_refundlambda(comment): reason = " ".join(splitted[3:]) text = "/u/%s has had %iλ refunded for the reason '%s'. /u/%s now has %iλ" % (user, toadd, reason, user, db.get_lambda(user)[0] + toadd) - db.change_lambda(user, toadd) + with database.Database() as db: + db.change_lambda(user, toadd) display("A moderator refunded %i lambda from /u/%s for the reason '%s'" % (toadd, user, reason)) except Exception as e: display("{ERROR while refunding λ} %s" % e) @@ -241,7 +246,8 @@ def handle_refundlambda(comment): return text def handle_submission(submission): - score = db.get_lambda(str(submission.author))[0] + with database.Database() as db: + score = db.get_lambda(str(submission.author))[0] if submission.link_flair_text in FREE_FLAIRS: if "youtube.com" in str(submission.url) or "youtu.be" in str(submission.url): text = "Your post has been removed because it has the wrong flair. [Discussion], [Meta] and [Collab] flairs are only for text submissions." @@ -259,7 +265,8 @@ def handle_submission(submission): text = """Thank you for submitting to /r/SmallYTChannel. You have spent 3λ to submit here, making your current balance %iλ. /u/%s, please comment `!givelambda` to the most helpful advice you are given. For more information, read the [FAQ.](https://www.reddit.com/user/SmallYTChannelBot/comments/a4u7qj/smallytchannelbot_faq/)""" % (score - 3, str(submission.author)) - db.change_lambda(str(submission.author), -3) + with database.Database() as db: + db.change_lambda(str(submission.author), -3) try: ytid = ytapi.get_videoId_from_url(submission.url) @@ -324,39 +331,43 @@ def main(): for comment in comment_stream: if comment is None: break - if not db.id_in_blacklist(comment.id): - db.add_to_blacklist(comment.id) - - response = None - if "!mylambda" in comment.body.lower() and str(comment.author) != "SmallYTChannelBot": - response = handle_mylambda(comment) - - if "!givelambda" in comment.body.lower() and str(comment.author) != "SmallYTChannelBot": - response = handle_givelambda(comment) - if comment.body.startswith("!takelambda") and str(comment.author) in get_mods(): - response = handle_takelambda(comment) + with database.Database() as db: + if not db.id_in_blacklist(comment.id): + db.add_to_blacklist(comment.id) + + response = None + if "!mylambda" in comment.body.lower() and str(comment.author) != "SmallYTChannelBot": + response = handle_mylambda(comment) + + if "!givelambda" in comment.body.lower() and str(comment.author) != "SmallYTChannelBot": + response = handle_givelambda(comment) - if comment.body.startswith("!refundlambda") and str(comment.author) in get_mods(): - response = handle_refundlambda(comment) + if comment.body.startswith("!takelambda") and str(comment.author) in get_mods(): + response = handle_takelambda(comment) - if response is not None: - reply = comment.reply(response + COMMENT_TAIL) - reply.mod.distinguish(sticky = False) + if comment.body.startswith("!refundlambda") and str(comment.author) in get_mods(): + response = handle_refundlambda(comment) + if response is not None: + reply = comment.reply(response + COMMENT_TAIL) + reply.mod.distinguish(sticky = False) + for submission in submission_stream: if submission is None: break - if not db.id_in_blacklist(submission.id): - db.add_to_blacklist(submission.id) - display("There has been a new submission: '%s', with flair '%s'" % (submission.title, submission.link_flair_text)) - response = None - if str(submission.author) not in get_mods(): - response = handle_submission(submission) - reply = submission.reply(response + COMMENT_TAIL) - reply.mod.distinguish(sticky = True) - reply.mod.approve() + with database.Database() as db: + if not db.id_in_blacklist(submission.id): + db.add_to_blacklist(submission.id) + display("There has been a new submission: '%s', with flair '%s'" % (submission.title, submission.link_flair_text)) + + response = None + if str(submission.author) not in get_mods(): + response = handle_submission(submission) + reply = submission.reply(response + COMMENT_TAIL) + reply.mod.distinguish(sticky = True) + reply.mod.approve() except Exception as e: display("{ERROR} %s" % e) @@ -371,15 +382,16 @@ def get_submission_times(permalink): def add_times_to_lambdas(): updated_permalinks = [] - for id_, permalink, user, created in db.get_all_lambdas(): - if created is None and permalink not in updated_permalinks: - db.add_date_to_permalink(permalink, get_submission_times(permalink)) - updated_permalinks.append(permalink) - logging.info("Added date for permalink %s" % permalink) + with database.Database() as db: + for id_, permalink, user, created in db.get_all_lambdas(): + if created is None and permalink not in updated_permalinks: + db.add_date_to_permalink(permalink, get_submission_times(permalink)) + updated_permalinks.append(permalink) + logging.info("Added date for permalink %s" % permalink) def format_monthly_leaderboard(): - with Database() as db: + with database.Database() as db: leaderboard = db.get_lambda_leaderboard() out = "**Username**|**Medal**|**Times Helped**|**Lambda**\n:-|:-|:-|:-\n" for username, times_helped, λ in leaderboard: @@ -389,9 +401,8 @@ def format_monthly_leaderboard(): if __name__ == "__main__": - file_ = open("pid.txt", "w") - file_.write(str(os.getpid())) - file_.close() + with open("pid.txt", "w") as f: + f.write(str(os.getpid())) display("\n####################\n[%s] RESTARTED\n####################\n" % get_time()) main() -- cgit v1.2.3 From c2496d212640bcdc439e39725f2e26c658a2bc63 Mon Sep 17 00:00:00 2001 From: jwansek Date: Mon, 27 Jul 2020 15:12:58 +0100 Subject: fixed a small bug --- subreddit.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'subreddit.py') diff --git a/subreddit.py b/subreddit.py index 6a398f2..5632b82 100755 --- a/subreddit.py +++ b/subreddit.py @@ -69,7 +69,8 @@ def update_users_flair(username): if username in get_mods(): newflair = "[🏆 ∞λ] %s" % (flairtext) else: - actualscore = db.get_lambda(username)[0] + with database.Database() as db: + actualscore = db.get_lambda(username)[0] newflair = "[%s%iλ] %s" % (get_medal(actualscore), actualscore, flairtext) logging.info("/u/%s had their flair updated" % username) @@ -187,7 +188,7 @@ def handle_givelambda(comment): elif str(comment.author) in get_mods(): text = "The moderator /u/%s has given /u/%s 1λ. /u/%s now has %iλ." % (str(comment.author), parentauthour, parentauthour, db.get_lambda(parentauthour)[0] + 1) db.give_lambda(parentauthour, submission.permalink, timestamp = int(submission.created_utc)) - display(text) + display(text) elif submission.link_flair_text in FREE_FLAIRS: text = "You cannot give lambda in free posts anymore." elif op != str(submission.author): @@ -216,8 +217,8 @@ def handle_takelambda(comment): toremove = int(splitted[2].replace("\\", "")) reason = " ".join(splitted[3:]) - text = "/u/%s has had %iλ taken away from them for the reason '%s'. /u/%s now has %iλ" % (user, toremove, reason, user, db.get_lambda(user)[0] - toremove) with database.Database() as db: + text = "/u/%s has had %iλ taken away from them for the reason '%s'. /u/%s now has %iλ" % (user, toremove, reason, user, db.get_lambda(user)[0] - toremove) db.change_lambda(user, -toremove) display("A moderator removed %i lambda from /u/%s for the reason '%s'" % (toremove, user, reason)) except Exception as e: @@ -234,8 +235,8 @@ def handle_refundlambda(comment): toadd = int(splitted[2].replace("\\", "")) reason = " ".join(splitted[3:]) - text = "/u/%s has had %iλ refunded for the reason '%s'. /u/%s now has %iλ" % (user, toadd, reason, user, db.get_lambda(user)[0] + toadd) with database.Database() as db: + text = "/u/%s has had %iλ refunded for the reason '%s'. /u/%s now has %iλ" % (user, toadd, reason, user, db.get_lambda(user)[0] + toadd) db.change_lambda(user, toadd) display("A moderator refunded %i lambda from /u/%s for the reason '%s'" % (toadd, user, reason)) except Exception as e: -- cgit v1.2.3 From e29f94fcdeddfc563b86545fb22a3b8fca77beb3 Mon Sep 17 00:00:00 2001 From: jwansek Date: Mon, 27 Jul 2020 19:51:36 +0100 Subject: started on switching logging to database --- subreddit.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'subreddit.py') diff --git a/subreddit.py b/subreddit.py index 5632b82..9ce5cbe 100755 --- a/subreddit.py +++ b/subreddit.py @@ -25,7 +25,7 @@ logging.basicConfig( format = "[%(asctime)s] %(message)s", level = logging.INFO, handlers=[ - logging.FileHandler("actions.log"), + # logging.FileHandler("actions.log"), logging.StreamHandler() ]) @@ -33,8 +33,10 @@ def get_time(): #this is not the correct way to do this but I don't care return time.strftime("%b %d %Y %H:%M:%S", time.gmtime()) -def display(message): +def display(message, concerning = None): logging.info(message) + with database.Database() as db: + db.append_log(message, concerning) def get_lambda_from_flair(s): result = re.search("\[(.*)\]", s) @@ -188,7 +190,7 @@ def handle_givelambda(comment): elif str(comment.author) in get_mods(): text = "The moderator /u/%s has given /u/%s 1λ. /u/%s now has %iλ." % (str(comment.author), parentauthour, parentauthour, db.get_lambda(parentauthour)[0] + 1) db.give_lambda(parentauthour, submission.permalink, timestamp = int(submission.created_utc)) - display(text) + display(text, concerning=comment.permalink) elif submission.link_flair_text in FREE_FLAIRS: text = "You cannot give lambda in free posts anymore." elif op != str(submission.author): @@ -196,7 +198,7 @@ def handle_givelambda(comment): elif db.user_given_lambda(parentauthour, str(submission.permalink)): text = "You have already given /u/%s λ for this submission. Why not give λ to another user instead?" % parentauthour else: - display("'/u/%s' has given '/u/%s' lambda!" % (op, parentauthour)) + display("'/u/%s' has given '/u/%s' lambda!" % (op, parentauthour), concerning=comment.permalink) text = "You have given /u/%s 1λ. /u/%s now has %iλ" % (parentauthour, parentauthour, db.get_lambda(parentauthour)[0] + 1) #bonus lambda giving was removed @@ -220,9 +222,9 @@ def handle_takelambda(comment): with database.Database() as db: text = "/u/%s has had %iλ taken away from them for the reason '%s'. /u/%s now has %iλ" % (user, toremove, reason, user, db.get_lambda(user)[0] - toremove) db.change_lambda(user, -toremove) - display("A moderator removed %i lambda from /u/%s for the reason '%s'" % (toremove, user, reason)) + display("A moderator removed %i lambda from /u/%s for the reason '%s'" % (toremove, user, reason), concerning=comment.permalink) except Exception as e: - display("{ERROR while removing λ} %s" % e) + display("{ERROR while removing λ} %s" % e, concerning=comment.permalink) text = r"An error was encountered. Please use the syntax `!takelambda [user] [how much to remove {integer}] [reason]`" + "\n\nThe error was:\n\n" + str(e) update_users_flair(user) @@ -238,9 +240,9 @@ def handle_refundlambda(comment): with database.Database() as db: text = "/u/%s has had %iλ refunded for the reason '%s'. /u/%s now has %iλ" % (user, toadd, reason, user, db.get_lambda(user)[0] + toadd) db.change_lambda(user, toadd) - display("A moderator refunded %i lambda from /u/%s for the reason '%s'" % (toadd, user, reason)) + display("A moderator refunded %i lambda from /u/%s for the reason '%s'" % (toadd, user, reason), concerning=comment.permalink) except Exception as e: - display("{ERROR while refunding λ} %s" % e) + display("{ERROR while refunding λ} %s" % e, concerning=comment.permalink) text = r"An error was encountered. Please use the syntax `!refundlambda [user] [how much to add {integer}] [reason]`" + "\n\nThe error was:\n\n" + str(e) update_users_flair(user) @@ -253,7 +255,7 @@ def handle_submission(submission): if "youtube.com" in str(submission.url) or "youtu.be" in str(submission.url): text = "Your post has been removed because it has the wrong flair. [Discussion], [Meta] and [Collab] flairs are only for text submissions." submission.mod.remove() - display("/u/%s had their submission removed for using the wrong flair." % submission.author) + display("/u/%s had their submission removed for using the wrong flair." % submission.author, concerning=submission.permalink) else: text = "Your post is a discussion, meta or collab post so it costs 0λ." else: @@ -261,7 +263,7 @@ def handle_submission(submission): text = """Thank you for submitting to /r/SmallYTChannel. Unfortunally, you submission has been removed since you do not have enough λ. You need 3λ to post. You currently have %iλ. For more information, read the [FAQ.](https://www.reddit.com/user/SmallYTChannelBot/comments/a4u7qj/smallytchannelbot_faq/)""" % score submission.mod.remove() - display("/u/%s had their submission removed for insufficient lambda." % submission.author) + display("/u/%s had their submission removed for insufficient lambda." % submission.author, concerning=submission.permalink) else: text = """Thank you for submitting to /r/SmallYTChannel. You have spent 3λ to submit here, making your current balance %iλ. /u/%s, please comment `!givelambda` to the most helpful advice you are given. @@ -361,7 +363,7 @@ def main(): with database.Database() as db: if not db.id_in_blacklist(submission.id): db.add_to_blacklist(submission.id) - display("There has been a new submission: '%s', with flair '%s'" % (submission.title, submission.link_flair_text)) + display("There has been a new submission: '%s', with flair '%s'" % (submission.title, submission.link_flair_text), concerning=comment.permalink) response = None if str(submission.author) not in get_mods(): @@ -405,6 +407,6 @@ if __name__ == "__main__": with open("pid.txt", "w") as f: f.write(str(os.getpid())) - display("\n####################\n[%s] RESTARTED\n####################\n" % get_time()) + display("####################[%s] RESTARTED####################" % get_time()) main() -- cgit v1.2.3 From 2caf723beae4e1a515e754dcbe5a0ea1cb448d98 Mon Sep 17 00:00:00 2001 From: jwansek Date: Fri, 31 Jul 2020 16:12:29 +0100 Subject: made logging work --- subreddit.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'subreddit.py') diff --git a/subreddit.py b/subreddit.py index 9ce5cbe..ff81bc4 100755 --- a/subreddit.py +++ b/subreddit.py @@ -22,21 +22,23 @@ FREE_FLAIRS = CONFIG["free_flairs"] IMGUR = ImgurClient(**CONFIG["imgurapi"]) logging.basicConfig( - format = "[%(asctime)s] %(message)s", + format = "%(process)s\t[%(asctime)s]\t%(message)s", level = logging.INFO, handlers=[ - # logging.FileHandler("actions.log"), + logging.FileHandler("actions.log"), logging.StreamHandler() ]) def get_time(): - #this is not the correct way to do this but I don't care return time.strftime("%b %d %Y %H:%M:%S", time.gmtime()) def display(message, concerning = None): logging.info(message) + + #yes it'd be prettier to do this with a logging.Handler, but alas + #due to `concerning` it'd be more complicated than doing it like this with database.Database() as db: - db.append_log(message, concerning) + db.append_log("%d\t[%s]\t%s" % (os.getpid(), get_time(), message), concerning) def get_lambda_from_flair(s): result = re.search("\[(.*)\]", s) -- cgit v1.2.3 From 4286c4a895548bdaaa190b248b43b1388e630a62 Mon Sep 17 00:00:00 2001 From: jwansek Date: Fri, 31 Jul 2020 20:02:13 +0100 Subject: fixed a bug with logging --- subreddit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'subreddit.py') diff --git a/subreddit.py b/subreddit.py index ff81bc4..eece316 100755 --- a/subreddit.py +++ b/subreddit.py @@ -365,7 +365,7 @@ def main(): with database.Database() as db: if not db.id_in_blacklist(submission.id): db.add_to_blacklist(submission.id) - display("There has been a new submission: '%s', with flair '%s'" % (submission.title, submission.link_flair_text), concerning=comment.permalink) + display("There has been a new submission: '%s', with flair '%s'" % (submission.title, submission.link_flair_text), concerning=submission.permalink) response = None if str(submission.author) not in get_mods(): @@ -374,7 +374,7 @@ def main(): reply.mod.distinguish(sticky = True) reply.mod.approve() - except Exception as e: + except KeyboardInterrupt as e: display("{ERROR} %s" % e) continue -- cgit v1.2.3