Docker 部署 _实现每日情话 定时推送(apscheduler)

由于最近工作比较忙,后续博客可能更新不及时,哈哈

前言:

 由于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

打包好运行如下:

 

 

***** 里面有时区问题坑,注意

 

效果如下:

 

posted @ 2020-05-26 18:52  Xcsg  Views(814)  Comments(1Edit  收藏  举报