Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- username = ''
- user_agent = 'modmail archiver by /u/Jakeable, running as /u/' + username
- client_id=''
- client_secret=''
- password = ''
- subreddit = 'subreddit'
- bot_auth = None
- def get_bot_auth():
- client_auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
- post_data = {"grant_type": "password", "username": username, "password": password}
- headers = {"User-Agent": user_agent}
- response = requests.post("https://www.reddit.com/api/v1/access_token", auth=client_auth, data=post_data, headers=headers)
- global bot_auth
- bot_auth = response.json()['access_token']
- def get_notifications():
- bearer = "bearer " + bot_auth
- headers = {"Authorization": bearer, "User-Agent": user_agent}
- response = requests.get("https://oauth.reddit.com/api/mod/conversations/?state=notifications&entity=" + subreddit + "&limit=1000", headers=headers)
- d = json.loads(response.text)
- return d
- def process():
- get_bot_auth()
- conversations = get_notifications()["conversations"]
- for key in conversations.keys():
- c = conversations[key]
- valid_author = False
- for author in c["authors"]:
- if author["name"].lower() in ["automoderator"]: #add other bot accounts if needed
- valid_author = True
- if "[notification]" in c["subject"] or valid_author:
- url = "https://oauth.reddit.com/api/mod/conversations/{0}/archive".format(key)
- bearer = "bearer " + bot_auth
- headers = {"Authorization": bearer, "User-Agent": user_agent}
- print("archived: " + key)
- requests.post(url, headers=headers)
- else:
- print("didn't archive: " + key)
- process()
Add Comment
Please, Sign In to add comment