Sunday, February 10, 2013

Check GMail script for Conky on Crunchbang Waldorf

I'm trying out Crunchbang Waldorf. I found a script for Crunchbang Statler (the previous version) that displayed the number of my new GMail messages in my Conky desktop display, but it didn't work with Waldorf. I was looking around for a simple and efficient script and eventually found one that works on the Arch Linux forum.


 I modified it a little not to show the number of emails on the server, just the number of new emails.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys, imaplib

port = 993
server = 'imap.gmail.com'

username = 'name@gmail.com'
passwd = 'password'

imap_server = imaplib.IMAP4_SSL(server, port)
try:
    imap_server.login(username, passwd)
except:
    print('?? new')
    sys.exit( 1 )

typ, data = imap_server.select ('Inbox', True)
if typ == 'OK':
    total = int(data[0])
    typ, data = imap_server.search (None, 'SEEN')
    if typ == 'OK':
        seen = len(data[0].split())
        print('{} new'.format(total - seen))

if typ != 'OK':
    print('?? new')

imap_server.logout()
 Credit: rockin turtle.

2 comments:

  1. I use multiple gmail accounts, (university, work, personal) I add some lines to your script for use one script with multiple calls from conky

    #!/usr/bin/python

    import sys, getopt, imaplib

    def main(argv):
    usuario = ''
    password = ''
    try:
    opts, args = getopt.getopt(argv,"hu:p:",["usuario=","password="])
    except getopt.GetoptError:
    print 'test.py -u -p '
    sys.exit(2)
    for opt, arg in opts:
    if opt == '-h':
    print 'test.py -u -p '
    sys.exit()
    elif opt in ("-u", "--usuario"):
    usuario = arg
    elif opt in ("-p", "--password"):
    password = arg
    #print 'Input file is "', usuario,'"'
    #print 'Output file is "', password,'"'


    port = 993
    server = 'imap.gmail.com'

    username = usuario
    passwd = password

    imap_server = imaplib.IMAP4_SSL(server, port)
    try:
    imap_server.login(username, passwd)
    except:
    print('?? new')
    sys.exit( 1 )
    #imap_server.login(username,passwd);

    typ, data = imap_server.select ('Inbox', True)
    if typ == 'OK':
    total = int(data[0])
    typ, data = imap_server.search (None, 'SEEN')
    if typ == 'OK':
    seen = len(data[0].split())
    result = total - seen
    resp = ''
    if result >= 100:
    resp = '+99'
    if result < 100:
    resp = '0'+str(result)
    if result < 10:
    resp = '00'+str(result)
    print('{}'.format(resp))

    if typ != 'OK':
    print('?? new')

    imap_server.logout()

    if __name__ == "__main__":
    main(sys.argv[1:])


    in the conky script I use

    ${execpi 10 ./scripts/gmail/test.py -u user@gmail.com -p "password"}


    however... Good Script :)
    thanks a lot!!

    ReplyDelete
  2. Thanks go to rockin turtle really, as s/he wrote the original script, but thanks for your additions and the comment.

    ReplyDelete