用python实现给女朋友定时推送

鼓捣了一阵子的微信机器人今天终于运行成功了,可以选择在每天的5点20分为你的女朋友发送当日的天气,每日一句还有日历,如果你有多个女朋友的话,还可以创建一个列表,之后循环遍历列表为你的多个女朋友发送你的关怀,让你更好的做好时间管理
设置好你需要关怀的人的微信名,微信扫码登录,剩下的交给程序就可以了

用python实现给女朋友定时推送_第1张图片

需要导入的库有requsets请求库 微信wxpy库 time时间库
import json,datetime
import requests,itchat,sxtwl
from itchat.content import *
from wxpy import TEXT
import time

注意:光理论是不够的,在此送大家一套2020最新Python全栈实战视频教程,点击此处 免费获取一起进步哦!

1.创建一个日历函数

def getYMD():#获得对应的农历
    ymc = [u"十一", u"十二", u"正", u"二", u"三", u"四", u"五", u"六", u"七", u"八", u"九", u"十"]
    rmc = [u"初一", u"初二", u"初三", u"初四", u"初五", u"初六", u"初七", u"初八", u"初九", u"初十",
           u"十一", u"十二", u"十三", u"十四", u"十五", u"十六", u"十七", u"十八", u"十九",
           u"二十", u"廿一", u"廿二", u"廿三", u"廿四", u"廿五", u"廿六", u"廿七", u"廿八", u"廿九", u"三十", u"卅一"]
    Gan = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
    Zhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
    ShX = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"]
    numCn = ["天", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
    lunar = sxtwl.Lunar()
    year = datetime.datetime.now().year
    month = datetime.datetime.now().month
    rday = datetime.datetime.now().day
    day = lunar.getDayBySolar(year, month, rday)
    d = str(day.y) + "年" + str(day.m) + "月" + str(day.d) + "日"
    if day.Lleap:
        a = "润" + ymc[day.Lmc] + "月" + rmc[day.Ldi] + "日"
    else:
        a = ymc[day.Lmc] + "月" + rmc[day.Ldi] + "日"
    b = "星期" + numCn[day.week]
    c = Gan[day.Lyear2.tg] + Zhi[day.Lyear2.dz] + "年" + Gan[day.Lmonth2.tg] + Zhi[day.Lmonth2.dz] + "月" + Gan[
        day.Lday2.tg] + Zhi[day.Lday2.dz] + "日"
    txt = '今天日期:'+d + ', ' + b + '\n'+'中华农历: ' + a + ', ' + c
    return txt  #  返回当前的日期信息

2.爬虫爬取爱词霸的每日一句

def get_iciba_everyday_chicken_soup():
    url = 'http://open.iciba.com/dsapi/' # 爱词霸的api地址
    r = requests.get(url)
    all = json.loads(r.text)
    Englis = all['content']
    Chinese = all['note']
    everyday_soup = Chinese+'\n'+Englis+'\n'
    return everyday_soup  #  返回爱词霸的每日一句

3.天气接口函数

def get_sentence(api):
    santence = requests.get(api)

    return santence.json()

4.微信图灵机器人

def get_response(question):
    apikey = '17216627bdd6495480ec7608fa1f4aeb'
    url = 'http://openapi.tuling123.com/openapi/api/v2' + apikey + '&info=' + question
    res = requests.get(url).json()
    return res['text']

#  微信机器人
@itchat.msg_register(TEXT, isFriendChat=True)
def auto_reply(msg):
    print("消息是:%s" % msg['Content'])
    itchat.send_msg(get_response(msg['Content']), toUserName=msg['FromUserName'])
    print('auto_reply:%s' % get_response(msg['Content']))

5主函数main()

if __name__ == '__main__':
    names  = input("请输入你要发送人的微信名:")
    hours = int(input("请输入几点发送消息:"))
    minutes = int(input("请输入几分发送消息:"))
    number = input("输入所在城市的编号:")
    g = getYMD()
    g1 = get_iciba_everyday_chicken_soup()
    #  天气接口的网站 number为城市编号
    name = 'http://t.weather.sojson.com/api/weather/city/'+ number
    #  向get_sentence 传入参数
    g2 = get_sentence(name)
    times = g2['cityInfo']
    for key, name in times.items():
        city = times['city']
        parent = times['parent']
    #  字典嵌套字典
    time1 = g2['data']
    for key, name in time1.items():
        shidu = time1['shidu']
        pm25 = time1['pm25']
        quality = time1['quality']
        ganmao = time1['ganmao']
    time1 = g2['data']
    time2 = time1.get('forecast', '不存在该键')
    time2 = time2[0]
    itchat.auto_login(hotReload=True)
    for key, name in time2.items():
        high = time2['high']
        low = time2['low']
        fx = time2['fx']
        fl = time2['fl']
        type = time2['type']
        notice = time2['type']
    #  调用微信机器人
    users = itchat.search_friends(names)  # 找到用户
    userName = users[0]['UserName']

    while True:

        t = datetime.datetime.now()
        t1=t.strftime('%Y-%m-%d %H:%M:%S')
        hour = t.hour
        minute = t.minute
        second = t.second
        print('%d:%d:%d' % (hour,minute,second))
        if hour == hours and minute == minutes:
            itchat.send_msg("%s" % g, toUserName=userName)
            itchat.send_msg('%s' % g1, toUserName=userName)
            itchat.send_msg('所在省份:%s\n'
                            '所在城市:%s\n'
                            '今日最高温度:%s\n '
                            '今日最低温度:%s\n'
                            '风向:%s\n '
                            '风力:%s\n'
                            '湿度:%s \n'
                            'PM2.5: %s\n'
                            '空气质量:%s \n'
                            '易感指数:%s\n'
                            '天气:%s - %s '%(parent,city,high,low,fx,fl,shidu,pm25,
                                                quality,ganmao,type,notice), toUserName=userName)
            break
        else:
            time.sleep(5)  #  延迟5秒
            continue
    itchat.run()
    time.sleep(86400)

PS:

1.输入城市编码时,通过下列网址查找所希望的城市编码

https://github.com/baichengzhou/weather.api/blob/master/src/main/resources/citycode-2019-08-23.json

2.在输入小时数的时候为24小时格式,输入早上8点,要输入08,不能输入8.

3.输入发送的微信时,必须是接收人的微信全名,备注不可以接收,会查询不到通讯录

用python实现给女朋友定时推送_第2张图片

愿你在我看不到的地方安然无恙,

愿你的冬天永远不缺暖阳。

愿你的明天不再经历雨打风霜,

愿你的未来永远热泪盈眶            --写给前任

你可能感兴趣的:(python)