It is currently Tue Sep 30, 2014 12:54 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Check Gmail in Tonido Shell
PostPosted: Thu Apr 28, 2011 1:19 pm 
Tonido MVP
Tonido MVP
User avatar
Joined: Sun Sep 05, 2010 6:21 pm
Posts: 504

Here is a little script that will tell you how many emails currently in your inbox or other folder you specify. It only gives you a summary, but I thought it was still niffty anyways.
Attachment:
File comment: Gmail script in action
gmail.jpg
gmail.jpg [ 22.47 KiB | Viewed 1260 times ]



This only works on the Plug!
This script taps into unix functionality to timeout the script. I am new to Python and do not know how to do similar functionality in windows yet.

This also works on any linux system from command line with python 2.5+ I believe. It might not work on python 3.0+.

This is provided as is, and I am not responsible if your plug explodes.

Instructions:
  • Download the script and save as gmail.py
  • change the username and password field to your own.
  • place script on plug
  • chmod +x gmail.py
  • launch script by specifying folder to check and max emails to list
  • example usage: ./gmail.py inbox 10

Code:
#!/usr/bin/python
import sys
import socket
import urllib
import errno
import os
import signal
from xml.dom import minidom
from functools import wraps

username = "user"
password = "pass"
label    = str(sys.argv[1])
limit    = int(sys.argv[2])

class TimeoutError(Exception):
    pass

def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
    def decorator(func):
        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

        @wraps(func)
        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, _handle_timeout)
            signal.alarm(seconds)
            try:
                result = func(*args, **kwargs)
            finally:
                signal.alarm(0)
            return result

        return wrapper

    return decorator

GMAIL_URL = 'https://'+urllib.quote(username)+':'+urllib.quote(password)+'@mail.google.com/mail/feed/atom/'+urllib.quote(label)+'/'

def utfEncode(string):
    return unicode(string).encode('utf-8')

def printEmails(emails):
    print '%s: %s\n' %(utfEncode(emails['labelname']),utfEncode(emails['labelcount']))
    for email in emails['emails']:
        print 'Title:   %s'      %utfEncode(email['title'])
        print 'Sender:  %s <%s>' %(utfEncode(email['sender']),utfEncode(email['address']))
        print 'Summary: %s\n'    %utfEncode(email['summary'])

@timeout(10)
def getGmail(label,maxCount):
    url = GMAIL_URL
    try:
        xml = urllib.urlopen(url)
        dom = minidom.parse(xml)
    except:
        print '\nError: Could not get emails\n'
        exit()
    mail = []
    for node in dom.getElementsByTagName('entry'):
        if maxCount == 0:
          break
        author = node.getElementsByTagName('author')[0]
        mail.append({
            'title'  : node.getElementsByTagName('title')[0].firstChild.data,
            'summary': node.getElementsByTagName('summary')[0].firstChild.data,
            'sender' : author.getElementsByTagName('name')[0].firstChild.data,
            'address': author.getElementsByTagName('email')[0].firstChild.data
        })
        maxCount -= 1
    count = dom.getElementsByTagName('fullcount')[0].firstChild.data
    return {
        'emails'    : mail,
        'labelname' : label,
        'labelcount': count
    }

printEmails(getGmail(label,limit))

_________________
My Tonido Plugins:


Offline
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours [ DST ]


 Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: