You are not logged in.

#1 2012-07-26 11:31:52

simon.swe
Member
From: sverige
Registered: 2012-02-26
Posts: 148
Website

gmail script

I need some way to check the gmail, that just gives me the number of unread mail. Is there any scripts for that?

Offline

#2 2012-07-26 11:42:17

knopwob
Member
From: Hannover, Germany
Registered: 2010-01-30
Posts: 239
Website

Re: gmail script

The wiki article about conky has some examples.

Offline

#3 2012-07-26 11:43:21

Onyros
Member
From: Lisbon, Portugal
Registered: 2007-10-11
Posts: 307

Re: gmail script

Sure.

#!/usr/bin/env python2

import os
import string

#Enter your username and password below within double quotes
# eg. username="username" and password="password"
username="username"
password="absolutelybrilliantpassword"

com="wget -O - https://"+username+":"+password+"@mail.google.com/mail/feed/atom --no-check-certificate"

temp=os.popen(com)
msg=temp.read()
index=string.find(msg,"<fullcount>")
index2=string.find(msg,"</fullcount>")
fc=int(msg[index+11:index2])

if fc==0:
   print "0 new"
else:
   print str(fc)+" new"

I have this script feeding my DWM statusbar with info, but I don't really remember where I got it from, as I only have it saved as gmail.py smile

edit: OK, knopwob beat me to it! And as bonus I got to know where I got the script from wink

Last edited by Onyros (2012-07-26 11:44:48)

Offline

#4 2012-07-26 13:23:12

hesse
Member
Registered: 2011-12-08
Posts: 88

Re: gmail script

Nice gmail script. Is it safe to run this (quering gmail) every second for use with dwm statusbar?

Offline

#5 2012-07-26 14:14:57

Onyros
Member
From: Lisbon, Portugal
Registered: 2007-10-11
Posts: 307

Re: gmail script

I use it with DWM, but the script "sleeps" for 5 seconds.

#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin

delim=" | ";

while true; do

	freq=`sed 8q /proc/cpuinfo | awk '/cpu MHz/{print int($4)}'`;

	memtotal=`awk '/MemTotal/{print $2}' < /proc/meminfo`;
	memcached=`sed 4q /proc/meminfo | awk '/Cached/{print $2}'`;
	membuffers=`awk '/Buffers/{print $2}' < /proc/meminfo`;
	memory=$(( $memtotal/1024 ));
	memfree=`awk '/MemFree/{print $2}' < /proc/meminfo`;
	memfreez=$(( ($memtotal-$memfree) ));
	freemem=$(( ($memfreez-$membuffers-$memcached)/1024 ));
	
	battery=`acpi`;
	
	ctime=`date '+%a, %d %b %H:%M'`;
	
	weather=`curl -s --connect-timeout 10 http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=1\&locCode\="EUR|PT|PO012|Lisbon" \ | sed -n '/Currently:/ s/.*: \(.*\): \([-0-9]*\)\([CF]\).*/\2°\3, \1/p'`;
	
	mail=`python2 ~/.scripts/gmail.py 2>/dev/null 2>/dev/null`
	
	xsetroot -name "CPU: ${freq}MHz${delim}$freemem MiB / $memory MiB${delim}$battery${delim}$ctime${delim}${weather}${delim}Mail: ${mail}"
	sleep 5;
done

It's actually just checking the RSS feed, shows up on Gmail's activity as Atom Feed access type. For me, it's much lighter than conky and has all information I want available easily.

Offline

#6 2012-07-26 14:23:40

ibrunton
Member
From: Canada
Registered: 2011-05-05
Posts: 270

Re: gmail script

I have a similar python script (here) that I run from a bash loop (here (note that it checks 2 different gmail accounts)) so it only queries Gmail every 5 minutes.  I don't really need it updated more often than that.

Offline

#7 2012-07-26 16:18:54

hesse
Member
Registered: 2011-12-08
Posts: 88

Re: gmail script

I modified the gmail script to loop with 5 min sleep and write the value to file (useful with dwm statusbar written in C):

#!/usr/bin/env python2

import os
import time
import string

username = "foo"
password = "faa"
outfile = "/home/herman/log/nmails"
com = 'curl -s -u "{}:{}" https://mail.google.com/mail/feed/atom'.format(username, password)
interval = 300 # seconds

while True:
    temp = os.popen(com)
    msg = temp.read()
    index = string.find(msg, "<fullcount>")
    index2 = string.find(msg, "</fullcount>")
    fc = int(msg[index + 11 : index2])
    f = open(outfile, "w")
    f.write(str(fc))
    f.close()
    time.sleep(interval);

Last edited by hesse (2012-07-26 22:10:18)

Offline

#8 2012-07-26 16:30:47

Paaskehare
Member
From: Denmark
Registered: 2008-11-09
Posts: 59
Website

Re: gmail script

I just made a python 3 compatible script like the ones above, except it doesnt use wget:

#!/usr/bin/env python
# encoding: utf-8

from urllib.request import FancyURLopener

username = 'username'
password  = 'superlongandhardpassword'

url = 'https://%s:%s@mail.google.com/mail/feed/atom' % (username, password)

opener = FancyURLopener()
page = opener.open(url)

contents = page.read().decode('utf-8')


ifrom = contents.index('<fullcount>') + 11
ito   = contents.index('</fullcount>')

unread = contents[ifrom:ito]

print(unread)

Link to gist here

Last edited by Paaskehare (2012-07-30 21:40:42)


im.ole

Offline

#9 2012-07-27 17:50:40

simon.swe
Member
From: sverige
Registered: 2012-02-26
Posts: 148
Website

Re: gmail script

tnx everyone! this is great big_smile:D:D

Offline

#10 2012-07-27 18:58:59

simon.swe
Member
From: sverige
Registered: 2012-02-26
Posts: 148
Website

Re: gmail script

found a small application, html2text So Im doing like this at the moment,
wget -O mail.html - 'https://user:pass@mail.google.com/mail/feed/atom'
html2text mail.html|awk '/New/ {print $7}'

Offline

#11 2012-07-27 19:01:07

simon.swe
Member
From: sverige
Registered: 2012-02-26
Posts: 148
Website

Re: gmail script

Paaskehare wrote:

I just made a python 3 compatible script like the ones above, except it doesnt use wget:

#!/usr/bin/env python
# encoding: utf-8

from urllib.request import FancyURLopener

username = 'username'
password  = 'superlongandhardpassword'

url = 'https://%s:%s@mail.google.com/mail/feed/atom' % (username, password)

opener = FancyURLopener()
page = opener.open(url)

contents = page.read().decode('utf-8')


ifrom = contents.index('<fullcount>') + 11
ito   = contents.index('</fullcount>')

unread = contents[ifrom:ito]

print(unread)


works very well! tnx!

Offline

#12 2012-07-30 21:40:57

Paaskehare
Member
From: Denmark
Registered: 2008-11-09
Posts: 59
Website

Re: gmail script

No problem, glad you found it useful :-)


im.ole

Offline

#13 2012-08-09 23:36:16

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: gmail script

Onyros wrote:

It's actually just checking the RSS feed, shows up on Gmail's activity as Atom Feed access type. For me, it's much lighter than conky and has all information I want available easily.

https://bbs.archlinux.org/viewtopic.php?id=30155&p=3

Offline

Board footer

Powered by FluxBB