Last active
August 29, 2015 14:10
-
-
Save kidpixo/58d0df6dd80d4e3172b6 to your computer and use it in GitHub Desktop.
tmux session description to use with tmuxinator example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/python | |
# see : https://developers.google.com/gmail/api/quickstart/quickstart-python | |
# google API doc : https://developers.google.com/gmail/api/v1/reference/users/messages#resource | |
import httplib2 | |
import sys | |
from apiclient.discovery import build | |
from oauth2client.client import flow_from_clientsecrets | |
from oauth2client.file import Storage | |
from oauth2client.tools import run | |
# Path to the client_secret.json file downloaded from the Developer Console | |
CLIENT_SECRET_FILE = sys.path[0]+'/client_secret_1052895885061-bpreeljt722nk6cings05asfv63aj882.apps.googleusercontent.com.json' | |
# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes | |
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly' | |
# Location of the credentials storage file | |
STORAGE = Storage('gmail.storage') | |
# Start the OAuth flow to retrieve credentials | |
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE) | |
http = httplib2.Http() | |
# Try to retrieve credentials from storage or run the flow to generate them | |
credentials = STORAGE.get() | |
if credentials is None or credentials.invalid: | |
credentials = run(flow, STORAGE, http=http) | |
# Authorize the httplib2.Http object with our credentials | |
http = credentials.authorize(http) | |
# Build the Gmail service from discovery | |
gmail_service = build('gmail', 'v1', http=http) | |
############################################################################## | |
############################################################################## | |
# doc : https://developers.google.com/gmail/api/v1/reference/#Users | |
# # Retrieve a page of threads | |
# threads = gmail_service.users().threads().list(userId='me').execute() | |
# # Print ID for each thread | |
# if threads['threads']: | |
# for thread in threads['threads']: | |
# print 'Thread ID: %s' % (thread['id']) | |
# | |
# | |
# labels = gmail_service.users().labels().list(userId = 'kidpixo@gmail.com').execute() | |
# | |
# ... | |
# {u'id': u'Label_60', | |
# u'labelListVisibility': u'labelShow', | |
# u'messageListVisibility': u'show', | |
# u'name': u'gtd/to_do', | |
# u'type': u'user'}, | |
# ... | |
# | |
# label_single = gmail_service.users().labels().get(userId = 'kidpixo@gmail.com', id = 'Label_60').execute() | |
# | |
# {u'id': u'Label_60', | |
# u'labelListVisibility': u'labelShow', | |
# u'messageListVisibility': u'show', | |
# u'messagesTotal': 41, | |
# u'messagesUnread': 10, | |
# u'name': u'gtd/to_do', | |
# u'threadsTotal': 18, | |
# u'threadsUnread': 3, | |
# u'type': u'user'} | |
# | |
# # single message | |
# | |
# [ '{%s} = {%s} ' % (i['name'],i['value']) for i in gmail_service.users().messages().get(userId = 'me',id = ('14b2bda41a080c41'),format = 'metadata').execute()['payload']['headers'] ] | |
# | |
# [u'{MIME-Version} = {1.0} ', | |
# u'{Received} = {by 10.216.209.70 with HTTP; Tue, 27 Jan 2015 06:44:58 -0800 (PST)} ', | |
# u'{In-Reply-To} = {<CA+eesaBk50=xqJCLxnSe4zPrXnVfDiqWnhvQwGb_XBvqYpt9CA@mail.gmail.com>} ', | |
# u'{References} = {<CA+eesaBk50=xqJCLxnSe4zPrXnVfDiqWnhvQwGb_XBvqYpt9CA@mail.gmail.com>} ', | |
# u'{Date} = {Tue, 27 Jan 2015 15:44:58 +0100} ', | |
# u'{Delivered-To} = {kidpixo@gmail.com} ', | |
# u'{Message-ID} = {<CA+Hhufa7bhaz1o=UDnoK-rxqW5qO0-GUL02yw5SsZYZM1g0sBw@mail.gmail.com>} ', | |
# u'{Subject} = {Re: Fliesen} ', | |
# u'{From} = {"Mario D\'Amore" <kidpixo@gmail.com>} ', | |
# u'{To} = {Jasmin Girndt <jasmin.girndt@gmail.com>} ', | |
# u'{Content-Type} = {multipart/alternative; boundary=f46d04428cc6902ae2050da34c9e} '] | |
# | |
# single_message['payload']['headers'][7]['value'] | |
# | |
# | |
# metadataHeaders=['Date','From','Subject'] | |
# single_message = gmail_service.users().messages().get(userId = 'me',id = '14b2bda41a080c41',format = 'metadata',metadataHeaders=metadataHeaders ).execute() | |
# | |
# [single['value'] for single in single_message['payload']['headers']] | |
# [u'Tue, 27 Jan 2015 15:44:58 +0100', | |
# u'Re: Fliesen', | |
# u'"Mario D\'Amore" <kidpixo@gmail.com>'] | |
############################################################################## | |
# retrieve messagges with query | |
messages_to_do = gmail_service.users().messages().list(userId='me',q='label:gtd-to_do').execute() | |
# if messages_to_do['messages']: | |
# for message in messages_to_do['messages']: | |
# print 'Thread ID: %s' % (message['id']) | |
############################################################################## | |
##### unique elements | |
i = [mess['id'] for mess in messages_to_do['messages']] | |
t = [mess['threadId'] for mess in messages_to_do['messages']] | |
# # it seems to be te first element. thread id the message id of the oldest element! | |
# 14b2b892765bf211 : [u'14b2bda41a080c41', u'14b2b892765bf211'] | |
# | |
# for msg_id in [u'14b2bda41a080c41', u'14b2b892765bf211']: | |
# print filter(lambda x: x['id'] == msg_id, batch_messages)[0]['payload']['headers'] | |
# | |
# u'Tue, 27 Jan 2015 15:44:58 +0100' | |
# u'Tue, 27 Jan 2015 14:16:23 +0100' | |
# | |
# # all the index | |
# for k in set(t): | |
# print k,' : ', [i[l] for l in [o for o, x in enumerate(t) if x == k]] | |
# # only the first : it could be better than this! | |
# [ [i[l] for l in [o for o, x in enumerate(t) if x == k]][0] for k in set(t)] | |
# just the first | |
messages_to_do_unique_last = [i[t.index(k)] for k in set(t)] | |
############################################################################## | |
# batch request all the messages | |
from apiclient.http import BatchHttpRequest | |
batch = BatchHttpRequest() | |
global batch_messages | |
batch_messages = [] | |
def mycallbackfunc(request_id, response, exception): | |
if exception is None: | |
# print response['payload']['headers'] | |
batch_messages.append(response) | |
pass | |
else: | |
print 'Exception : %s' % exception | |
pass | |
metadataHeaders=['Date','From','Subject'] | |
# for message in messages_to_do['messages']: | |
# print message['id'] | |
# batch.add(gmail_service.users().messages().get(userId='me', id = message['id'],format = 'metadata',metadataHeaders=metadataHeaders), callback=mycallbackfunc) | |
for message in messages_to_do_unique_last: | |
# print message | |
batch.add(gmail_service.users().messages().get(userId='me', id = message,format = 'metadata',metadataHeaders=metadataHeaders), callback=mycallbackfunc) | |
batch.execute() | |
# [single['value'] for single in batch_messages[0]['payload']['headers']] | |
# [u'Tue, 27 Jan 2015 15:44:58 +0100', # convert datetime | |
# u'Re: Fliesen', # ok | |
# u'"Mario D\'Amore" <kidpixo@gmail.com>'] # take the name out or only the email if no name | |
# | |
# print [mess['payload']['headers'] for mess in batch_messages] | |
from dateutil import parser | |
import re | |
email_at_pattern = re.compile('@') | |
# cicle thorugh all the messages | |
for tmp in batch_messages: | |
message = tmp['payload']['headers'] | |
# sort the field! | |
for meta in metadataHeaders: | |
# extract the actual field ordere like in metadataHeaders | |
value = filter(lambda x: x['name'] == meta, message)[0]['value'] | |
# start checking for different values and different operations | |
if meta == 'Date': | |
print parser.parse(value).strftime("%Y-%m-%d").strip(), | |
if meta == 'From': | |
not_address_part = filter(lambda x: email_at_pattern.search(x) == None, value.split('<'))[0] | |
if len(not_address_part) == 0: | |
print filter(lambda x: email_at_pattern.search(x) != None, value.split('<'))[0].strip('>').strip(), | |
else : | |
print not_address_part.strip(' "').strip(), | |
#print ',', | |
if meta == 'Subject': | |
print "'%s'" % (value.strip().encode('utf-8')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment