Docker 部署 _实现每日情话 定时推送(apscheduler)
2024-10-12 15:47:54
由于最近工作比较忙,后续博客可能更新不及时,哈哈
前言:
由于python对于微信推送不够友好,需要扫码登录,短信接口需要RMB。我就想到了qq邮箱发送到好友,然而微信有qq邮箱提醒功能,就实现了我需要定时推送消息的需求。
import smtplib,requests
from email.mime.text import MIMEText
from email.utils import formataddr
from apscheduler.schedulers.blocking import BlockingScheduler def send_mail(recipients,content):
#收件人 内容
ret=True
try:
msg=MIMEText(content,'plain','utf-8')
msg['From']=formataddr(["每日一乐",'396961930@qq.com']) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['To']=formataddr(["",recipients]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['Subject']="开心每一天" # 邮件的主题,也可以说是标题
server=smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是465
server.login('396961930@qq.com', '自己的秘钥') # smtp秘钥 server.sendmail('396961930@qq.com',[recipients,],msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit()# 关闭连接
except Exception:# 如果 try 中的语句没有执行,则会执行下面的 ret=False
ret=False
return ret def get_lovelive_info():
'''
从土味情话中获取每日一句。
:return: str,土味情话
'''
print('获取土味情话...')
resp = requests.get("https://api.lovelive.tools/api/SweetNothings")
if resp.status_code == 200:
return resp.text + "\n"
else:
print('每日一句获取失败')
return None
def send():
send_mail('396961930@qq.com',get_lovelive_info()+"\t\t"+"(@Xcsg消息来自互联网)")
send_mail('768158105@qq.com',get_lovelive_info()+"\t\t"+"(@Xcsg消息来自互联网)")
send_mail('595829154@qq.com',get_lovelive_info()+"\t\t"+"(@Xcsg消息来自互联网)")
send_mail('312161486@qq.com',get_lovelive_info()+"\t\t"+"(@Xcsg消息来自互联网)") if __name__ == "__main__":
#定时任务
scheduler = BlockingScheduler()
scheduler.add_job(send, 'cron', hour='7,22', minute='0')
scheduler.start()
dokcerfile
FROM python:3.7 RUN mkdir -p /usr/src/app WORKDIR /usr/src/app COPY requirements.txt /usr/src/app/ RUN pip install -r /usr/src/app/requirements.txt RUN rm -rf /usr/src/app COPY . /usr/src/app CMD [ "python3", "./message.py"]
requirements:
apscheduler
requests
打包好运行如下:
***** 里面有时区问题坑,注意
效果如下:
最新文章
- webapi post 请求多个参数
- C# WebBrowser控件使用教程与技巧收集
- nginx https性能优化
- 转载 Android快捷键 转载
- Installing Erlang
- img、input到底是行内还是块级元素?
- EF Code First:实体映射,数据迁移,重构(1)
- C++编程练习(14)-------“单例模式”的实现
- EFBaseDal
- Razor视图引擎
- bzoj1047-理想的正方形(二维单调队列)
- 也来“玩”Metro UI之磁贴
- 响应的系统设置的事件——重写onConfigurationChanged响应系统设置更改
- javascript核心概念之——数组
- scrapy相关:splash安装 A javascript rendering service 渲染
- 一次Spring Bean初始化顺序问题排查记录
- react-redux中的数据传递
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
- python全栈开发day87~91-整个流程梳理、CRM功能、知识点梳理
- spring中afterPropertiesSet方法与init-method配置描述
热门文章
- Dynamics 365-表单元素取值/赋值
- 浅谈 STL
- Centos7安装Kubernetes k8s v1.16.0 国内环境
- 20200416_Centos 7.2 在安装系统之前把数据备份出来
- 树莓派RTL8723BU_LINUX驱动安装
- CoProcessFunction实战三部曲之三:定时器和侧输出
- 第7.21节 Python抽象类—register注册虚拟子类
- Python使用property函数和使用@property装饰器定义属性访问方法的异同点分析
- PyQt(Python+Qt)学习随笔:Qt Designer中主窗口对象的iconSize属性
- PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的accepted、rejected和helpRequested信号