diff --git a/mftp/mail.py b/mftp/mail.py index 1ff05bc..aac901c 100644 --- a/mftp/mail.py +++ b/mftp/mail.py @@ -4,9 +4,9 @@ from bs4 import BeautifulSoup as bs from email.mime.text import MIMEText from email.mime.base import MIMEBase +from endpoints import NOTICE_CONTENT_URL from email.mime.multipart import MIMEMultipart from env import FROM_EMAIL, FROM_EMAIL_PASS, BCC_EMAIL_S -from endpoints import NOTICE_CONTENT_URL, ATTACHMENT_URL def send(mails, smtp, gmail_api, notice_db): @@ -108,22 +108,13 @@ def format_notice(notices, session): message.attach(MIMEText(body, "html")) - # Handling attachment - try: - attachment = parseAttachment(session, year, id_) - except Exception as e: - logging.error(f" Failed to parse mail attachment ~ {str(e)}") - break - - if len(attachment) != 0: - notice['Attachment'] = attachment - + if 'Attachment' in notice: file = MIMEBase('application', 'octet-stream') - file.set_payload(attachment) + file.set_payload(notice['Attachment']) encoders.encode_base64(file) file.add_header('Content-Disposition', 'attachment', filename='Attachment.pdf') message.attach(file) - logging.info(f" [PDF ATTACHED] On notice #{id_} of length ~ {len(attachment)}") + logging.info(f" [PDF ATTACHED] On notice #{id_} of length ~ {len(notice['Attachment'])}") formatted_notifs.append({"formatted_notice": message, "original_notice": notice}) @@ -139,15 +130,6 @@ def parseBody(session, year, id_): return str(body) -def parseAttachment(session, year, id_): - stream = session.get(ATTACHMENT_URL.format(year, id_), stream=True) - attachment = b'' - for chunk in stream.iter_content(4096): - attachment += chunk - - return attachment - - def generate_send_service(): import os from googleapiclient.discovery import build diff --git a/mftp/notice.py b/mftp/notice.py index ffd9395..3b1484e 100644 --- a/mftp/notice.py +++ b/mftp/notice.py @@ -1,7 +1,7 @@ import logging import xml.etree.ElementTree as ET from bs4 import BeautifulSoup as bs -from endpoints import TPSTUDENT_URL, NOTICEBOARD_URL, NOTICES_URL +from endpoints import TPSTUDENT_URL, NOTICEBOARD_URL, NOTICES_URL, ATTACHMENT_URL LAST_NOTICES_CHECK_COUNT = 30 @@ -40,6 +40,16 @@ def fetch(headers, session, ssoToken, notice_db): 'Company': row.find('cell[4]').text.strip(), } + # Handling attachment + try: + attachment = parseAttachment(session, year, id_) + except Exception as e: + logging.error(f" Failed to parse mail attachment ~ {str(e)}") + break + + if attachment: + notice['Attachment'] = attachment + latest_notices.append(notice) # This is done to reduce DB queries @@ -53,3 +63,12 @@ def fetch(headers, session, ssoToken, notice_db): return new_notices + +def parseAttachment(session, year, id_): + stream = session.get(ATTACHMENT_URL.format(year, id_), stream=True) + attachment = b'' + for chunk in stream.iter_content(4096): + attachment += chunk + + return attachment + diff --git a/mftp/ntfy.py b/mftp/ntfy.py index cb76b01..b2f4cc1 100644 --- a/mftp/ntfy.py +++ b/mftp/ntfy.py @@ -5,7 +5,7 @@ import requests from urllib.parse import quote from bs4 import BeautifulSoup as bs -from endpoints import NOTICE_CONTENT_URL, ATTACHMENT_URL +from endpoints import NOTICE_CONTENT_URL from env import NTFY_BASE_URL, NTFY_TOPICS, NTFY_TOPIC_ICON, NTFY_USER, NTFY_PASS, HEIMDALL_COOKIE @@ -83,18 +83,9 @@ def format_notice(notices, session): # NTFY TOPICS LIST: Based on filters notification["NTFY_TOPICS"] = filter_subscribers(notice, NTFY_TOPICS) - # Handling attachments - try: - attachment = parseAttachment(session, year, id_) - except Exception as e: - logging.error(f" Failed to parse attachment ~ {str(e)}") - break - - if len(attachment) != 0: - notice['Attachment'] = attachment - + if 'Attachment' in notice: file_name = notification['Attachment'] - if save_file(file_name, attachment): + if save_file(file_name, notice['Attachment']): notification['Attachment'] = file_name else: break @@ -217,15 +208,6 @@ def delete_file(file_name): return False -def parseAttachment(session, year, id_): - stream = session.get(ATTACHMENT_URL.format(year, id_), stream=True) - attachment = b'' - for chunk in stream.iter_content(4096): - attachment += chunk - - return attachment - - def parseBody(notice, session, year, id_): content = session.get(NOTICE_CONTENT_URL.format(year, id_)) content_html = bs(content.text, 'html.parser')