VSIDO Community

VSIDO Support => Scripts and How To's => Scripts => Topic started by: VastOne on November 15, 2016, 02:23:50 AM

Title: New curl and python method to get gmail output
Post by: VastOne on November 15, 2016, 02:23:50 AM
Over on this thread we have been discussing gmail conky script changes. (http://vsido.org/index.php?topic=1222.msg13763#msg13763) And so far it is anything but successful even though we are getting great help from so many

I have found an interesting alternative that uses curl and works flawlessly from terminal... if that's all you wanted

The ideal thing would be to get this to format to a conky output

(change location of gmail.py if you put it somewhere other than /home/your_username)

vastone@vsido:~$ curl -u gmail_login:gmail_password --silent "https://mail.google.com/mail/feed/atom" | ./gmail.py

gmail.py (must be executable)

#!/usr/bin/env python3

import sys
import xml.etree.ElementTree as ET

colors = {
             'RED' : '\033[31m',      'LRED' : '\033[91m',
           'GREEN' : '\033[32m',    'LGREEN' : '\033[92m',
          'YELLOW' : '\033[93m',   'LYELLOW' : '\033[93m',
            'BLUE' : '\033[34m',     'LBLUE' : '\033[94m',         
         'MAGENTA' : '\033[35m',  'LMAGENTA' : '\033[95m',
            'CYAN' : '\033[36m',     'LCYAN' : '\033[96m',
            'GREY' : '\033[37m',     'WHITE' : '\033[97m',
            'BOLD' : '\033[1m',
            'ENDC' : '\033[0m'
         }

def print_data( tag, flist, template ):
    values = {}
    for f in flist:
        node = tag.find( '/'.join( xmlns+x for x in f.split('/') ) )
        values[f.replace('/','_')] = node.text
        values.update( colors )
    print( template.format( **values ) )

# -----------------

fields = ( 'title', 'tagline', 'fullcount' )
e_fields = ( 'title', 'summary', 'author/name', 'author/email', 'issued' )

template = """{BOLD}{MAGENTA}{title}{ENDC}
{GREEN}{tagline} ({BOLD}{fullcount}{ENDC}{GREEN}){ENDC}"""

e_template = """
{LCYAN}{BOLD}{author_name}{ENDC}   [{issued}]
{LGREEN}{title}{ENDC}
"""



data = ''.join( sys.stdin.readlines() )
xroot = ET.fromstring( data )
xmlns = xroot.tag[:xroot.tag.find('}')+1]

print_data( xroot, fields, template )

for e in xroot.findall( xmlns+'entry' ):
    print_data( e, e_fields, e_template )


Let me know what y'all think and if successful in getting it to conky
Title: Re: New curl and python method to get gmail output
Post by: VastOne on November 15, 2016, 02:26:47 AM
This is the terminal output


  (http://www.zimagez.com/miniature/screenshot2016-11-1420-25-28.php)
(http://www.zimagez.com/zimage/screenshot2016-11-1420-25-28.php)
Title: Re: New curl and python method to get gmail output
Post by: PackRat on November 15, 2016, 04:01:29 AM
I get this error -

Traceback (most recent call last):
  File "/home/doug/bin/cgmail.py", line 45, in <module>
    print_data( xroot, fields, template )
  File "/home/doug/bin/cgmail.py", line 22, in print_data
    values[f.replace('/','_')] = node.text
AttributeError: 'NoneType' object has no attribute 'text'


I suspect I'm missing a python package that debian brings in automatically, but Void does not consider a hard dependency (the upside of Debian's dependency hell).

Does that command not run in conky as:

${excepi 600 <command>}     # change 600 to however long you want between mail checks

You may want to fold the output, or have a real wide conky window.
Title: Re: New curl and python method to get gmail output
Post by: jedi on November 15, 2016, 06:35:46 AM
Works good for me...
Title: Re: New curl and python method to get gmail output
Post by: PackRat on November 15, 2016, 01:42:07 PM
Google admin just sent me an email - apparently I need to configure Google to allow access from unsecure apps for this to work; so I stop looking through the python3 packages available to see if I'm missing something.
Title: Re: New curl and python method to get gmail output
Post by: hakerdefo on November 15, 2016, 05:41:59 PM
Quote from: PackRat on November 15, 2016, 01:42:07 PM
Google admin just sent me an email - apparently I need to configure Google to allow access from unsecure apps for this to work; so I stop looking through the python3 packages available to see if I'm missing something.
Hi there RatMan,
Perhaps you missed my last post,

http://vsido.org/index.php?topic=1222.msg13801#msg13801

As stated there a better option in this scenario is to generate and use app password than allowing, what google considers unsecure, apps the access to your account.

Cheers!!!