diff options
author | Ubuntu <ubuntu@ip-172-31-23-235.eu-west-2.compute.internal> | 2018-12-16 16:11:43 +0000 |
---|---|---|
committer | Ubuntu <ubuntu@ip-172-31-23-235.eu-west-2.compute.internal> | 2018-12-16 16:11:43 +0000 |
commit | 824ebfd153bf8e63a812ec2b9145bf49c8285435 (patch) | |
tree | 050c813b6c51c4e983efda120df1fe0d43aade7f | |
download | SmallYTChannelBot-824ebfd153bf8e63a812ec2b9145bf49c8285435.tar.gz SmallYTChannelBot-824ebfd153bf8e63a812ec2b9145bf49c8285435.zip |
added files
-rw-r--r-- | .vs/ProjectSettings.json | 3 | ||||
-rw-r--r-- | .vs/SmallYTChannelBot/v15/.suo | bin | 0 -> 17920 bytes | |||
-rw-r--r-- | .vs/VSWorkspaceState.json | 7 | ||||
-rw-r--r-- | .vs/slnx.sqlite | bin | 0 -> 73728 bytes | |||
-rw-r--r-- | SmallYTChannelBotSubmissions.py | 166 | ||||
-rw-r--r-- | SmallYTChannelDatabase.db | bin | 0 -> 43008 bytes | |||
-rw-r--r-- | __pycache__/SmallYTChannelBot.cpython-35.pyc | bin | 0 -> 2359 bytes | |||
-rw-r--r-- | __pycache__/SmallYTChannelBotSubmissions.cpython-36.pyc | bin | 0 -> 5875 bytes | |||
-rw-r--r-- | __pycache__/database.cpython-35.pyc | bin | 0 -> 3437 bytes | |||
-rw-r--r-- | __pycache__/database.cpython-36.pyc | bin | 0 -> 3933 bytes | |||
-rw-r--r-- | __pycache__/login.cpython-36.pyc | bin | 0 -> 330 bytes | |||
-rw-r--r-- | archive_posts.py | 28 | ||||
-rw-r--r-- | database.py | 86 | ||||
-rw-r--r-- | login.py | 7 |
14 files changed, 297 insertions, 0 deletions
diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 0000000..866f1e1 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{
+ "CurrentProjectSetting": null
+}
\ No newline at end of file diff --git a/.vs/SmallYTChannelBot/v15/.suo b/.vs/SmallYTChannelBot/v15/.suo Binary files differnew file mode 100644 index 0000000..08ba5aa --- /dev/null +++ b/.vs/SmallYTChannelBot/v15/.suo diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 0000000..5fef1c5 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,7 @@ +{
+ "ExpandedNodes": [
+ ""
+ ],
+ "SelectedNode": "\\SmallYTChannelBotSubmissions.py",
+ "PreviewInSolutionExplorer": false
+}
\ No newline at end of file diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite Binary files differnew file mode 100644 index 0000000..3cb47c4 --- /dev/null +++ b/.vs/slnx.sqlite diff --git a/SmallYTChannelBotSubmissions.py b/SmallYTChannelBotSubmissions.py new file mode 100644 index 0000000..cb1dc21 --- /dev/null +++ b/SmallYTChannelBotSubmissions.py @@ -0,0 +1,166 @@ +from operator import itemgetter +from database import Database +import datetime +import login +import time +import praw +import re + +reddit = login.REDDIT + +subreddit = reddit.subreddit("SmallYTChannel") +#subreddit = reddit.subreddit("jwnskanzkwktest") +db = Database() + +def get_lambda_from_flair(s): + result = re.search("\[(.*)\]", s) + if result is not None and "λ" in result.group(1): + return result.group(1) + else: + return "" + +def update_users_flair(comment): + username = str(comment.author) + flairscore = get_lambda_from_flair(str(comment.author_flair_text)) + flairtext = comment.author_flair_text + if flairtext is None: + flairtext = "" + else: + flairtext = str(flairtext.replace("[%s] " % flairscore, "")) + if username in [str(i) for i in subreddit.moderator()] + ["AutoModerator"]: + newflair = "[∞λ] %s" % (flairtext) + else: + actualscore = db.get_lambda(username)[0] + newflair = "[%iλ] %s" % (actualscore, flairtext) + subreddit.flair.set(redditor = username, text = newflair) + +def get_mods(): + return [str(i) for i in subreddit.moderator()] + ["AutoModerator"] + +def update_tables(): + content = "" + date = str(datetime.date.today()) + mods = get_mods() + data = db.get_scores() + bylambda = [i for i in sorted(data, key = itemgetter(1), reverse = True) if i[0] not in mods][:10] + byhelps = sorted(data, key = itemgetter(2), reverse = True)[:10] + + content += "\n\n#/r/SmallYTChannel lambda tables: %s" % date + + content += "\n\n##By lambda:" + content += "\n\nUsername|Lambda|Help given\n:--|:--|:--" + for line in bylambda: + content += "\n/u/%s|%i|%i" % (line[0], line[1], line[2]) + + content += "\n\n##By Help given:" + content += "\n\nUsername|Lambda|Help given\n:--|:--|:--" + for line in byhelps: + λ = str(line[1]) + if line[0] in mods: + λ = "∞" + content += "\n/u/%s|%s|%i" % (line[0], λ, line[2]) + + subreddit.wiki["lambdatables"].edit(content, reason = "Update: %s" % date) + +def main(): + tail = "\n\n\n ^/u/SmallYTChannelBot ^*made* ^*by* ^/u/jwnskanzkwk. ^*PM* ^*for* ^*bug* ^*reports.* ^*For* ^*more* ^*information,* ^*read* ^*the* ^[FAQ.](https://www.reddit.com/user/SmallYTChannelBot/comments/a4u7qj/smallytchannelbot_faq/)" + + comment_stream = subreddit.stream.comments(pause_after=-1) + submission_stream = subreddit.stream.submissions(pause_after=-1) + while True: + try: + for comment in comment_stream: + if comment is None: + break + if not db.id_in_blacklist(comment.id): + db.add_to_blacklist(comment.id) + + if "!mylambda" in comment.body and str(comment.author) != "SmallYTChannelBot": + author = str(comment.author) + λ, links = db.get_lambda(author) + if author in get_mods(): + text = "/u/%s is a moderator, and therefore has ∞λ." % author + else: + if λ == 0: + text = "/u/%s has 0λ." % author + else: + text = "/u/%s has %iλ, from helping the following posts:" % (author, λ) + count = 0 + for link in links: + if "www.reddit.com" not in link: + link = "https://www.reddit.com" + link + + #set a max limit on the number of times this will iterate to stop it + #breaking because of Reddit's character limit. + count += 1 + text += "\n\n- [%s](%s)" % (reddit.submission(url = link).title, link) + if count > 20: #experminent with this number + text += "\n\n[%i more...]" % len(links) - count + break + + reply = comment.reply(text + tail) + reply.mod.distinguish(sticky = False) + update_users_flair(comment) + + + if "!givelambda" in comment.body and str(comment.author) != "SmallYTChannelBot": + submission = comment.submission + parentauthour = str(comment.parent().author) + op = str(comment.author) + if op == parentauthour: + text = "You cannot give yourself λ." + elif op == "SmallYTChannelBot": + text = "Please only give lambda to humans." + elif op != str(submission.author): + text = "Only the OP can give λ." + elif comment.is_root: + text = "You can only give λ to top-level comments." + else: + print("'/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) + + 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) + print("The OP has received lambda too!") + else: + db.give_lambda(parentauthour, submission.permalink) + + update_users_flair(comment) + update_users_flair(comment.parent()) + reply = comment.reply(text + tail) + reply.mod.distinguish() + + for submission in submission_stream: + if submission is None: + break + if not db.id_in_blacklist(submission.id): + db.add_to_blacklist(submission.id) + print("There has been a new submission: '%s', with flair '%s'" % (submission.title, submission.link_flair_text)) + + if str(submission.author) not in get_mods(): + score = db.get_lambda(str(submission.author))[0] + if submission.link_flair_text in ["Discussion", "Meta", "Collab"]: + text = "Your post is a discussion, meta or collab post so it costs 0λ." + else: + if score < 3: + text = """Thank you for submitting to /r/SmallYTChannel. Please be aware that soon you will need to have at least 3λ to submit here. + You currently have %iλ. /u/%s, please comment `!givelambda` to the most helpful advice you are given. You will be rewarded 1λ if you + do so. For more information, read the [FAQ](https://www.reddit.com/user/SmallYTChannelBot/comments/a4u7qj/smallytchannelbot_faq/)""" % (score, str(submission.author)) + #submission.mod.remove() + else: + #db.change_lambda(str(submission.author), -3) + text = """Thank you for submitting to /r/SmallYTChannel. You have spent 3λ to submit here, making your current balance %iλ. Soon + you will have to spend your λ to post here. /u/%s, please comment `!givelambda` to the most helpful advice you are given. You + will be rewarded 1λ if you do so. For more information, read the [FAQ](https://www.reddit.com/user/SmallYTChannelBot/comments/a4u7qj/smallytchannelbot_faq/)""" % (score, str(submission.author)) + + update_users_flair(submission) + reply = submission.reply(text + tail) + reply.mod.distinguish(sticky = True) + + except Exception as e: + print("[ERROR]\t%s" % e) + continue + +if __name__ == "__main__": + main() + diff --git a/SmallYTChannelDatabase.db b/SmallYTChannelDatabase.db Binary files differnew file mode 100644 index 0000000..2423b66 --- /dev/null +++ b/SmallYTChannelDatabase.db diff --git a/__pycache__/SmallYTChannelBot.cpython-35.pyc b/__pycache__/SmallYTChannelBot.cpython-35.pyc Binary files differnew file mode 100644 index 0000000..c2a99da --- /dev/null +++ b/__pycache__/SmallYTChannelBot.cpython-35.pyc diff --git a/__pycache__/SmallYTChannelBotSubmissions.cpython-36.pyc b/__pycache__/SmallYTChannelBotSubmissions.cpython-36.pyc Binary files differnew file mode 100644 index 0000000..e9d82cd --- /dev/null +++ b/__pycache__/SmallYTChannelBotSubmissions.cpython-36.pyc diff --git a/__pycache__/database.cpython-35.pyc b/__pycache__/database.cpython-35.pyc Binary files differnew file mode 100644 index 0000000..042d938 --- /dev/null +++ b/__pycache__/database.cpython-35.pyc diff --git a/__pycache__/database.cpython-36.pyc b/__pycache__/database.cpython-36.pyc Binary files differnew file mode 100644 index 0000000..5bca449 --- /dev/null +++ b/__pycache__/database.cpython-36.pyc diff --git a/__pycache__/login.cpython-36.pyc b/__pycache__/login.cpython-36.pyc Binary files differnew file mode 100644 index 0000000..bf91463 --- /dev/null +++ b/__pycache__/login.cpython-36.pyc diff --git a/archive_posts.py b/archive_posts.py new file mode 100644 index 0000000..ec6206b --- /dev/null +++ b/archive_posts.py @@ -0,0 +1,28 @@ +import praw +import database + +reddit = praw.Reddit(client_id = "PyyyRXa3veWsWA", + client_secret = "hAMbhuzdHIew5hmn1CFwWb5FurM", + user_agent = "SmallYTChannelBot", + username = "SmallYTChannelBot", + password = "6NEWGNPBjJjbOjk3lbtm") + +subreddit = reddit.subreddit("SmallYTChannel") +db = database.Database() + +comment_stream = subreddit.stream.comments(pause_after=-1) +submission_stream = subreddit.stream.submissions(pause_after=-1) +while True: + for comment in comment_stream: + if comment is None: + break + if not db.id_in_blacklist(comment.id): + print("archived: ", comment.id) + db.add_to_blacklist(comment.id) + + for submission in submission_stream: + if submission is None: + break + if not db.id_in_blacklist(submission.id): + print("archived: ", submission.id) + db.add_to_blacklist(submission.id) diff --git a/database.py b/database.py new file mode 100644 index 0000000..f31a338 --- /dev/null +++ b/database.py @@ -0,0 +1,86 @@ +import sqlite3 + +class Database: + def __init__(self): + self.connection = sqlite3.connect("SmallYTChannelDatabase.db") + self.cursor = self.connection.cursor() + + def change_lambda(self, user, changeby): + #this will make it go negative. You must check this operation is allowed. + self.cursor.execute("UPDATE users SET lambda = ((SELECT lambda FROM users WHERE user_name = ?) + ?) WHERE user_name = ?;", (user, changeby, user)) + self.connection.commit() + + def give_lambda(self, user, link, op = None): + def give(user, link = None): + #check if the user has an entry in the database + self.cursor.execute("SELECT userID FROM users WHERE user_name = ?;", (user, )) + try: + id_ = self.cursor.fetchone()[0] + except TypeError: + #the user isn't in the database + self.cursor.execute("INSERT INTO users (user_name, lambda) VALUES (?, 1);", (user, )) + self.connection.commit() + if link is not None: + self.cursor.execute("INSERT INTO lambdas (userID, permalink) VALUES ((SELECT userID FROM users WHERE user_name = ?), ?);", (user, link)) + else: + #update their lambda and add to lambdas + self.change_lambda(user, 1) + if link is not None: + self.cursor.execute("INSERT INTO lambdas (userID, permalink) VALUES (?, ?);", (id_, link)) + + self.connection.commit() + + #give one lambda to both the user and the OP + give(user, link) + if op is not None: + give(op) + + def get_lambda(self, user): + self.cursor.execute("SELECT lambda FROM users WHERE user_name = ?", (user, )) + try: + lambda_ = self.cursor.fetchone()[0] + except TypeError: + #the user isn't in the database, and therefore has no lambda + return 0, [] + else: + self.cursor.execute("SELECT permalink FROM lambdas WHERE userID = (SELECT userID FROM users WHERE user_name = ?);", (user, )) + links = [i[0] for i in self.cursor.fetchall()] + + return lambda_, links + + def link_in_db(self, link): + self.cursor.execute("SELECT permalink FROM lambdas;") + try: + links = [i[0] for i in self.cursor.fetchall()] + except TypeError: + links = [] + + return link in links + + def add_to_blacklist(self, id): + self.cursor.execute("INSERT INTO blacklist (prawID) VALUES (?);", (id, )) + self.connection.commit() + + def id_in_blacklist(self, id): + self.cursor.execute("SELECT prawID FROM blacklist;") + try: + ids = [i[0] for i in self.cursor.fetchall()] + except TypeError: + ids = [] + + return id in ids + + def get_scores(self): + self.cursor.execute("SELECT users.user_name, users.lambda, COUNT(users.user_name) FROM lambdas INNER JOIN users ON users.userID = lambdas.userID GROUP BY users.user_name;") + return self.cursor.fetchall() + + + def update_stats(self): + query = """INSERT INTO stats (lambdaCount, helpGiven, uniqueUsers, date) VALUES ( + (SELECT SUM(lambda) FROM users), + (SELECT COUNT(lambdaID) FROM lambdas), + (SELECT COUNT(user_name) FROM users), + (SELECT strftime('%Y-%m-%d')));""" + + self.cursor.execute(query) + self.connection.commit() diff --git a/login.py b/login.py new file mode 100644 index 0000000..67eb525 --- /dev/null +++ b/login.py @@ -0,0 +1,7 @@ +import praw + +REDDIT = praw.Reddit(client_id = "PyyyRXa3veWsWA", + client_secret = "hAMbhuzdHIew5hmn1CFwWb5FurM", + user_agent = "SmallYTChannelBot", + username = "SmallYTChannelBot", + password = "6NEWGNPBjJjbOjk3lbtm") |