Saturday, December 25, 2010

"ruby, any messages for me?"

i have successfully consolidated all of my email accounts to gmail. this makes it easy for me to read all my messages from my android phone or other portable internet devices. during the work week i'm rather tied to my email, but it does no good to leave a web browser tab open on the gmail site while i'm teaching class, and it eats up resources unnecessarily. so, i knocked off this little ruby script to check the google xml feed and determine if i have any new mail messages. this simply displays in my conky resource monitor on my desktop, but you can do something fancier, even use this in a more elaborate mail fetching script, if you wish.

01 #!/usr/bin/ruby
02 # gmail.rb
03 # Checks a gmail account and displays the number of unread (new) messages
04 # waiting on the server.
05 # Robert Ritter <rritter@centriq.com>
06 # December 20, 2010
07
08 # Use 'nokogiri' to parse XML.
09 require 'nokogiri'
10 require 'open-uri'
11
12 username = 'not_a_real_user@gmail.com'
13 password = 'not_a_real_password_either'
13
14 tree = Nokogiri.XML(open('https://mail.google.com/mail/feed/atom',
15 :http_basic_authentication=>[username, password]))
16
17 puts tree.xpath('//xmlns:fullcount').first.content
18

until next time!

No comments:

Post a Comment