From 6e5ceca13447af0794ed5df237077113f304fbf6 Mon Sep 17 00:00:00 2001 From: IronXiao Date: Wed, 29 Sep 2021 19:07:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=8C=E7=BB=B4=E7=A0=81?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=88=B0=E9=82=AE=E7=AE=B1=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E4=BE=BF=E4=BA=BA=E4=B8=8D=E5=9C=A8=E7=94=B5?= =?UTF-8?q?=E8=84=91=E6=97=81=E7=99=BB=E9=99=86=E5=A4=B1=E6=95=88=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=BF=9C=E7=A8=8B=E6=89=AB=E6=8F=8F=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- py12306/helpers/notification.py | 45 +++++++++++++++++++++++++++++++++ py12306/log/common_log.py | 2 ++ py12306/user/job.py | 5 ++++ 3 files changed, 52 insertions(+) diff --git a/py12306/helpers/notification.py b/py12306/helpers/notification.py index 280372b..4a35db9 100644 --- a/py12306/helpers/notification.py +++ b/py12306/helpers/notification.py @@ -33,6 +33,11 @@ class Notification(): self = cls() self.send_email_by_smtp(to, title, content) + @classmethod + def send_email_with_qrcode(cls, to, title='', qrcode_path=''): + self = cls() + self.send_email_by_smtp_with_qrcode(to, title, qrcode_path) + @classmethod def send_to_telegram(cls, content=''): self = cls() @@ -134,6 +139,46 @@ class Notification(): except Exception as e: CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_EMAIL_FAIL.format(e)).flush() + def send_email_by_smtp_with_qrcode(self, to, title, qrcode_path): + import smtplib + from email.mime.text import MIMEText + from email.mime.multipart import MIMEMultipart + from email.mime.image import MIMEImage + to = to if isinstance(to, list) else [to] + message = MIMEMultipart() + message['Subject'] = title + message['From'] = Config().EMAIL_SENDER + message['To'] = ", ".join(to) + htmlFile = """ + + + +

+ 这是你的二维码 +

+

+

+ + + """ + htmlApart = MIMEText(htmlFile, 'html') + imageFile = qrcode_path + imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1]) + imageApart.add_header('Content-ID', '<0>') + message.attach(imageApart) + message.attach(htmlApart) + try: + server = smtplib.SMTP(Config().EMAIL_SERVER_HOST) + server.ehlo() + server.starttls() + server.login(Config().EMAIL_SERVER_USER, Config().EMAIL_SERVER_PASSWORD) + server.send_message(message) + server.quit() + CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_EMAIL_WITH_QRCODE_SUCCESS).flush() + self.push_bark(CommonLog.MESSAGE_SEND_EMAIL_WITH_QRCODE_SUCCESS) + except Exception as e: + CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_EMAIL_FAIL.format(e)).flush() + def send_dingtalk_by_webbook(self, content): from dingtalkchatbot.chatbot import DingtalkChatbot webhook = Config().DINGTALK_WEBHOOK diff --git a/py12306/log/common_log.py b/py12306/log/common_log.py index f8c2be8..3f50119 100644 --- a/py12306/log/common_log.py +++ b/py12306/log/common_log.py @@ -35,6 +35,8 @@ class CommonLog(BaseLog): MESSAGE_SEND_EMAIL_SUCCESS = '邮件发送成功,请检查收件箱' MESSAGE_SEND_EMAIL_FAIL = '邮件发送失败,请手动检查配置,错误原因 {}' + MESSAGE_SEND_EMAIL_WITH_QRCODE_SUCCESS = '二维码邮件发送成功,请检查收件箱扫描登陆' + MESSAGE_SEND_TELEGRAM_SUCCESS = 'Telegram推送成功' MESSAGE_SEND_TELEGRAM_FAIL = 'Telegram推送失败,错误原因 {}' diff --git a/py12306/user/job.py b/py12306/user/job.py index 38bad74..a59142b 100644 --- a/py12306/user/job.py +++ b/py12306/user/job.py @@ -215,11 +215,16 @@ class UserJob: else: print_qrcode(png_path) UserLog.add_log(UserLog.MESSAGE_QRCODE_DOWNLOADED.format(png_path)).flush() + Notification.send_email_with_qrcode(Config().EMAIL_RECEIVER, '你有新的登录二维码啦!', png_path) return result.get('uuid'), png_path raise KeyError('获取二维码失败: {}'.format(result.get('result_message'))) except Exception as e: UserLog.add_quick_log( UserLog.MESSAGE_QRCODE_FAIL.format(e, self.retry_time)).flush() + try: + os.remove(self.get_cookie_path()) + except: + pass time.sleep(self.retry_time) return self.download_code()