让python每日给女友发情话早安

如何让python每日清晨给女友一句暖心早安

2.0版本 带图片 自定义名称 非订阅号 教程代码已经发布:点我点我

先看效果:
在这里插入图片描述

  • 微信测试号注册
    · 首先打开微信接口测试号,因为这个消息是从接口号发出的

链接: 微信测试号申请地址

在这里插入图片描述
然后点击登录
1、登陆之后,我们需要记录几个信息
appid 和appsecret
在这里插入图片描述
2、用你女朋友的微信扫码,并且关注这个测试公众号,关注之后右侧就会出现女友的微信号了,这个微信号也要复制下来记住。
在这里插入图片描述
3、创建消息模版,也就是微信收到消息的样式模版

点击新增测试模板,然后模板标题就是消息的标题信息,下面的模板内容就是里面所有的消息信息
在这里插入图片描述
模板内容复制下面一段就ok:

	{{name.DATA}} 
	今天是:{{date.DATA}} 
	当前城市:{{city.DATA}} 
	今天的天气:{{weather.DATA}} 
	最低气温:{{min_temperature.DATA}} 
	最高气温:{{max_temperature.DATA}} 
	今天是我们恋爱的第{{love_day.DATA}}天 
	{{note.DATA}}

之后就是这个样子
在这里插入图片描述
然后保存后就可以看到模板ID,id也要记下来,之后会用到。
在这里插入图片描述

  • 程序编写
    在准备好appid appsecret 女友微信号 模版id后开始编写程序吧
from time import localtime,time
import time as t
import random
import cityinfo
from requests import get, post
from datetime import datetime, date
# schedule是服务器定时任务使用
import schedule

# notes中自己去百度搜索小情话,每次随机选取一条发送
notes = ['世上之事,皆难如意,世间之人,都不如你。',
'没有电影电视剧的浪漫,不敢许你三生三世,只愿给你两厅三室。',
]


class config():
    # 公众号配置
    # 公众号appId
    app_id = "appId"
    # 公众号appSecret
    app_secret = "公众号的Sceret"
    # 模板消息id
    template_id = "模板消息id"
    # 接收公众号消息的女友微信号
    user = ["女友微信号"]
    # 称呼,消息最上面的第一行数据
    name = '亲爱的'
    # 信息配置
    # 所在省份
    province = "吉林"
    # 所在城市
    city = "松原"
    # 在一起的日子,格式同上
    love_date = "2019-7-21"


def get_access_token():
    # appId
    app_id = config.app_id
    # appSecret
    app_secret = config.app_secret
    post_url = ("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}"
                .format(app_id, app_secret))
    access_token = get(post_url).json()['access_token']
    # print(access_token)
    return access_token


def get_weather(province, city):
    # 城市id
    city_id = cityinfo.cityInfo[province][city]["AREAID"]
    # city_id = 101280101
    # 毫秒级时间戳
    t = (int(round(time() * 1000)))
    headers = {
        "Referer": "http://www.weather.com.cn/weather1d/{}.shtml".format(city_id),
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
    }
    url = "http://d1.weather.com.cn/dingzhi/{}.html?_={}".format(city_id, t)
    response = get(url, headers=headers)
    response.encoding = "utf-8"
    response_data = response.text.split(";")[0].split("=")[-1]
    response_json = eval(response_data)
    # print(response_json)
    weatherinfo = response_json["weatherinfo"]
    # 天气
    weather = weatherinfo["weather"]
    # 最高气温
    temp = weatherinfo["temp"]
    # 最低气温
    tempn = weatherinfo["tempn"]
    return weather, temp, tempn


def send_message(to_user, access_token, city_name, weather, max_temperature, min_temperature):
    url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}".format(
        access_token)
    week_list = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
    year = localtime().tm_year
    month = localtime().tm_mon
    day = localtime().tm_mday
    today = datetime.date(datetime(year=year, month=month, day=day))
    week = week_list[today.isoweekday()]
    # 获取在一起的日子的日期格式
    love_year = int(config.love_date.split("-")[0])
    love_month = int(config.love_date.split("-")[1])
    love_day = int(config.love_date.split("-")[2])
    love_date = date(love_year, love_month, love_day)
    # 获取在一起的日期差
    love_days = str(today.__sub__(love_date)).split(" ")[0]
    # 软文

    notedetail = str(random.choice(notes))

    data = {
        "touser": to_user,
        "template_id": config.template_id,
        "url": "http://weixin.qq.com/download",
        "topcolor": "#FF0000",
        "data": {
            "name": {
                "value": config.name,
                "color": "#9470dc"

            },
            "date": {
                "value": "{} {}".format(today, week),
                "color": "#00FFFF"
            },
            "note": {
                "value": notedetail,
                "color": "#FA8AA7"
            },

            "city": {
                "value": city_name,
                "color": "#808A87"
            },
            "weather": {
                "value": weather,
                "color": "#ED9121"
            },
            "min_temperature": {
                "value": min_temperature,
                "color": "#00FF00"
            },
            "max_temperature": {
                "value": max_temperature,
                "color": "#FF6100"
            },
            "love_day": {
                "value": love_days,
                "color": "#87CEEB"
            },
        }
    }
    headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
    }
    response = post(url, headers=headers, json=data)
    print(response.text)
    # print(data)


def start():
    # 获取accessToken
    accessToken = get_access_token()
    # 接收的用户
    # user = config.user
    # 传入省份和市获取天气信息
    province, city = config.province, config.city
    weather, max_temperature, min_temperature = get_weather(province, city)

    # 公众号推送消息
    for user in config.user:
        send_message(user, accessToken, city, weather,
                     max_temperature, min_temperature)

# 如果有服务器可以运行这一段代码,把程序挂上去,然后每天自动7点发送
# schedule.every().day.at("7:00").do(start)
# while True:
#     schedule.run_pending()
#     t.sleep(1)

#如果没有服务器,就每天点击一下运行就发送一次
start()

因为整体都没用外部接口获取天气等数据,除了这个主文件,还有一个城市的文件cityinfo.py 两个文件源码都打包在一起了。
链接: 源码下载

如果需要自动化每日发送的话,就要买云服务器了
我这用的是阿里云的服务器,想买的自己研究,我就不打广告了
阿里云服务器
如果感觉云服务器太贵的话 最近我也在研究用serverless方式部署,之后弄好了再更新吧

最近朋友推给我了一份python全能工程师2022版视频,36周全栈教学,据说是某课的5K的课程,不要分,有要的自取吧
python全能工程师2022教学视频下载

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小袁同学.

赏个早餐吃?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值