Skip to content

Instantly share code, notes, and snippets.

@Madh93
Last active April 16, 2016 19:45
Show Gist options
  • Save Madh93/99049e9b51685313dc62 to your computer and use it in GitHub Desktop.
Save Madh93/99049e9b51685313dc62 to your computer and use it in GitHub Desktop.
Check and notify new gmails
#! /usr/bin/env ruby
# NAME : gmailer.rb
# DESCRIPTION : Check and notify new gmails
# AUTHOR : Madh93 (Miguel Hernandez)
# VERSION : 0.0.1
# LICENSE : GNU General Public License v3
# USAGE : ruby gmailer.rb XML [OPTION]
require 'nokogiri'
class Gmailer
def initialize(xml)
@xml = Nokogiri::XML(xml)
@mails = @xml.xpath('//xmlns:entry')
end
def count
@xml.xpath('//xmlns:fullcount').children
end
def read(n)
name = @mails.xpath('//xmlns:name')[n].children.to_s
title = @mails.xpath('//xmlns:title')[n+1].children.to_s
"#{name.gsub(';',"")};#{title.gsub(';',"")}"
end
end
# Main
case ARGV[1]
when "-c", "--count"
puts Gmailer.new(ARGV[0]).count
when "-r", "--read"
puts Gmailer.new(ARGV[0]).read(ARGV[2].to_i)
end
#! /bin/bash
# NAME : gmailer.sh
# DESCRIPTION : Check and notify new gmails
# AUTHOR : Madh93 (Miguel Hernandez)
# VERSION : 0.0.1
# LICENSE : GNU General Public License v3
# USAGE : bash gmailer.sh
# CONFIG
GMAIL_RUBY_SCRIPT="" # $HOME/gmailer.rb
GMAIL_USER="" # mygmailuser
GMAIL_PASS="" # mygmailpassword
GMAIL_ICON="" # $HOME/Images/gmail.png
GMAIL_SOUND="" # $HOME/Sounds/gmail.oga
GMAIL_INTERVAL="" # 1m
# Run
while true; do
xml=$(curl -u $GMAIL_USER:$GMAIL_PASS --silent 'https://mail.google.com/mail/feed/atom')
count=$(echo $xml | grep "<fullcount>0</fullcount>")
if [ -n "$xml" ] && [ -z "$count" ]; then
# Sound notification
paplay $GMAIL_SOUND
# Desktop notifications
count=$(ruby $GMAIL_RUBY_SCRIPT "$xml" --count)
for (( i=0; i<$count; i++ )); do
mail=$(ruby $GMAIL_RUBY_SCRIPT "$xml" --read $i)
name=$(echo $mail | cut -d ';' -f1)
title=$(echo $mail | cut -d ';' -f2)
notify-send --icon=$GMAIL_ICON --expire-time=5000 "$name" "$title"
done
fi
sleep $GMAIL_INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment