0

I'm trying to make a script that gets the number of various notifications from websites I use. Unread emails, you number of unread reddit/facebook messages, and I'd also like to get the number of stackoverflow notifications.

Unfortunately googling any python scripts to get information from stack overflow gets me questions on stack overflow, not regarding stack overflow.

Is it possible to get something along the lines of these two scripts but for stack exchange?

import imaplib
import praw

obj = imaplib.IMAP4_SSL("imap.gmail.com","993")
obj.login("","")
obj.select()
obj.search(None,'UnSeen')
unread_gmail = len(obj.search(None, "UnSeen")[1][0].split())

print("Unread emails:", unread_gmail)


r = praw.Reddit(user_agent = "example")
r.login("", "")
i = 0
for message in r.get_unread(): i += 1 
unread_reddit = i

print("Unread reddit:", unread_reddit)
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Charles Clayton
  • 17,005
  • 11
  • 87
  • 120

1 Answers1

1

You will make a request to the /me/notifications/unread route.

At minimum, this needs to contain an access_token, with a scope containing read_inbox and a key. This will return a list of notifications that have not been read.

To get an access_token, you must authenticate the user. The key is received after you register your application.

Example:

import requests
import json
import pprint

TOKEN = "YOURTOKEN"
KEY = "YOURKEY"
SITE = "stackoverflow"
REQUEST_URL = "https://api.stackexchange.com/2.2/me/notifications/unread"

params = {
            "pagesize": 1,
            "filter": 'default',
            "site": SITE,
            "key": KEY,
            "access_token": TOKEN
        }

response = requests.get(REQUEST_URL, params=params)
json_response = response.json()
pprint.pprint(json_response, indent=4)

Outputs:

{   u'has_more': False,
    u'items': [],
    u'quota_max': 10000,
    u'quota_remaining': 9823}

items is empty, as I have no notifications at the moment.

Andy
  • 49,085
  • 60
  • 166
  • 233