Skip to content

Commit

Permalink
feat: parse notice while fetching ERP
Browse files Browse the repository at this point in the history
  • Loading branch information
proffapt committed Nov 27, 2024
1 parent 13cf823 commit afd146e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 44 deletions.
26 changes: 4 additions & 22 deletions mftp/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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})

Expand All @@ -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
Expand Down
21 changes: 20 additions & 1 deletion mftp/notice.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

24 changes: 3 additions & 21 deletions mftp/ntfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit afd146e

Please sign in to comment.