jakeable

Archive All Notifications

Mar 7th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. import requests
  2. import json
  3.  
  4. username = ''
  5. user_agent = 'modmail archiver by /u/Jakeable, running as /u/' + username
  6. client_id=''
  7. client_secret=''
  8. password = ''
  9. subreddit = 'subreddit'
  10. bot_auth = None
  11.  
  12. def get_bot_auth():
  13.     client_auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
  14.     post_data = {"grant_type": "password", "username": username, "password": password}
  15.     headers = {"User-Agent": user_agent}
  16.     response = requests.post("https://www.reddit.com/api/v1/access_token", auth=client_auth, data=post_data, headers=headers)
  17.     global bot_auth
  18.     bot_auth = response.json()['access_token']
  19.  
  20. def get_notifications():
  21.     bearer = "bearer " + bot_auth
  22.     headers = {"Authorization": bearer, "User-Agent": user_agent}
  23.     response = requests.get("https://oauth.reddit.com/api/mod/conversations/?state=notifications&entity=" + subreddit + "&limit=1000", headers=headers)
  24.     d = json.loads(response.text)
  25.     return d
  26.  
  27. def process():
  28.     get_bot_auth()
  29.     conversations = get_notifications()["conversations"]
  30.     for key in conversations.keys():
  31.         c = conversations[key]
  32.         valid_author = False
  33.         for author in c["authors"]:
  34.             if author["name"].lower() in ["automoderator"]:  #add other bot accounts if needed
  35.                 valid_author = True
  36.         if "[notification]" in c["subject"] or valid_author:
  37.             url = "https://oauth.reddit.com/api/mod/conversations/{0}/archive".format(key)
  38.             bearer = "bearer " + bot_auth
  39.             headers = {"Authorization": bearer, "User-Agent": user_agent}
  40.             print("archived: " + key)
  41.             requests.post(url, headers=headers)
  42.         else:
  43.             print("didn't archive: " + key)
  44.  
  45. process()
Add Comment
Please, Sign In to add comment