0

So i'm checking for new notifications on my facebook using a python script. The script suns perfectly when there are unread notifications, but when there are no notifications the "except" clause gets executed even though I have entered an if else clause before to try to solve this issue.

Code:

while (True):
    try:
        graph = facebook.GraphAPI(access_token)
        notifications = graph.get_object("me/notifications")
        print "notifications"
        if len(notifications['summary']) != 0:
            unseen_notifications = notifications['summary']['unseen_count']
            if(int(unseen_notifications) > 0):
                print("> You have " + str(unseen_notifications) + " unseen Facebook notifications!")
                ser.write("1")
                print "Wrote to Arduino :D"
            else:
                print "No New Notifications"
                ser.write("0")
    except:
        print "Notifications are null!"
        ser.write("0")
    time.sleep(5)

So every time there are no new notifications the code enters the except clause which is not what I need. Any help is much appreciated :D

nick28
  • 67
  • 3
  • 15
  • 1
    As a hint, I would remove the try/except statement to see what error you are actually getting on which line. I suspect a KeyError on line 6 or 7. – Rob Nov 03 '14 at 08:44
  • does an empty notification reply have the "summary" key? – Reut Sharabani Nov 03 '14 at 08:45
  • @Rob I removed them and got KeyError: 'summary' on line 6 – nick28 Nov 03 '14 at 08:49
  • @ReutSharabani I'm new to this facebook api, but I don't know any other method to check wether there are no notifications – nick28 Nov 03 '14 at 08:50
  • Just debug or print the reply and see what it actually has. Facebook APIs used to be outdated everytime I tried using them, so I suggest you see for yourself. Make sure the reply object that you get back when there are no notifications actually has 'summary'. you can also try: `if 'summary' in notifications` – Reut Sharabani Nov 03 '14 at 08:52
  • @ReutSharabani I tried to enter if(notifications['summary']) but it also gave me KeyError – nick28 Nov 03 '14 at 09:05
  • `KeyError` means the key does not exist. You should make sure the `notifications` object has the key you're trying to access **before** accessing it. It's usually done by using: `if notifications and 'key' in notifications:` – Reut Sharabani Nov 03 '14 at 09:28

1 Answers1

0

If there are no new notifications the Facebook API probably doesn't return any content. You also got a key error on 'summary' which means that Facebook is not returning content with the key 'summary' if there are no new conditions.

Try:

if notifications['data']: 

or (because this is the way you have written it currently):

if not notifications['data']:

If these do not work check what response Facebook is giving you when there are no new notifications. Just print that response and adapt your if statement to check for that response.

EDIT: added the answer that worked from the comments

  • i tried both statements, the first one (without not) makes the script skip anything after it, the second one didn't affect the script it continues to enter the except clause. – nick28 Nov 03 '14 at 09:04
  • how can I check what response is Facebook giving me? sorry for these questions but i'm new to pythn. – nick28 Nov 03 '14 at 09:09
  • okay, can you try printing str(notifications) and posting it here? – AlexanderL92 Nov 03 '14 at 09:11
  • I just did, it gave me: {u'data': []} – nick28 Nov 03 '14 at 09:14
  • can you try checking for: if not notifications['data'] ? – AlexanderL92 Nov 03 '14 at 09:17
  • 1
    @AlexanderL92, you can simple do `if notifications` or `if not notifications`. Also, when comparing against `None` you should use `is` for comparison, since `id(None) == id(None)` is always true. Read more here: http://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs – Reut Sharabani Nov 03 '14 at 09:20
  • 1
    @ReutSharabani Ah yes, I just thought of the inherent Boolean values of the lists and edited my comment. But I didn't realize you could do just `if notifications`. Don't you need the `notifactions['data']` at least to get the list? And thanks for the tip about `None`. – AlexanderL92 Nov 03 '14 at 09:22