Skip to content

Instantly share code, notes, and snippets.

@guimello
Created July 17, 2010 18:34
Show Gist options
  • Save guimello/479733 to your computer and use it in GitHub Desktop.
Save guimello/479733 to your computer and use it in GitHub Desktop.
crazy ubuntu gmail checker
#!/usr/bin/env ruby
################################################################################
# Require.
require 'rubygems'
require 'gmail'
require 'libnotify'
################################################################################
# Reporter that checks for unread messages.
class Reporter
################################################################################
# Constants.
DEFAULT_MESSAGE = 'Most important are: '
MEDIUM = {}
HIGH = {}
################################################################################
# Attributes
attr_reader :total_unread, :report
attr_accessor :gmail
################################################################################
def initialize(options = {})
@report = options[:report] || DEFAULT_MESSAGE
@gmail = options[:gmail] || raise('kant')
@total_unread = 0
end
################################################################################
def check(label)
return if (label =~ (/(\[Gmail\])+/)) || (unread = @gmail.in_label(label).count :unread) <= 0
@report += "#{HIGH[label]} => #{unread} | " if HIGH.key? label
@total_unread += unread
end
################################################################################
def message
(@report == DEFAULT_MESSAGE) ?
(@total_unread > 0) ?
'No new important messages, but you still have to check your e-mail for the other ones... ;)' :
'No messages for now... ;)' :
@report[0..-4]
end
end
################################################################################
runs = 0
while true do
################################################################################
begin
Gmail.new 'user', 'pass' do |gmail|
reporter = Reporter.new :gmail => gmail
is_low = runs % 4 == 0
gmail.labels.each do |label|
if !Reporter::MEDIUM.key?(label) && !Reporter::HIGH.key?(label)
reporter.check label if is_low
elsif is_low || runs % 3 == 0 || !Reporter::MEDIUM.key?(label)
reporter.check label
end
end
if reporter.total_unread > 0
Libnotify.show :summary => "#{reporter.total_unread} unread message(s)",
:body => reporter.message,
:timeout => 10,
:icon_path => "",
:urgency => :critical
#system "aplay 'path'"
end
end
rescue Exception
end
################################################################################
sleep 120 rescue nil
runs += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment