feat: use Mailer template
This commit is contained in:
parent
77b51204af
commit
5b4982acf1
27
mailer.py
Normal file
27
mailer.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import smtplib
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
|
||||||
|
class Mailer():
|
||||||
|
def __init__(self, google_owner, google_code):
|
||||||
|
self.google_owner = google_owner
|
||||||
|
self.google_code = google_code
|
||||||
|
|
||||||
|
def send(self, sender, receiver, subject, msg):
|
||||||
|
msg = MIMEText(msg, 'html')
|
||||||
|
msg['Subject'] = subject
|
||||||
|
msg['From'] = sender
|
||||||
|
msg['To'] = receiver
|
||||||
|
|
||||||
|
smtp = smtplib.SMTP('smtp.gmail.com', 587)
|
||||||
|
smtp.ehlo()
|
||||||
|
smtp.starttls()
|
||||||
|
smtp.login(self.google_owner, self.google_code)
|
||||||
|
|
||||||
|
status = smtp.send_message(msg)
|
||||||
|
smtp.quit()
|
||||||
|
|
||||||
|
if status == {}:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
24
main.py
24
main.py
@ -5,11 +5,14 @@ import time
|
|||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
import os
|
import os
|
||||||
|
from mailer import Mailer
|
||||||
|
|
||||||
|
|
||||||
DEBUG_MODE = False
|
DEBUG_MODE = False
|
||||||
|
|
||||||
URL = 'https://www.cmlab.csie.ntu.edu.tw/status-gpu/'
|
URL = 'https://www.cmlab.csie.ntu.edu.tw/status-gpu/'
|
||||||
GPU_LIMIT = 2
|
GPU_LIMIT = 2
|
||||||
|
GOOGLE_OWNER = "snsd0805@cmlab.csie.ntu.edu.tw"
|
||||||
GOOGLE_CODE = os.environ['GOOGLE_CODE']
|
GOOGLE_CODE = os.environ['GOOGLE_CODE']
|
||||||
MAIL_CD_HOUR = 12
|
MAIL_CD_HOUR = 12
|
||||||
MAIL_MESSAGE = '''
|
MAIL_MESSAGE = '''
|
||||||
@ -39,6 +42,8 @@ Email: unix_manager@cmlab.csie.ntu.edu.tw<br>
|
|||||||
</pre>
|
</pre>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE)
|
||||||
|
|
||||||
def get_server_gpu_status() -> list:
|
def get_server_gpu_status() -> list:
|
||||||
'''
|
'''
|
||||||
It will get html from CMLab webpage.
|
It will get html from CMLab webpage.
|
||||||
@ -188,30 +193,25 @@ def mail_notify(violators: list) -> None:
|
|||||||
username = violator['username']
|
username = violator['username']
|
||||||
|
|
||||||
if check_send(send_log, username):
|
if check_send(send_log, username):
|
||||||
|
week_count = 1 if username not in send_log else send_log[username]['week_count']+1
|
||||||
# update log
|
# update log
|
||||||
send_log[username] = time.time()
|
send_log[username] = {
|
||||||
|
'time': time.time(),
|
||||||
|
'week_count': week_count
|
||||||
|
}
|
||||||
with open('send_log.json', 'w') as fp:
|
with open('send_log.json', 'w') as fp:
|
||||||
json.dump(send_log, fp)
|
json.dump(send_log, fp)
|
||||||
usage_msg = get_usage_msg(violator['usage'])
|
usage_msg = get_usage_msg(violator['usage'])
|
||||||
print(f' {username} {usage_msg}')
|
print(f' {username} {usage_msg}')
|
||||||
msg = MAIL_MESSAGE.format(username, usage_msg)
|
msg = MAIL_MESSAGE.format(username, usage_msg)
|
||||||
msg = MIMEText(msg, 'html') # 郵件內文
|
|
||||||
msg['Subject'] = f'[網管通知] 提醒您已經超過 CMLab GPU 使用限制!({username})'
|
|
||||||
msg['From'] = 'unix_manager@cmlab.csie.ntu.edu.tw'
|
|
||||||
msg['To'] = f'{username}@cmlab.csie.ntu.edu.tw' if not DEBUG_MODE else 'snsd0805@cmlab.csie.ntu.edu.tw'
|
|
||||||
# msg['Cc'] = 'unix_manager@cmlab.csie.ntu.edu.tw' if not DEBUG_MODE else 'snsd0805@cmlab.csie.ntu.edu.tw'
|
|
||||||
|
|
||||||
smtp = smtplib.SMTP('smtp.gmail.com', 587)
|
status = mailer.send('unix_manager@cmlab.csie.ntu.edu.tw', f'{username}@cmlab.csie.ntu.edu.tw' if not DEBUG_MODE else 'snsd0805@cmlab.csie.ntu.edu.tw', \
|
||||||
smtp.ehlo()
|
f'[網管通知] 提醒您已經超過 CMLab GPU 使用限制!({username})', msg)
|
||||||
smtp.starttls()
|
|
||||||
smtp.login('snsd0805@cmlab.csie.ntu.edu.tw', GOOGLE_CODE)
|
|
||||||
|
|
||||||
status = smtp.send_message(msg)
|
|
||||||
if status == {}:
|
if status == {}:
|
||||||
print(f' {username}, 郵件傳送成功!')
|
print(f' {username}, 郵件傳送成功!')
|
||||||
else:
|
else:
|
||||||
print(f' {username}, 郵件傳送失敗...')
|
print(f' {username}, 郵件傳送失敗...')
|
||||||
smtp.quit()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user