Last active
November 22, 2020 17:59
-
-
Save liamstrilchuk/89374b9c4a9499bca8a4a677a172f01f to your computer and use it in GitHub Desktop.
nicebot
This file contains 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 praw, sys, json | |
login_details = json.loads(open("login.json", "r").read()) | |
reddit = praw.Reddit(client_id=login_details["client_id"], \ | |
client_secret=login_details["client_secret"], \ | |
user_agent=login_details["user_agent"], \ | |
username=login_details["username"], \ | |
password=login_details["password"]) | |
scores = {} | |
delete = False | |
deleted = 0 | |
if len(sys.argv) < 2: | |
delete = False | |
else: | |
delete = sys.argv[1].lower() == "yes" | |
for comment in reddit.redditor("nicebot2").comments.new(limit=250): | |
if not comment.score in scores: | |
scores[comment.score] = 1 | |
else: | |
scores[comment.score] += 1 | |
if delete and comment.score < 1: | |
#print(f"deleting post with {comment.score} score") | |
comment.delete() | |
deleted += 1 | |
sorted_scores = [item for item in sorted(scores, reverse=True)] | |
total_score = 0 | |
to_delete = 0 | |
total_comments = 0 | |
for item in sorted_scores: | |
total_score += (item - 1) * scores[item] | |
total_comments += scores[item] | |
if item < 1: | |
to_delete += scores[item] | |
#print(f"{item}:{' ' * (15 - len(str(item)))}{scores[item]}") | |
#print(f"\nTotal score: {total_score}") | |
#print(f"To delete: {to_delete}") | |
#print(f"Total comments: {total_comments}") | |
data = json.loads(open("stats.json", "r").read()) | |
data["comments_deleted"] += deleted | |
open("stats.json", "w").write(json.dumps(data)) |
This file contains 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 praw, os, json, prawcore, re | |
login_details = json.loads(open("login.json", "r").read()) | |
reddit = praw.Reddit(client_id=login_details["client_id"], \ | |
client_secret=login_details["client_secret"], \ | |
user_agent=login_details["user_agent"], \ | |
username=login_details["username"], \ | |
password=login_details["password"]) | |
counter = 0 | |
regex = r"\b69\b(?!\S)" | |
def in_optout(name): | |
usernames = open("optout.txt", "r").read().split("\n") | |
for username in usernames: | |
if username.lower() == name.lower(): | |
return True | |
return False | |
for comment in reddit.subreddit("all").stream.comments(): | |
if re.search(regex, comment.body): | |
try: | |
if in_optout(comment.author.name): | |
print(f"User opted out, name: {comment.author.name}") | |
continue | |
except: | |
continue | |
if len(comment.body) < 200 and not comment.subreddit.over18: | |
counter += 1 | |
if counter % 5 == 0: | |
os.system("cd ~/Documents && python3 delete.py yes") | |
try: | |
comment.reply("Nice\n\n^I'm ^a ^bot. ^Join ^my ^community ^at ^r/nicebot2.") | |
data = {} | |
with open("stats.json", "r") as json_file: | |
data = json.loads(json_file.read()) | |
if comment.subreddit.display_name in data["subreddits"]: | |
data["subreddits"][comment.subreddit.display_name] += 1 | |
else: | |
data["subreddits"][comment.subreddit.display_name] = 1 | |
data["total_comments"] += 1 | |
f = open("stats.json", "w") | |
f.write(json.dumps(data)) | |
f.close() | |
print(f"Replied, name: {comment.author.name}, sub: {comment.subreddit.display_name}") | |
except: | |
data = {} | |
with open("stats.json", "r") as json_file: | |
data = json.loads(json_file.read()) | |
data["errors"] += 1 | |
f = open("stats.json", "w") | |
f.write(json.dumps(data)) | |
f.close() | |
try: | |
print(f"Error gotten, name: {comment.author.name}, sub: {comment.subreddit.display_name}") | |
except: | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment