Last active
April 16, 2016 19:45
-
-
Save Madh93/99049e9b51685313dc62 to your computer and use it in GitHub Desktop.
Check and notify new gmails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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