Pregunta

I'm having quite some trouble doing that :
I'm using Conky on my Archlinux distro and coded a quick script in python to check if I have a new mail in my gmail. In my conkyrc this script executes every 5 minutes and returns a number of mails (0 if I don't have any). Works fine.

What I wanted to do is :
If the number of mail is > 0 then display a notification (a gnome-shell notification). The only problem I have now is that if I have unread mails (for example 4 mails unread), each 5 minutes, there will be a NEW notification saying that I have 4 mails unread. What I would like to do is checking if there is already a notification so that I don't have to display it again... Does anyone know how to solve that kind of problem ?

Here is my code :

#!/usr/bin/python

from gi.repository import Notify
from urllib.request import FancyURLopener

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)

if unread != "0" :
    Notify.init ("New Mail")
    Hello=Notify.Notification.new ("New mail","You have "+unread+" new mail(s)","/usr/share/icons/Faenza/actions/96/mail-forward.png")
    Hello.show ()

I must precise that I'm quite new to python. Thanks in advance if anyone got a solution :)

¿Fue útil?

Solución

One possible solution is serialise the value of unread into a file.

Then change your check from if the number of mails is greater than 0, to if the number of mails is greater than zero or different from the last serialised count from file.

An extension of this is to also serialise the time when a notification is run along with the mail count, this way you extend the check to show the repeat notification (showing 4 emails twice, not every 5 minutes say, but every 3 hours).

So your original check was if unread != "0" : it would become something like if unread != "0" && unread != serialisedvalue: . In the show repeat notification time threshold case it becomes

if unread != "0":
    if ((datetime.now() - serialiseddate) < threshold) :
        if unread != serialisedvalue:

Where threshold = 3600*3 is for 3 hours.

Sample code for serialising and deserialising is below

#Serialising
try:
    # This will create a new file or **overwrite an existing file**.
    f = open("mailcount.txt", "w")
    try:
        f.write(unread + "\n") # Write a string to a file
        f.write(datetime.now().strftime('%b %d %Y %I:%M%p'))
    finally:
        f.close()
except IOError:
    pass

#Deserialising 
try:
    f = open("mailcount.txt", "r")
    try:
        # Read the entire contents of a file at once.
        serialisedvalue = f.readline()
        serialseddate = datetime.strptime(f.readline(), '%b %d %Y %I:%M%p')
    finally:
        f.close()
except IOError:
    pass

Another possible solution would be to get the current active notification count somehow and add that to your condition, though I couldn't find a method for doing that using the API that Notify uses.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top