-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmail.py
executable file
·106 lines (82 loc) · 3.05 KB
/
gmail.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
# -*- coding: utf-8 -*-
import imaplib
import os
import sys
# Private centralization of notification
try:
import maya
except ImportError:
pass
class GmailBackup():
def __init__(self, l, p, backup_dir):
self.l = l
self.p = p
self.backup_dir = backup_dir
def connect(self):
self.m = imaplib.IMAP4_SSL("imap.gmail.com")
try:
self.m.login(self.l, self.p)
except :
print >> sys.stderr, "Something wrong ? Bad logins ?"
exit()
# Sélection du dossier
# print self.m.list("All")
if(self.m.select("[Gmail]/Tous les messages", readonly=True)[0] == 'NO'):
self.m.select("[Gmail]/All Mail", readonly=True)
def save(self, search='ALL'):
# Recherche de tous les messages
#_, items = self.m.search(None, search) # ALL # (HEADER Subject "Yanonix")
_, items = self.m.uid('search', None, 'ALL')
l = items[0]
items = l.split()
count = len(items)
i = 1
nb_new = 0
print("Mails quantity : %d" % count)
for mid in items:
name = "%s/%s.mail" % (self.backup_dir, mid)
print("(%d%%) N°%d\t UID°%s" % (i*100/count, i, mid)),
if not os.path.isfile(name):
print(" ! New !")
try:
resp, data = self.m.fetch('%d' % (i), "(RFC822)")
body = data[0][1]
f = open(name, "a")
f.write(body)
f.close()
nb_new += 1
except (KeyboardInterrupt, SystemExit):
if os.path.isfile(name):
os.remove(name)
print("\n\tFile %s deleted" % name)
exit()
except (TypeError):
print("\tType error")
if maya:
maya.fatal("Gmail", "download", "type", "Erreur de type lors d'un téléchargement")
print("\n"),
else:
print("\r"),
i += 1
if nb_new > 0:
print("%d new mails saved" % nb_new)
if maya:
maya.success("Gmail", "new", nb_new, "%d nouveaux mails récupérés" % nb_new)
try:
from __configuration__ import username, password, directory
print("GmailBackup (from __configuration__.py)\nConnection...")
GB = GmailBackup(username, password, directory)
except ImportError:
import argparse
print("GmailBackup (from command line)")
parser = argparse.ArgumentParser(description='Gmail backup system')
parser.add_argument('username', type=str, help='[email protected]')
parser.add_argument('password', type=str, help='Password for connection')
parser.add_argument('directory', type=str, help='Directory for emails backup')
args = parser.parse_args()
print("Connection...")
GB = GmailBackup(args.username, args.password, args.directory)
GB.connect()
print("Backup...")
GB.save()