Author Topic: conky - Gmail new email information (bash version)  (Read 708 times)

hakerdefo

  • Posts: 520
    • View Profile
    • Looking at Linux through the Windows of Life
conky - Gmail new email information (bash version)
« on: November 25, 2016, 04:04:18 PM »
As promised, ladies & gentlemen, here is a bash script to get new email info from your Gmail account for dislay in conky  8)

First we need to make sure that the script dependencies are installed,

Code: [Select]
sudo apt-get install xmlstarlet
sudo apt-get install curl
sudo apt-get install wget
sudo apt-get install coreutils

On VSIDO three of the above four should most likely be already installed  8)

Next open your favourite text-editor and copy the script,

Code: [Select]
#!/usr/bin/env bash
###the-script-begins###
###
type curl >/dev/null 2>&1 || { printf "\033c"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; echo -e '\e[38;5;82m'"A required dependency \"cURL\" is unavailable"; echo -e '\e[38;5;82m'"\"cURL\" is available for almost all linux distros."; echo -e '\e[38;5;82m'"Please install \"curl\" using your package manager."; echo -e '\e[38;5;82m'"Please visit the homepage of \"cURL\" for more info"; echo -e '\e[38;5;82m'"https://curl.haxx.se/"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; tput sgr0; exit 1; }
type wget >/dev/null 2>&1 || { printf "\033c"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; echo -e '\e[38;5;82m'"A required dependency \"Wget\" is unavailable"; echo -e '\e[38;5;82m'"\"Wget\" is available for almost all linux distros."; echo -e '\e[38;5;82m'"Please install \"wget\" using your package manager."; echo -e '\e[38;5;82m'"Please visit the homepage of \"Wget\" for more info"; echo -e '\e[38;5;82m'"https://www.gnu.org/software/wget/"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; tput sgr0; exit 1; }
type xmlstarlet >/dev/null 2>&1 || { printf "\033c"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; echo -e '\e[38;5;82m'"A required dependency \"XMLStarlet\" is unavailable"; echo -e '\e[38;5;82m'"\"XMLStarlet\" is available for almost all linux distros."; echo -e '\e[38;5;82m'"Please install \"xmlstarlet\" using your package manager."; echo -e '\e[38;5;82m'"Please visit the homepage of \"XMLStarlet\" for more info"; echo -e '\e[38;5;82m'"http://xmlstar.sourceforge.net/"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; tput sgr0; exit 1; }
wget --spider --user-agent="Mozilla/5.0 Gecko/20100101" --timeout=30 -q "www.google.com" -O /dev/null
RETVAL=$?
if [[ ! "$RETVAL" == 0 ]]; then
echo -e ""
echo -e "Internet Connection Seems Down."
echo -e ""
exit
fi
local_user="gmail_username"
local_pass="gmail_password"
local_count="5"
local_rm=$(which rm)
curl --silent --connect-timeout 30 --user-agent "Mozilla/5.0 Gecko/20100101" "https://"$local_user":"$local_pass"@mail.google.com/mail/feed/atom" -o /tmp/cgmail.xml
grep "401" "/tmp/cgmail.xml"
RETVAL=$?
if [[ "$RETVAL" == 0 ]]; then
echo -e ""
echo -e "Incorrect username or password."
echo -e ""
"$local_rm" "/tmp/cgmail.xml"
exit
fi
mail_count=$(xmlstarlet sel -N x="http://purl.org/atom/ns#" -t -m '//x:feed' -v  x:fullcount -n /tmp/cgmail.xml)
if [[ "$local_count" == 0 ]]; then
echo -e ""
echo -e "$local_user :: No New Email."
echo -e ""
"$local_rm" "/tmp/cgmail.xml"
exit
fi
if (( "$local_count" < "$mail_count" )); then
final_count="$local_count"
else
final_count="$mail_count"
fi
local_subject=$(cat "/tmp/cgmail.xml" | xmlstarlet sel -N x="http://purl.org/atom/ns#" -t -m "//x:entry[position()<=$final_count]" -v x:title -n)
mail_date=$(cat "/tmp/cgmail.xml" | xmlstarlet sel -N x="http://purl.org/atom/ns#" -t -m "//x:entry[position()<=$final_count]" -v x:issued -n)
local_date=$(while IFS= read -r line ; do date -d"$line" +%y-%m-%d ; done <<< "$mail_date")
echo -e ""
echo -e "$local_user :: \"$mail_count\" New Emails."
echo -e ""
pr -m -t <(echo "$local_date") <(echo "$local_subject")
echo -e ""
"$local_rm" "/tmp/cgmail.xml"
exit 0
###
###the-script-ends###


Go to the line 15-16 in the script and change "gmail_username" &  "gmail_password" to reflect your Google account credentials,

Code: [Select]
local_user="gmail_username"
local_pass="gmail_password"

The script by default prints info of the "5" newest emails. You can change this number on line 17 to display as many new emails as you wish,

Code: [Select]
local_count="5"

Save the script somewhere in your $PATH with the name "check-gmail" and give it execute permissions,

Code: [Select]
chmod 755 /path/to/check-gmail

And finally add following line in your ".conkyrc" to call the script from conky,

Code: [Select]
execi 600 bash /path/to/check-gmail

Of-course "/path/to/check-gmail" above will have to be replaced with the actual path of the script.

It is very easy to colorize the output of the script. Look for the "echo" commands in the script and adjust them to print the output in your favourite colors. If you need any help regarding this script don't hesitate to post your query here  :)

Cheers!!!
You Can't Always Git What You Want

Ritsompong

  • sbo
  • Posts: 1
    • View Profile
Re: conky - Gmail new email information (bash version)
« Reply #1 on: March 21, 2017, 03:44:38 AM »
The knowledge I acquired after reading the above content. It is new knowledge for me and it is a very useful knowledge.