Today I decided to learn some Python with my Raspberry Pi.
I already have an Ambilight/Boblight system hooked up to my Raspberry Pi (you can read about it here), essentially it’s a series of LEDs mounted on the edge of my TV which change colour in sync with the video playing on the Raspberry Pi/TV.
I decided to try and make these LEDs flash whenever I receive an email. It was actually pretty easy so I set it up to also flash when I get Facebook notifications or mentions on Twitter.
It works by reading RSS feeds at a set interval and checking if there have been any changes since it last checked.
It seems to work, it flashes a different colour for each type of notification I receive so I’m happy with that. I doubt I will actually use this code as my phone already does this, but it was a bit of fun and I’ve learnt some Python in the process.
With some modifications, you could get the code to turn on a single LED connected to the Pi’s GPIO, play a sound, boil the kettle or whatever else you can think of!
You could also combine the Gmail portion of code with any of the triggers at If This Then That. So you could change the colour of your LEDs depending on the weather forecast, or an event in your Google Calendar or…
If you want to give it a go you will need to install feedparser, if you haven’t done so already:
sudo pip install feedparser
My dodgy looking Python script:
import feedparser, time, sys, os #variables you need to set user = "xxx" #gmail username passw = "xxx" #gmail password twUser = "@_nadnerb" #twitter username interval = 900 #seconds between checking for updates numFlashes = 3 #number of LED flashes #these are the RSS URLs to parse gmailURL = "https://" + user + ":" + passw +"@mail.google.com/gmail/feed/atom" twURL = "http://search.twitter.com/search.atom?q="+twUser fbURL = "http://www.facebook.com/feeds/notifications.php?id=xxxxxxxxxxxxxxxxxxxxxx" #replace the above FB link with your personal FB RSS feed URL which you will find here: #https://www.facebook.com/notifications # these are the colours you want to flash when you get an update flashRed = "boblight-constant -p 127 -o value=1 FF0000 -f > /dev/null" flashGreen= "boblight-constant -p 127 -o value=1 00FF00 -f > /dev/null" flashBlue = "boblight-constant -p 127 -o value=1 0088FF -f > /dev/null" killBoblight = "kill $(ps aux | grep '[b]oblight-constant' | awk '{print $2}' | sed '$!d') > /dev/null" # command which kills the most recent boblight-constant instance #get the initial values which we check against later no_of_emails = int(feedparser.parse(gmailURL)["feed"]["fullcount"]) #parse the gmail feed to get number of unread emails fbFeed = feedparser.parse(fbURL)["channel"] #parse the fb feed twFeed = feedparser.parse(twURL)["feed"] #parse the tw feed twUpdated = twFeed.updated # extract the date/time of the last update fbUpdated = fbFeed.updated # extract the date/time of the last update #start a loop while True: ####### GMAIL ######### no_of_emails_new = int(feedparser.parse(gmailURL)["feed"]["fullcount"]) if no_of_emails_new > no_of_emails: for i in range(numFlashes): os.system(flashRed) os.system(killBoblight) #end for no_of_emails = no_of_emails_new #reset the count as we only want 1 email alert #end if ####### TWITTER ######### twUpdated_new = feedparser.parse(twURL)["feed"] if twUpdated_new.updated > twUpdated: #checks if RSS feed has been updated for i in range(numFlashes): os.system(flashGreen) os.system(killBoblight) #end for twUpdated = twUpdated_new.updated #end if ####### FACEBOOK ######### fbFeed_new = feedparser.parse(fbURL)["channel"] if fbFeed_new.updated > fbUpdated: #checks if RSS feed has been updated for i in range(numFlashes): os.system(flashBlue) os.system(killBoblight) #end for fbUpdated = fbFeed_new.updated #end if time.sleep(interval)
Awesome! I will try it.
Hey, I’m getting an error message when using the .updated function that looks like this: http://i1194.photobucket.com/albums/aa378/Gorgoth117/2014-05-23151423.jpg
Is it that the function no longer works or is it more likely that I’ve made a mistake?
Actually I isolated the code and it only gives an error for the twitter rss feed.
I tried this link in my browser http://search.twitter.com/search.atom?q=@_nadnerb
It turns out this API has been replaced with a new one.
If I get the chance this weekend I will see if I can get it working again.
I am also having this KeyError posted by Mr. Andy Grant. Is there any way for LinkedIn also ?
Yeah it’s broken because Twitter have changed their API. You now need to create a custom app, grant access to it and set up authentication etc. Not worth the effort imo.
You could possibly look at using https://ifttt.com/ to check twitter or linkedin for updates and then forward them to a certain email address which autolabels them based on keywords. Then use the above python to check the individual labels in your gmail account. e.g You setup a “Linkedin” label in gmail, you then use this URL to check it https://mail.google.com/mail/feed/atom/Linkedin
Hope that helps.
But IFTTT has some limited “Actions” which are of no use for me.. If I have to do like that , then I can simply use gmail notifications by activating mail notifications on facebook,twitter,linkedin, anything and everything.
Thanx for the suggestion. 🙂
question – could i have it blink everytime a particular person’s name shows up on my feed. I wouldnt want it forevery RSS update on my facebook feed – just when 1 particular person post anything
I guess you could do that, you’d need to parse the RSS feed and look for the name.
Anyway you can help me with the code for that 😉
actually trying to get the facebook RSS to work – seems that they broke the RSS too – it now requires a user and a key – thoughts?