吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3787|回复: 45
收起左侧

[Python 转载] python 给女友播报天气(学习坛友 实战)

  [复制链接]
生虢 发表于 2021-8-16 20:08
本帖最后由 生虢 于 2021-8-16 20:32 编辑

最近逛论坛发现几个帖子
Qmsg酱的实时关爱推送(利用python写个假女友) - 『编程语言区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn
女朋友要我每天给她报天气,这不耽误我打游戏嘛 - 『编程语言区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn


突有灵感 借 网站
Qmsg酱-您的专属QQ消息推送服务小姐姐-qmsg.zendee.cn

天行数据TianAPI - 开发者API数据平台



外加腾讯云的 云函数
完成自动给女友发消息 本着分享的心态与大家交流 也借此机会巩固一下python
QQ截图20210816165952.png



食用方法
1.得到代码
2.用自己的key替换代码中 key 城市 也要替换 注意 do.py 156行 的key 是天气key https://console.qweather.com/ 这里获取
   其他地方key都可从 https://www.tianapi.com/ 中 获取
3. 替换完成后 在 Qmsg酱-您的专属QQ消息推送服务小姐姐-qmsg.zendee.cn 配置你要推送qq信息 注意 如果想要接收到消息 必须添加机器人好友   Qmsg酱的实时关爱推送(利用python写个假女友) - 『编程语言区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn
   女朋友要我每天给她报天气,这不耽误我打游戏嘛 - 『编程语言区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn  这两个贴中都有详细教程
4.本地测试一下
5.测试ok后 可直接打包上传到TX云函数中
6.大功告成 坐等消息

如果本贴对你有帮助 还请给个热心值 鼓励一下喽
本来是七夕当天编辑的 由于个人原因就 就今天发出来了





weather.zip

3.01 KB, 下载次数: 91, 下载积分: 吾爱币 -1 CB

免费评分

参与人数 4吾爱币 +10 热心值 +3 收起 理由
xuexizaixuexi + 1 + 1 谢谢@Thanks!
寒冰流火 + 1 + 1 热心回复!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
dgy + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 生虢 发表于 2021-8-16 20:22
获取天气 key 地址 console.qweather.com/

其他api key获取地址 https://www.tianapi.com/
 楼主| 生虢 发表于 2021-8-16 20:19
do.py
[Asm] 纯文本查看 复制代码
# coding=utf-8

import requests
import json
import time


# 改进
# 天气提示语
# 天气图标
# 获取天气图标

# 用个数表示天气 注意 0 时
def make_weather_icon(weather_name):
    weather_msg = ''
    if '晴' in weather_name:
        weather_id = 74
        weather_msg = f'@face={weather_id}@'
    elif '多' or '阴' in weather_name:  # 多云 或者 阴天
        weather_id = 91
        weather_msg = f'@face={weather_id}@'
    elif '小' or '雨' or '阵' in weather_name:  # 小于 或者 雨
        weather_id = 90
        weather_msg = f'@face={weather_id}@'
    elif '大' or '暴' or '暴雨' in weather_name:  # 小雨 或者 雨
        weather_id = 90
        weather_msg = f'@face={weather_id}@ @face={weather_id}@ @face={weather_id}@'
    elif '雷电' or '雷' or '电' in weather_name:  # 雷电
        weather_id = 54
        weather_msg = f'@face={weather_id}@ @face={weather_id}@'
    return weather_msg


# 温度 判断
def tem_icon(tem_num):
    tem_num = int(tem_num)
    tem_msg = ''
    if tem_num <= 10:  # 冷
        tem_id = 1
        tem_msg = f'有点冷 注意保暖哦~~~~  @face={tem_id}@ '
    elif 20 <= tem_num <= 25:
        tem_id = 21  # 晴天 温度适中
        tem_msg = f'这温度好凉爽啊~~ @face={tem_id}@'
    elif 25 < tem_num < 30:
        tem_id = 35  # 有点热哦
        tem_msg = f'有点热哦~~ @face={tem_id}@'
    elif tem_num > 30:
        tem_id = 34  # 有点热哦
        tem_msg = f'快热晕了~~ @face={tem_id}@'
    return tem_msg


