Skip to content

Commit 82bd004

Browse files
committed
feat: newly opened companies; persist company list
1 parent d206ab4 commit 82bd004

File tree

5 files changed

+80
-10
lines changed

5 files changed

+80
-10
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ services:
1313
- $MFTP_CONFIG/mail_send_token.json:/app/mail_send_token.json
1414
- $MFTP_CONFIG/mail_send_creds.json:/app/mail_send_creds.json
1515
- $MFTP_CONFIG/.session:/app/.session
16+
- $MFTP_CONFIG/companies.json:/app/companies.json
1617
depends_on:
1718
- db
1819
networks:

mftp/company.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import json
13
import logging
24
from env import ROLL_NUMBER
35
from datetime import datetime
@@ -6,6 +8,9 @@
68
from endpoints import TPSTUDENT_URL, COMPANIES_URL
79

810

11+
COMPANIES_FILE = f"{os.path.dirname(__file__)}/companies.json"
12+
13+
914
def filter(companies, filter):
1015
print('[FILTERING COMPANY UPDATES]', flush=True)
1116

@@ -44,7 +49,7 @@ def fetch(session, headers, ssoToken):
4449
xml_encoded = xml_string.encode("utf-8")
4550
root = ET.fromstring(xml_encoded)
4651

47-
companies = []
52+
fetched_companies = []
4853
for row in root.findall("row"):
4954
jd_args = row.find("cell[4]").text.split("'")[5].split('"')
5055
jnf_id, com_id, year = jd_args[1], jd_args[3], jd_args[5]
@@ -75,9 +80,52 @@ def fetch(session, headers, ssoToken):
7580
"Interview_Date": row.find("cell[12]").text.strip() if row.find("cell[12]").text.strip() else None,
7681
}
7782

78-
companies.append(company_info)
83+
fetched_companies.append(company_info)
84+
85+
stored_companies = get_list()
86+
new_companies, modified_companies = get_new_and_modified_companies(fetched_companies, stored_companies)
87+
88+
store_list(fetched_companies)
89+
90+
return fetched_companies, new_companies, modified_companies
91+
92+
93+
def get_new_and_modified_companies(fetched, stored, unique_key="Job_Description"):
94+
# Create dictionaries for quick lookup by the unique key
95+
stored_dict = {entry[unique_key]: entry for entry in stored}
96+
fetched_dict = {entry[unique_key]: entry for entry in fetched}
97+
98+
new_entries = []
99+
updated_entries = []
100+
101+
for key, fetched_entry in fetched_dict.items():
102+
if key not in stored_dict:
103+
# New entry
104+
new_entries.append(fetched_entry)
105+
else:
106+
# Compare the values of the fetched entry with the stored entry
107+
stored_entry = stored_dict[key]
108+
if any(fetched_entry[k] != stored_entry.get(k) for k in fetched_entry):
109+
updated_entries.append(fetched_entry)
110+
111+
return new_entries, updated_entries
112+
113+
114+
def store_list(companies):
115+
with open(COMPANIES_FILE, "w") as json_file:
116+
json.dump(companies, json_file, indent=2)
117+
79118

80-
return companies
119+
def get_list():
120+
try:
121+
with open(COMPANIES_FILE, "r") as json_file:
122+
return json.load(json_file)
123+
except json.JSONDecodeError as _:
124+
store_list([])
125+
return []
126+
except FileNotFoundError:
127+
store_list([])
128+
return []
81129

82130

83131
# Downloads pdf content in bytes format

mftp/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ services:
1313
- $MFTP_CONFIG/mail_send_token.json:/app/mail_send_token.json
1414
- $MFTP_CONFIG/mail_send_creds.json:/app/mail_send_creds.json
1515
- $MFTP_CONFIG/.session:/app/.session
16+
- $MFTP_CONFIG/companies.json:/app/companies.json
1617
depends_on:
1718
- db
1819
networks:

mftp/mail.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ def send_companies(mail, gmail_api, smtp):
5656
logging.error(f" Failed to Send Mail : {mail['Subject']} ~ {str(e)}")
5757

5858

59-
def format_companies(ssoToken, companies):
59+
def format_companies(ssoToken, companies, subject):
6060
print('[FORMATTING COMPANY UPDATES]', flush=True)
6161

6262
message = MIMEMultipart()
63-
message["Subject"] = "APPLY NOW! | Companies Open = Y & Applied = N "
63+
message["Subject"] = subject
6464
message["From"] = f'MFTP < {FROM_EMAIL} >'
6565
message["Bcc"] = ", ".join(HOSTER_EMAIL)
6666

mftp/mftp.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import notice
77
import company
88

9+
import logging
910
import requests
1011
import argparse
1112
from datetime import datetime
@@ -32,11 +33,30 @@
3233
_, ssoToken = erp.login(headers, session, ERPCREDS=env, OTP_CHECK_INTERVAL=2, LOGGING=True, SESSION_STORAGE_FILE='.session')
3334

3435
if args.gmail_api or args.smtp:
35-
if now.minute == 0:
36-
companies = company.fetch(session, headers, ssoToken)
37-
open_not_applied_companies = company.filter(companies, "OPEN_N")
38-
companies_update_mail = mail.format_companies(session.cookies.get('ssoToken'), open_not_applied_companies)
39-
mail.send_companies(companies_update_mail, args.gmail_api, args.smtp)
36+
_, new, modified = company.fetch(session, headers, ssoToken)
37+
38+
if new:
39+
print('[NEW COMPANIES]', flush=True)
40+
for com in new:
41+
logging.info(f' {com["Name"]} | {com["Role"]} | {com["CTC"]} | {com["End_Date"]} | {com["Interview_Date"]}')
42+
if modified:
43+
print('[MODIFIED COMPANIES]', flush=True)
44+
for com in modified:
45+
logging.info(f' {com["Name"]} | {com["Role"]} | {com["CTC"]} | {com["End_Date"]} | {com["Interview_Date"]}')
46+
47+
filtered = []
48+
if new + modified:
49+
filtered = company.filter(new + modified, "OPEN_N")
50+
if filtered:
51+
for com in filtered:
52+
logging.info(f' {com["Name"]} | {com["Role"]} | {com["CTC"]} | {com["End_Date"]} | {com["Interview_Date"]}')
53+
54+
latest_ssoToken = session.cookies.get('ssoToken')
55+
mail_subject = "APPLY NOW! New companies opened"
56+
companies_mail = mail.format_companies(latest_ssoToken, filtered, mail_subject)
57+
mail.send_companies(companies_mail, args.gmail_api, args.smtp)
58+
else:
59+
print("[NO NEW COMPANIES]")
4060

4161
notice_db = db.NoticeDB(config={
4262
'uri': env.MONGO_URI,

0 commit comments

Comments
 (0)