Keep an eye on your facebook account unseen notification.

Facebook unseen notification counter at your OSX StatusBar.

Posted by Gediminas Siutilas on November 30, 2014

Preface

Using Facebook GraphAPI your facebook account unseen notifications counter can be extracted to your OSX statusbar.

Don't waste your time by constantly checking your facebook account for nothing. Be there when it's reasonable to be there.

Configuration

In order to make this script working you need to acquire a valid access token. Easiest way is to open GraphAPI explorer and copy/paste automatically generated short lived access token.

# https://developers.facebook.com/tools/explorer, "Manage notifications" access should be granted.
#@@@{Access token (short-lived). You can obtain it through the FB explorer at https://developers.facebook.com/tools/explorer}
accessToken=ABBABASDBASDLJSDJLAJS..
In case access token is expired or invalid icon will be changed accordingly:

Script:

Download
#!/bin/bash

#   ____ __        __ __ _      __                       __         
#  / __// /  ___  / // /| | /| / /____ ___ _ ___  ___ _ / /___  ____
# _\ \ / _ \/ -_)/ // / | |/ |/ // __// _ `// _ \/ _ `// // -_)/ __/
#/___//_//_/\__//_//_/  |__/|__//_/   \_,_//_//_/\_, //_/ \__//_/   
#                                               /___/               
#                                              
# Script author: gadominas@gmail.com
# 
#$$${ Show the number of Facebook unseen notifications. }

# https://developers.facebook.com/tools/explorer, "Manage notifications" access should be granted.
#@@@{Access token (shortlived). You can obtain it through the FB explorer at https://developers.facebook.com/tools/explorer}
accessToken=VALID-TOKEN-TO-BE-RETRIEVED

showUnseenNotifications() {
	unseenCount=$(echo $1 | grep -Eo "unseen_count\":[0-9]*" | sed -e 's/unseen_count\"://')
	
	if [ ! -z "$unseenCount" -a "$unseenCount"!=" " ];
		then
			printf "%s" $unseenCount
		else 
			printf "0"
	fi
}

isAuthRequired() {
	authRequired=$(echo $1 | grep -Eo "OAuthException" | wc -l)

	if [ "$authRequired" -eq 1 ];
		then
			return 1
		else
			return 0
	fi
}

checkFBNotifications() {	
	notifContext=$(/usr/bin/curl --silent \
		https://graph.facebook.com/me/notifications?access_token=$accessToken)
	 
	if [ ! -z "$notifContext" -a "$notifContext"!=" " ];
		then {
			isAuthRequired "$notifContext"
			
			if [ "$?" -eq 0 ]
				then
					showUnseenNotifications "$notifContext"
				else 
					echo "🔑"
			fi	
		}	
		else 
			## Connection timeout?
			printf "?!"
	fi
}


run() {
	printf "🌏|"
	checkFBNotifications
}

run