# 天气提示
def set_weather_msg(weather_name_d, weather_name_n):
    weather_msg = ''
    if ('晴' or '晴天') in weather_name_d and ('多云' or '云' or '阴天') in weather_name_n:  # 晴转多云
        weather_msg = '[提示语]'
    elif ('晴' or '晴天') in weather_name_d and ('晴' or '晴天') in weather_name_n:  # 晴转多云
        weather_msg = '[提示语]'
    elif '小雨' in weather_name_d and ('晴' or '晴天') in weather_name_n:  # 晴转多云
        weather_msg = '[提示语]'
    elif '小雨' in weather_name_d and '小雨' in weather_name_n:  # 晴转多云
        weather_msg = '[提示语]'
    elif '小雨' in weather_name_d and '多云' in weather_name_n:  # 晴转多云
        weather_msg = '[提示语]'

    elif ('晴' or '晴天') in weather_name_d and '小雨' in weather_name_n:  # 晴转小雨
        weather_msg = '[提示语]'

    elif ('晴' or '晴天') in weather_name_d and '大雨' in weather_name_n:  # 晴转大雨
        weather_msg = '[提示语]'

    elif ('云' or '多云') in weather_name_d and ('晴天' or '晴') in weather_name_n:  # 多云转晴
        weather_msg = '[提示语]'
    elif ('云' or '多云') in weather_name_d and ('云' or '多云') in weather_name_n:  # 多云转晴
        weather_msg = '[提示语]'
    # 雷雨转晴
    elif ('雷' or '闪电' or '雷阵雨' or '雷阵') in weather_name_d and ('晴天' or '晴') in weather_name_n:  # 多云转晴
        weather_msg = '[提示语]'
    elif ('雷' or '闪电' or '雷阵雨' or '雷阵') in weather_name_d and ('雷' or '闪电' or '雷阵雨' or '雷阵') in weather_name_n:  # 多云转晴
        weather_msg = '[提示语]'
    # 阵雨转晴
    elif ('阵雨' or '阵') in weather_name_d and ('晴天' or '晴') in weather_name_n:  # 多云转晴
        weather_msg = '[提示语]'
    elif ('阵雨' or '阵') in weather_name_d and ('阵雨' or '阵') in weather_name_n:  # 多云转晴
        weather_msg = '[提示语]'
    # 大雨转晴
    elif ('大' or '大雨') in weather_name_d and ('晴天' or '晴') in weather_name_n:  # 多云转晴
        weather_msg = '[提示语]'
    elif ('大' or '大雨') in weather_name_d and ('大' or '大雨') in weather_name_n:  # 多云转晴
        weather_msg = '[提示语]'

    return weather_msg


# 天气
def weather(address, key, day):  # day :0 =今天 1 明天 2后天 (最大为2)
    url = f"https://free-api.heweather.com/s6/weather/forecast"
    content = {
        'day': '',  # 日期
        'city': '',  # 城市
        'province': '',  # 省份
        'cond_txt_d': '',  # 初始  初始 转 结束
        'cond_txt_d_icon': '',  # 初始 图标
        'cond_txt_n': '',  # 结束
        'cond_txt_n_icon': '',  # 结束 图标
        'cond_msg': '',  # 天气提示语
        'tmp_max': '',  # 最高温度
        'tmp_max_id': 0,  # 最高温度图标id
        'tmp_min': '',  # 最低温度
        'tmp_min_id': 0,  # 最低温度图标id
        'tmp_msg': ''  # 温度提示语
    }
    try:
        if day > 2:
            return '超出天数'
        dic = {
            'location': f'{address}',
            'key': f'{key}',
            'lang': 'zh'
        }
        req = requests.get(url=url, params=dic)

        req_json = req.json()
        content['city'] = req_json['HeWeather6'][0]['basic']['location']  # 城市
        content['province'] = req_json['HeWeather6'][0]['basic']['admin_area']  # 省份

        content['day'] = req_json['HeWeather6'][0]['daily_forecast'][day]['date']  # 日期
        content['cond_txt_d'] = req_json['HeWeather6'][0]['daily_forecast'][day]['cond_txt_d']  # 天气
        content['cond_txt_n'] = req_json['HeWeather6'][0]['daily_forecast'][day]['cond_txt_n']  # 天气

        content['tmp_max'] = req_json['HeWeather6'][0]['daily_forecast'][day]['tmp_max']  # 最高温度
        content['tmp_min'] = req_json['HeWeather6'][0]['daily_forecast'][day]['tmp_min']  # 最低温度

        # 设置图标
        # 设置天气图标
        content['cond_txt_d_icon'] = make_weather_icon(content['cond_txt_d'])
        content['cond_txt_n_icon'] = make_weather_icon(content['cond_txt_n'])
        # 温度图标
        content['tmp_max_id'] = tem_icon(content['tmp_max'])
        content['tmp_min_id'] = tem_icon(content['tmp_min'])

        # 设置提示语
        # 天气提示语
        content['cond_msg'] = set_weather_msg(content['cond_txt_d'], content['cond_txt_n'])
        # 温度提示
        content['tmp_msg'] = tem_icon(content['tmp_max'])
        return content

    except Exception as e:
        print(f"出错了  错误信息是{e}")


# 整合天气提示语
def make_msg_weather_details(day):
    weather_result = weather('[城市]', '【你的key】', day=day)

    weather_msg = f"天气: 省份 {weather_result['province']} 城市  {weather_result['city']}   日期 {weather_result['day']} @face=144@  天气 {weather_result['cond_txt_d']} {weather_result['cond_txt_d_icon']}  转 {weather_result['cond_txt_n']} {weather_result['cond_txt_n_icon']}  {weather_result['cond_msg']}  最高温度 {weather_result['tmp_max']} °C   {weather_result['tmp_msg']} 最低温度 {weather_result['tmp_min']} °C "
    return weather_msg


