Now it reads my emails and writes down the sender and content, I just need to implement that it only reads emails sent from my school and downloads the attachment to pass to 'update_time_school.py' which will do the rest of the work.

This commit is contained in:
Stefano Assenzo
2022-12-28 15:04:42 +00:00
parent 74d1ef52c6
commit 6b7f0f86b6
3 changed files with 49 additions and 4 deletions

10
app.py
View File

@ -1,6 +1,7 @@
from src.script import api_test
from flask import Flask, render_template, request, redirect, session, url_for, jsonify
#from src.script import api_test
from flask import Flask, render_template, request, session, jsonify
import requests
import bcrypt
import os
app = Flask(__name__)
@ -16,12 +17,15 @@ def homepage():
@app.route('/orario')
def orario():
return render_template('html/orario.html')
if 'username' in session:
return "You are logged in as " + session['username']
#return render_template('html/orario.html')
@app.route('/calendario')
def calendario():
return render_template('html/calendario.html')
# Da sistemare
@app.route('/api', methods = ['GET', 'POST'])
def api():
if(request.method == 'GET'):

40
src/events/check_email.py Normal file
View File

@ -0,0 +1,40 @@
import imaplib
import email
import traceback
import os
from dotenv import load_dotenv
load_dotenv()
FROM_PWD = os.getenv('PWD_EMAIL')
FROM_EMAIL = os.getenv('EMAIL')
SMTP_SERVER = os.getenv('SMTP_SERVER')
SMTP_PORT = os.getenv('SMTP_PORT')
def read_email_from_gmail():
try:
mail = imaplib.IMAP4_SSL(SMTP_SERVER)
mail.login(FROM_EMAIL,FROM_PWD)
mail.select('inbox')
data = mail.search(None, 'ALL')
mail_ids = data[1]
id_list = mail_ids[0].split()
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])
for i in range(latest_email_id,first_email_id, -1):
data = mail.fetch(str(i), '(RFC822)' )
for response_part in data:
arr = response_part[0]
if isinstance(arr, tuple):
msg = email.message_from_string(str(arr[1],'utf-8'))
email_subject = msg['subject']
email_from = msg['from']
print('From : ' + email_from + '\n')
print('Subject : ' + email_subject + '\n')
except Exception as e:
traceback.print_exc()
print(str(e))
read_email_from_gmail()

View File

@ -5,6 +5,7 @@ import pymongo
import os
import datetime
from bson.objectid import ObjectId
import os
load_dotenv() #Load .env file
PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') #Password for MongoDB
@ -58,7 +59,7 @@ for row in range (1, 100):
collection_archive.update_one(
{ "_id": ObjectId(array_username)},
{
"$push": { "School Subject": str(remove_things_in_front) }
"$push": { "School Subject": "null" }
}
)
else: