Coverage for src/ensae_teaching_cs/automation_students/mail_helper.py: 9%
43 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-04-28 06:23 +0200
« prev ^ index » next coverage.py v7.1.0, created at 2023-04-28 06:23 +0200
1"""
2@file
3@brief Some automation helpers to grab mails from student about project.
4"""
6from pyquickhelper.loghelper import noLOG
7from pymmails import MailBoxImap
10def grab_addresses(mailbox, subfolder, date, no_domain=False, max_dest=5, names=False, fLOG=noLOG):
11 """
12 Looks for some emails in a mail box
13 from specific emails or sent to specific emails.
15 @param mailbox MailBoxImap object (we assume you are logged in)
16 @param date date (grab emails since ..., example ``1-Oct-2014``)
17 @param subfolder folder of the mailbox to look into
18 @param no_domain remove domain when searching for emails
19 @param max_dest number of receivers to have a valid mail
20 @param names if true, return suggestions for names for each mail
21 @param fLOG logging function
22 @return list of emails or tuple(list of emails, dictionary(email: name)) if names is True
24 .. exref::
25 :title: Collect email addresses from mails in an inbox folder)
26 :tag: Automation
28 ::
30 from ensae_teaching_cs.automation_students import grab_addresses
31 from pymmails import MailBoxImap
33 user = "xavier.dupre"
34 pwd = "***"
35 server = "imap.gmail.com"
36 mailfolder = ["ensae/ENSAE_2016", "ensae/ensae_interro_2015"]
37 date = "1-Dec-2015"
39 box = MailBoxImap(user, pwd, server, ssl=True, fLOG=fLOG)
40 box.login()
41 emails = grab_addresses(box, mailfolder, date, fLOG=fLOG)
42 box.logout()
43 """
44 emails = mailbox.enumerate_mails_in_folder(
45 subfolder, date=date, body=False)
46 res = []
47 suggestions = {}
48 for i, mail in enumerate(emails):
49 if i % 25 == 0:
50 if len(suggestions) == 0:
51 fLOG(f"[grab_addresses] {i} collected {len(res)}")
52 else:
53 fLOG("[grab_addresses] {0} collected {1} names {2}".format(
54 i, len(res), len(suggestions)))
55 tos = mail.get_to()
56 cc = mail.get_to(cc=True)
57 if cc:
58 tos.extend(cc)
59 if max_dest > 0 and len(tos) <= max_dest:
60 tos = [(m[1].split('@')[0] if no_domain else m[1])
61 for m in tos if m and m[1]]
62 res.extend(tos)
63 frs = [mail.get_from()]
64 frs = [(m[1].split('@')[0] if no_domain else m[1])
65 for m in frs if m and m[1]]
66 if names:
67 identity = mail.get_name()
68 if identity is not None:
69 for m in frs:
70 if m not in suggestions:
71 suggestions[m] = {identity}
72 elif identity not in suggestions[m]:
73 suggestions[m].add(identity)
74 res.extend(frs)
75 res = list(sorted(set(res)))
76 return (res, suggestions) if names else res
79def extract_students_mail_and_name_from_gmail(user=None, pwd=None, server="imap.gmail.com",
80 mailfolder=["ensae/actuariat"],
81 date="1-Jan-2016", fLOG=noLOG):
82 """
83 Extracts mails and names from a mail box.
85 @param user user of the gmail inbox
86 @param pwd password of the gmail inbox
87 @param server gmail server, it should be ``"imap.gmail.com"``,
88 it works with others mail servers using the *IMAP* protocol
89 @param mailfolder folder in your inbox to look into,
90 there can be several
91 @param date when to start looking (do not change the format,
92 look at the default value)
93 @param fLOG logging function
94 @return list of dictionary ``[{"name": ..., "mail": ...}]``
95 """
96 box = MailBoxImap(user, pwd, server, ssl=True, fLOG=fLOG)
97 box.login()
98 emails, suggestions = grab_addresses(
99 box, mailfolder, date, names=True, fLOG=fLOG)
100 box.logout()
102 rows = []
103 for mail in emails:
104 el = {"mail": mail}
105 if mail in suggestions:
106 el["name"] = ";".join(sorted(suggestions[mail]))
107 rows.append(el)
108 return rows