# 早安 晚安
def morning(loca_time, key):
    content = ''
    url = "http://api.tianapi.com/txapi/zaoan/index?key="

    try:
        if loca_time > 5 and loca_time < 8:  # 早上
            req = requests.get(url=(url + key))
            req_json = req.json()
            content = req_json['newslist'][0]['content']
        else:
            print("现在还不是早上")
            print(f"现在不是早上现在是{time.localtime().tm_hour}时 传入时间是{loca_time}时")
        return content
    except Exception as e:
        print(f"出错了 错误信息是{e}")
        pass


# 土味情话
def love_msg(key):
    content = ''
    url = f"http://api.tianapi.com/txapi/saylove/index?key={key}"
    try:
        req = requests.get(url=url)
        req_json = req.json()
        content = req_json['newslist'][0]['content']
        return content
    except Exception as e:
        print(f"出错了 错误信息是{e}")


# 英语一句话
def english_msg(key):
    content = {
        "zh": '',
        "en": ''
    }
    url = f"http://api.tianapi.com/txapi/everyday/index?key={key}"
    try:
        req = requests.get(url=url)
        req_json = req.json()
        # print(req_json)
        content["zh"] = req_json['newslist'][0]['content']
        content["en"] = req_json['newslist'][0]['note']

        # 输出
        # for key, value in content.items():
        #     print(key, value)
        return content
    except Exception as e:
        print(f"出错了 错误信息是{e}")


# 古代情诗
def ancient_poetry(key):
    content = {
        'cont': '',
        'source': '',
        'author': ''
    }
    url = f"http://api.tianapi.com/txapi/qingshi/index?key={key}"
    try:
        req = requests.get(url=url)
        req_json = req.json()
        # print(req_json)
        content['cont'] = req_json['newslist'][0]['content']
        content['source'] = req_json['newslist'][0]['source']
        content['author'] = req_json['newslist'][0]['author']

        # 输出
        # for key, value in content.items():
        #     print(key, value)
        return content
    except Exception as e:
        print(f"出错了 错误信息是{e}")


# 发送消息
def send_msg(msg):
    try:
        url = f'https://qmsg.zendee.cn/send/fcab95a8092f2e4c94f7ec846b8817d3?msg={msg}'
        requests.get(url=url)
    except Exception as e:
        print(f"出错了 错误信息是{e}")
 楼主| 生虢 发表于 2021-8-16 20:18
index.py

[Asm] 纯文本查看 复制代码
# coding=utf-8
import time

import do

global content  # 内容
global msg  # 要发送的消息


def sign():
    try:
        # 早安

        do.send_msg(do.morning(time.localtime().tm_hour, '[你的key]'))
        time.sleep(5)
        # 天气
        weather_msg = do.make_msg_weather_details(day=0)
        # 发送天气
        do.send_msg(weather_msg)

        print("喝杯茶歇会儿5秒")
        time.sleep(5)


        # 情话
        love_msg = do.love_msg('[你的key]')
        do.send_msg(love_msg)
        print("情话发送完成")
        print("喝杯茶歇会儿5秒")
        time.sleep(5)
        # 诗句
        ancient_poetry = do.ancient_poetry('[你的key]')
        ancient_poetry_msg = f"《{ancient_poetry['source']}》 \n {ancient_poetry['cont']} \n --{ancient_poetry['author']}"
        do.send_msg(ancient_poetry_msg)
        print('每日诗句播报完毕')
        print("喝杯茶歇会儿5秒")
        time.sleep(5)

        # 英语

        english = do.english_msg('[你的key]')
        englist_msg = f"每日一句英语\n {english['zh']} \n {english['en']}"
        do.send_msg(englist_msg)
        print('每日英语播报完毕')
        print("喝杯茶歇会儿5秒")
        time.sleep(5)
        do.send_msg("播报完毕")

        print("信息全部投递完成~~")
        return True
    except Exception as e:
        print(f"出错了 错误信息是{e}")
        return False


def main():
    sign()


def main_handler(event, context):
    return main()


if __name__ == '__main__':
    main()
璐璐诺 发表于 2021-8-16 20:13
默默地问一句 可以实现微信的吗~
 楼主| 生虢 发表于 2021-8-16 20:16
璐璐诺 发表于 2021-8-16 20:13
默默地问一句 可以实现微信的吗~

嗯... 没找到微信机器人 如果你有的话可以试试
小小佩奇 发表于 2021-8-16 20:20
璐璐诺 发表于 2021-8-16 20:13
默默地问一句 可以实现微信的吗~

企业微信可以,我现在用这个每天向自己微信推送一句话
zhaomingzhi 发表于 2021-8-16 20:34
老乡啊,支持一下
山东菏泽曹县666我的宝贝儿
 楼主| 生虢 发表于 2021-8-16 20:37
本帖最后由 生虢 于 2021-8-20 17:49 编辑

~~~~~~~~~~~~~~
htpidk 发表于 2021-8-16 20:40
你女朋友真耐心啊 能看这么多字都不骂你
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 01:26

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表