Author Topic: New curl and python method to get gmail output  (Read 732 times)

VastOne

  •      v-ger
  • Posts: 3861
    • View Profile
    • VSIDO Community
New curl and python method to get gmail output
« on: November 15, 2016, 02:23:50 AM »
Over on this thread we have been discussing gmail conky script changes. 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)

Code: [Select]
vastone@vsido:~$ curl -u gmail_login:gmail_password --silent "https://mail.google.com/mail/feed/atom" | ./gmail.py
gmail.py (must be executable)

Code: [Select]
#!/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
VSIDO      VSIDO Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO

VastOne

  •      v-ger
  • Posts: 3861
    • View Profile
    • VSIDO Community
Re: New curl and python method to get gmail output
« Reply #1 on: November 15, 2016, 02:26:47 AM »
This is the terminal output


 
VSIDO      VSIDO Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO

PackRat

  • Posts: 1816
    • View Profile
Re: New curl and python method to get gmail output
« Reply #2 on: November 15, 2016, 04:01:29 AM »
I get this error -

Code: [Select]
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:

Code: [Select]
${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.
I am tired of talk that comes to nothing.
-- Chief Joseph

...the sun, the darkness, the winds are all listening to what we have to say.
-- Geronimo

jedi

  • DEFCON 1
  • Posts: 1495
  • Not all those who wander are lost.
    • View Profile
    • NixNut.com
Re: New curl and python method to get gmail output
« Reply #3 on: November 15, 2016, 06:35:46 AM »
Works good for me...
Forum Netiquette

"No matter how smart you are you can never convince someone stupid that they are stupid."  Anonymous

PackRat

  • Posts: 1816
    • View Profile
Re: New curl and python method to get gmail output
« Reply #4 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.
I am tired of talk that comes to nothing.
-- Chief Joseph

...the sun, the darkness, the winds are all listening to what we have to say.
-- Geronimo

hakerdefo

  • Posts: 494
    • View Profile
    • Looking at Linux through the Windows of Life
Re: New curl and python method to get gmail output
« Reply #5 on: November 15, 2016, 05:41:59 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!!!
You Can't Always Git What You Want