Skip to content

Instantly share code, notes, and snippets.

@notesbot
Last active September 18, 2022 19:29
Show Gist options
  • Save notesbot/a7f9100907bae3cda002aee54f492b18 to your computer and use it in GitHub Desktop.
Save notesbot/a7f9100907bae3cda002aee54f492b18 to your computer and use it in GitHub Desktop.
# Usernotes Notifier Bot v 2.0 by u/BuckRowdy
from datetime import datetime
import praw
import time
import pmtw
import re
import json
reddit = praw.Reddit(
user_agent="UserNotes Notifier Bot v 2.0 by u/BuckRowdy",
client_id="",
client_secret="",
refresh_token="",
)
print("Connecting to reddit...")
print("Starting up...")
print(f"Logged in as: {reddit.user.me()}")
note_threshold = 5
user_blacklist = {}
with open("/home/pi/bots/usernotes/usernotes_log.json", "w+") as outfile:
try:
user_blacklist = json.load(outfile)
except Exception:
pass
print("Listening for new usernotes...")
for log in reddit.subreddit("mod").mod.stream.log(skip_existing=True):
log_subreddit = log.subreddit
sub_name = reddit.subreddit(f"{log_subreddit}")
log_details = log.description
if log.subreddit == 'PoliticalHumor':
note_threshold = 19
if log.action == "wikirevise":
match = re.search(
r"\bcreate new note on ([\w-]{3})?.?user \x27?([\w-]{3,20})", log_details
)
if match:
username = match.group(2)
print(f"New Note left in r/{sub_name}")
notes = pmtw.Usernotes(reddit, sub_name)
settings = pmtw.Settings(reddit, sub_name)
users_notes = notes.get_user_notes(username)
note_list = []
for notecount, note in enumerate(users_notes):
note_list.append(
f"{note.note} | {datetime.fromtimestamp(note.time)} | {note.warning} | {note.mod} | [link]({note.link})"
)
notecount = notecount + 1
print(f"Total Notes for u/{username}: {notecount}")
for note in note_list:
new_notelist = "\n".join(note_list)
if notecount >= note_threshold:
if username in user_blacklist:
if log_subreddit in user_blacklist[username]:
print(f"This user is already in user blacklist for {log_subreddit}.")
else:
message_title = f"Usernotes update: u/{username} has {notecount} notes in r/{sub_name}."
message_body = f"**I found the following notes for u/{username}**:\n\n"+table_header+new_notelist
reddit.subreddit(f"{sub_name}").message(
subject=message_title, message=message_body
)
add_to_dict = {f'{username}' : f'{log_subreddit}'}
user_blacklist.update(add_to_dict)
json.dump(user_blacklist, outfile)
print("Message was sent and user added to blacklist.\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment