participant class

This commit is contained in:
Jan Grewe 2019-08-20 17:32:50 +02:00
parent f67a4b0515
commit 34be02ccc7

View File

@ -17,25 +17,78 @@ import datetime
from IPython import embed from IPython import embed
import re import re
EMAIL_ACCOUNT = "bzigr02" EMAIL_ACCOUNT = "bzigr02"
# Use 'INBOX' to read inbox. Note that whatever folder is specified,
# after successfully running this script all emails in that folder
# will be marked as read.
EMAIL_FOLDER = "INBOX" EMAIL_FOLDER = "INBOX"
fields = ["Fist Name", "Last Name", "e-Mail", "Institution", "Phone", "Country", "City", "ZIP code", fields = ["Fist name", "Last name", "e-Mail", "Institution", "Phone", "Country", "City", "ZIP code", "Street",
"Career stage and fees", "Farewell event (Thu 20 Feb)", "Presentation type", "Fees", "Farewell event (Thu 20 Feb)", "Gwinner Award",
"Food preference", "LabTour (Fri 21 Feb)", "Messages"] "Food preferences", "LabTour (Fri 21 Feb)", "Messages"]
reg_fees = {'early': {"proMember": 95, "proNonMember":115, "studentMember": 55, "studentNonMember": 65},
'late': {"proMember": 115, "proNonMember":135, "studentMember": 65, "studentNonMember": 75}}
class Participation(object): class Participation(object):
_fields = ["Fist Name", "Last Name", "e-Mail", "Institution", "Phone", "Country", "City", "ZIP code",
"Career stage and fees", "Farewell event (Thu 20 Feb)", "Presentation type", def __init__(self, email_message):
"Food preference", "LabTour (Fri 21 Feb)", "Messages"] self._registration_date = ""
self._first_name = ""
pass self._last_name = ""
self._phone = ""
self._email = ""
self._student = False
self._member = False
self._farewell = False
self._food_vegi = False
self._food_vegan = False
self._food_gluten = False
self._food_normal = True
self._address_street = ""
self._address_city = ""
self._address_zip = ""
self._country = ""
self._institution = ""
self._fee = 0.0
self.__parse_message(email_message)
def __parse_message(self, message):
self._registration_date = message["Date"]
msg_body = message.get_payload()[0].as_string()
msg_body = re.sub(r'=20', ' ', msg_body)
msg_body = re.sub(r'=\n', '', msg_body)
msg_body = re.sub(r'=E2=82=AC', 'EUR', msg_body)
msg_body = re.sub(r'&', 'and', msg_body)
lines = msg_body.split('\n')
self.__parse_food(lines)
self.__parse_address(lines)
embed()
pass
def __parse_address(self, lines):
street = [s for s in lines if s.lower().startswith("street")]
self._address_street = street[0]
city = [s for s in lines if s.lower().startswith("city")]
self._address_city = city[0]
plz = [s for s in lines if s.lower().startswith("zip")]
self._address_zip = plz[0]
inst = [s for s in lines if s.lower().startswith("institution")]
self._institution = ', '.join(inst)
def __parse_food(self, lines):
food = [l for l in lines if "Food preference" in l]
if len(food) > 0:
prefs = food[0].lower()
self._food_normal = "no special" in prefs
self._food_vegan = "vegan" in prefs
self._food_vegi = "vegetarian" in prefs
self._food_gluten = "gluten" in prefs
@property
def registration_date(self):
return self._registration_date
def process_mailbox(M): def process_mailbox(M):
@ -44,7 +97,7 @@ def process_mailbox(M):
For the sake of this example, print some headers. For the sake of this example, print some headers.
""" """
rv, data = M.search(None, "ALL") rv, data = M.search(None, "UNSEEN")
if rv != 'OK': if rv != 'OK':
print("No messages found!") print("No messages found!")
return return
@ -57,7 +110,6 @@ def process_mailbox(M):
msg = email.message_from_bytes(data[0][1]) msg = email.message_from_bytes(data[0][1])
hdr = email.header.make_header(email.header.decode_header(msg['Subject'])) hdr = email.header.make_header(email.header.decode_header(msg['Subject']))
embed()
subject = str(hdr) subject = str(hdr)
print('Message %s: %s' % (num, subject)) print('Message %s: %s' % (num, subject))
print('Raw Date:', msg['Date']) print('Raw Date:', msg['Date'])
@ -71,53 +123,51 @@ def process_mailbox(M):
def process_message(msg_index): def process_message(msg_index):
rv, data = M.fetch(msg_index, '(RFC822)') rv, data = M.fetch(msg_index.encode('ascii'), '(RFC822)')
if rv != 'OK': if rv != 'OK':
print("ERROR getting message", num) print("ERROR getting message", rv)
return return
msg = email.message_from_bytes(data[0][1]) msg = email.message_from_bytes(data[0][1])
msg_body = msg.get_payload()[0].as_string() return Participation(msg)
msg_body = re.sub(r'=20', ' ', msg_body)
msg_body = re.sub(r'=\n', '', msg_body)
msg_body = re.sub(r'=E2=82=AC', 'EUR', msg_body)
msg_body = re.sub(r'&', 'and', msg_body)
lines = msg_body.split('\n')
embed()
def process_registrations(M): def process_registrations(M):
rv, msgs = M.search(None, "SUBJECT", "etho2020") rv, msgs = M.search(None, "SUBJECT", "etho2020", "UNSEEN")
participations = []
if rv != "OK":
print("ERROR searching messages", rv)
return participations
msgs = msgs[0].decode('ascii').split()
print("Found %i registration mails" % len(msgs)) print("Found %i registration mails" % len(msgs))
for msg_index in msgs: for msg_index in msgs:
process_message(msg_index) participations.append(process_message(msg_index))
return participations
M = imaplib.IMAP4_SSL('mailserv.uni-tuebingen.de')
passwd = "r.8-*0Fc"
try: if __name__ == "__main__":
rv, data = M.login(EMAIL_ACCOUNT, passwd) M = imaplib.IMAP4_SSL('mailserv.uni-tuebingen.de')
except imaplib.IMAP4.error: passwd = "r.8-*0Fc"
print ("LOGIN FAILED!!! ")
sys.exit(1)
try:
rv, data = M.login(EMAIL_ACCOUNT, passwd)
except imaplib.IMAP4.error:
print ("LOGIN FAILED!!! ")
sys.exit(1)
rv, mailboxes = M.list() #rv, mailboxes = M.list()
if rv == 'OK': #if rv == 'OK':
print("Mailboxes:") # print("Mailboxes:")
print(mailboxes) # print(mailboxes)
rv, data = M.select(EMAIL_FOLDER) rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK': if rv == 'OK':
print("Processing mailbox...\n") print("Processing mailbox %s\n"%EMAIL_FOLDER)
# process_mailbox(M) # process_mailbox(M)
else:
print("ERROR: Unable to open mailbox folder ", rv)
else: new_participations = process_registrations(M)
print("ERROR: Unable to open mailbox ", rv) embed()
M.close()
process_registrations(M) M.logout()
M.close()
M.logout()