-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·101 lines (84 loc) · 2.9 KB
/
run.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
# Python 3.8.0
import logging
import traceback
from src.argmenu import ArgMenu
from src.configs import Config
from src.mymail import MyMail
from src.archive import Archive
# -------------------------------------------------
#
# Utility to read email from Gmail Using Python
#
# ------------------------------------------------
def read_email_from_gmail():
try:
#Import arguments
argmenu = ArgMenu()
args = argmenu.get_args()
credentials = Config(args.cred)
#Initialize logging
logging.basicConfig(format='%(asctime)-15s %(levelname)s %(message)s')
if args.verbose == True:
logging.getLogger().setLevel(logging.DEBUG)
else:
logging.getLogger().setLevel(logging.INFO)
# print(args.delete)
# print(args.verbose)
# exit(1)
#Initialize mail
if args.delete == True:
mymail = MyMail(
mail_account=credentials.get_data("mail"),
password=credentials.get_data("password"),
apiserver=credentials.get_data("apiserver"),
port=credentials.get_data("port"),
readonly=False
)
else:
mymail = MyMail(
mail_account=credentials.get_data("mail"),
password=credentials.get_data("password"),
apiserver=credentials.get_data("apiserver"),
port=credentials.get_data("port")
)
# mymail.get_folders()
#
# exit(0)
mails = mymail.search_mail(args.search)
#Initialize archive
archive = Archive(basedir=args.output_dir)
#Get all mails and archive
if len(mails) == 0:
logging.info("No mail returned")
exit(0)
else:
logging.info(f"Progress {len(mails)}")
index = []
for id in mails:
index.append(int(str(id, 'UTF-8')))
index=sorted(index)
if index[0] < index[-1]:
index = sorted(index,reverse=True)
count=0
for id in index:
count = count + 1
if args.mails is not None:
if count == args.mails + 1 :
exit(0)
else:
logging.info(f"{args.mails - count}/{args.mails} Messages to process.")
else:
logging.info(f"{len(index) - count}/{len(index)} Messages to process.")
b = str(id).encode('utf-8')
mail = mymail.get_mails(b)
if archive.archive_mail(mail) == True:
if args.delete == True:
mymail.delete_mail(id=b, msgid=mail['Message-ID'])
else:
logging.error(f"Mail {b} not archive by previus ERROR.")
mymail.close()
except Exception as e:
traceback.print_exc()
print(str(e))
if __name__ == "__main__":
read_email_from_gmail()