-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.py
87 lines (75 loc) · 3.03 KB
/
messages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
from googleapiclient import discovery
from googleapiclient.http import BatchHttpRequest
from oauth2client import client
from rq import Queue
from rq.job import Job
#from worker import conn
import httplib2
import re
import json
# Find the stack on which we want to store the database connection.
# Starting with Flask 0.9, the _app_ctx_stack is the correct one,
# before that we need to use the _request_ctx_stack.
#q = Queue(connection=conn)
def get_messages(pagenr,contact,credentials):
messages=[]
gmail_credentials = client.OAuth2Credentials.from_json(credentials)
http = gmail_credentials.authorize(httplib2.Http())
# Build the Gmail service from discovery
gmail_service = discovery.build('gmail', 'v1', http=http)
query = "\"to:'" + contact + "' AND from:me \" OR from:'" + contact + "'"
message_ids = gmail_service.users().messages().list(userId='me',
maxResults=10, labelIds='INBOX', q=query).execute()
def mailscallbackfunc(result, results, moreresults):
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if 'UNREAD' in results['labelIds']:
Unread = 'true'
else:
Unread = 'false'
for header in results['payload']['headers']:
if header['name'] == 'Date':
Date = header['value']
if header['name'] == 'From':
From = header['value']
if From == "Anders Damsgaard <[email protected]>": #SKAL ÆNDRES
Sent = True
else:
Sent = False
if header['name'] == 'Subject':
Subject = header['value']
Contact = {
'messageId': results['id'],
'date': Date,
'subject': Subject,
'snippet': results['snippet'],
'unread': Unread,
'sent': Sent
}
messages.append(Contact)
#for msg_id in message_ids['messages']:
# batchContacts.add(gmail_service.users().messages().get(userId='me',
# id=msg_id['id'], format='metadata',
# metadataHeaders=['from', 'date']))
#batchContacts.execute()
batchMails = BatchHttpRequest(callback=mailscallbackfunc)
for msg_id in message_ids['messages']:
batchMails.add(gmail_service.users().messages().get(userId='me',
id=msg_id['id'], format='metadata',
metadataHeaders=['from', 'date', 'subject']))
batchMails.execute()
#def mailscallbackfunc(result, results, moreresults):
# if 'UNREAD' in results['labelIds']:
# Unread = 'true'
# else:
# Unread = 'false'
#
#batchMails = BatchHttpRequest(callback=mailscallbackfunc)
#for contact in session['seen']:
# batchContacts.add(gmail_service.users().messages().get(userId='me',
# id=msg_id['id'], format='metadata',
# metadataHeaders=['from', 'date']))
#batchMails.execute()
return messages