Skip to content

Instantly share code, notes, and snippets.

@iamtalhaasghar
Created February 27, 2023 19:57
Show Gist options
  • Save iamtalhaasghar/73552b7a5612a982c67ec69b3919cd44 to your computer and use it in GitHub Desktop.
Save iamtalhaasghar/73552b7a5612a982c67ec69b3919cd44 to your computer and use it in GitHub Desktop.
a script to check time taken by a student to complete his/her thesis in my university by scraping data through emails i've received from thesis manager.
import imaplib
import re
import time
from dotenv import load_dotenv
import os
import email
load_dotenv()
imap_ssl_host = 'imap.gmail.com'
imap_ssl_port = 993
def listen_new_email(username, password):
stats = dict()
server = imaplib.IMAP4_SSL(imap_ssl_host, imap_ssl_port)
server.login(username, password)
server.select(readonly=True)
result, data = server.search(None, '(SUBJECT "MS Thesis Final Defence")')
for i,num in enumerate(data[0].split()):
try:
#result, data = server.fetch(num, '(BODY[HEADER.FIELDS (SUBJECT FROM DATE)])')
result, data = server.fetch(num, '(RFC822)')
data_dict = data[0][1].decode()
msg = str(email.message_from_bytes(data[0][1]))
#open('/tmp/email.txt', 'w').write(str(msg))#print(msg)
#regno = re.findall('Regn No.(.*?),', msg.replace('\n', ' '))[0]
#date = re.findall('thesis on (.*?)in', msg.replace('\n', ' '))[0]
#print(i,regno, date)
index = msg.index('Fall')
enroll_year = int(msg[index+5:index+9].replace('K', '0'))
last_year = int(re.findall('\*\*(.*)\* \*@', msg.replace('\n', ' '))[0][-4:].replace('K', '0'))
stats[last_year-enroll_year] = stats.get(last_year-enroll_year, 0) + 1
print(i, enroll_year, last_year, stats)
except Exception as e:
print(e)
with open('/tmp/research_stats.txt', 'w') as f:
for i in list(stats.keys()).sort(reverse=True):
f.write(f'{i}: {stat[i]}\n')
server.logout()
def main():
try:
user, passw = os.getenv('USER'), os.getenv('PASS') # enter your username / pass here
listen_new_email(user, passw)
except Exception as ex:
print(ex)
raise Exception(str(ex))
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment