吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4574|回复: 24
收起左侧

[Python 转载] Qmsg酱的实时关爱推送(利用python写个假女友)

  [复制链接]
从零学习的歪经小 发表于 2021-4-28 20:01
本帖最后由 从零学习的歪经小 于 2021-4-28 20:13 编辑

先感谢下   医生 发表的 女朋友要我每天给她报天气,这不耽误我打游戏嘛这个,发现QQ也有推送机器人。
本想写个玩玩,然后发现  https://www.tianapi.com/  这里面有好多还挺好玩的api。就弄了几个玩玩,每天免费100次调用
555.png
Key只能你们自己去申请了,刚放了一个直接用超,哎。
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-
import requests
import traceback
import sys
headers = {
'User - Agent': 'Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 86.0.4240.198Safari / 537.36'
}

def post_data(name, data):
    """
    调用Qmsg酱 发送消息~~
    :param name:
    :param data:
    :return:
    """
    QmsgKEY = "?????"  # 申请地址https://qmsg.zendee.cn/me.html#/login
    try:
        qmsg_url = 'https://qmsg.zendee.cn/send/%s' % QmsgKEY
        res = requests.post(qmsg_url, data=data).json()
        print("当前模块名: %s" % name, res['reason'])
    except:
        print('调用Qmsg酱出错,代码为:')
        traceback.print_exc()


def morning(time):
    """
    早安/晚安的话~~~
    :return:
    """
    try:
        url = 'http://api.tianapi.com/txapi/%san/index?key=%s' % (time, key)  # 申请地址https://www.tianapi.com/apiview/142和https://www.tianapi.com/apiview/143
        res = requests.get(url).json()
        content = res['newslist'][0]['content']
        if time == "zao":
            greetings = "早上好呀,老公大人"
        elif time == "wan":
            greetings = "晚安了哦,老公大人"
        data = {
            'msg': "%s@face=49@@face=109@\n%s@face=175@" % (greetings, content)
        }
        post_data("早安/晚安心语", data)
    except:
        print('早安/晚安心语出错,代码为:')
        traceback.print_exc()


def weather(code):
    """
    每日的天气模块
    :param code:
    :return:
    """
    try:
        url = 'http://t.weather.itboy.net/api/weather/city/%d' % code
        res = requests.get(url, headers=headers).json()

        city = res['cityInfo']['city']  # 地区
        forecast = res['data']['forecast']  # 具体天气
        quality = res['data']['quality']
        pm25 = res['data']['pm25']
        pm10 = res['data']['pm10']
        ganmao = res['data']['ganmao']  # ganmao: "各类人群可自由活动"
        shidu = res['data']['shidu']
        wendu = res['data']['wendu'] + '℃'

        def poem(type_we):
            """
            天气诗句~~
            :param type_we:
            :return:
            """
            try:
                for type_line in list(type_we):  # 1=风、2=云、3=雨、4=雪、5=霜、6=露 、7=雾、8=雷、9=晴、10=阴
                    tqtype = 9
                    if type_line == '风':
                        tqtype = 1
                        break
                    elif type_line == '云':
                        tqtype = 2
                        break
                    elif type_line == '雨':
                        tqtype = 3
                        break
                    elif type_line == '雪':
                        tqtype = 4
                        break
                    elif type_line == '霜':
                        tqtype = 5
                        break
                    elif type_line == '露':
                        tqtype = 6
                        break
                    elif type_line == '雾':
                        tqtype = 7
                        break
                    elif type_line == '雷':
                        tqtype = 8
                        break
                    elif type_line == '晴':
                        tqtype = 9
                        break
                    elif type_line == '阴':
                        tqtype = 10
                        break
                url = "http://api.tianapi.com/txapi/tianqishiju/index?key=%s&tqtype=%d" % (key, tqtype)  # 申请地址https://www.tianapi.com/apiview/91
                res = requests.get(url).json()
                content = res['newslist'][0]['content']
                author = res['newslist'][0]['author']
                source = res['newslist'][0]['source']
            except:
                content = "东边日出西边雨,道是无晴却有晴"  # 万一请求失败,就随便来一句防止下面报错(、、、、)
                author = "刘禹锡"
                source = "竹枝词"
                print('天气诗句出错,代码为:')
                traceback.print_exc()
            return content, author, source

        for lmaki_data, lmaki in zip(forecast, ['今天', '明天']):  # 这你可以加个后天,来多请求几次。最多好像有10次?
            ymd = lmaki_data['ymd']  # 时间
            week = lmaki_data['week']  # 星期几
            sunrise = lmaki_data['sunrise']  # 日出
            sunset = lmaki_data['sunset']  # 日落
            high = lmaki_data['high']  # 最高温
            low = lmaki_data['low']  # 最低温
            fx = lmaki_data['fx']  # 风向
            fl = lmaki_data['fl']  # 几级风
            type_we = lmaki_data['type']  # 天气
            notice = lmaki_data['notice']  # 提示语
            content, author, source = poem(type_we)
            data = {
                'msg': f'@face=6@{lmaki}是{week},天气是 {type_we} 哦,温度为{wendu},湿度为{shidu}\npm2.5的值为{pm25},pm1.0的值是{pm10},{ganmao}\n'
                       f'{high},{low},{notice}。\n{content}--{author}\n吹的是{fx},风速达到了{fl}了。\n太阳将在 {sunrise} 升起,{sunset} 落下\n'
                       f'天气质量为 {quality}@face=13@   {ymd}   {city}'
            }
            post_data("天气", data)
    except:
        print('天气出错,代码为:')
        traceback.print_exc()


def earthy():
    """
    土味情话
    :return:
    """
    try:
        url = "http://api.tianapi.com/txapi/saylove/index?key=%s" % key  # 申请地址 https://www.tianapi.com/apiview/80
        rqs = requests.get(url).json()
        content = rqs["newslist"][0]["content"]
        data = {
            'msg': "%s\n@face=6@@face=6@@face=6@@face=6@" % content
        }
        post_data("土味情话", data)
    except:
        print('土味情话出错,代码为:')
        traceback.print_exc()


def zhen():
    """
    英语一句话
    :return:
    """
    try:
        url = "http://api.tianapi.com/txapi/ensentence/index?key=%s" % key  # 申请地址https://www.tianapi.com/apiview/62
        res = requests.get(url).json()
        content_zh = res['newslist'][0]['zh']
        content_en = res['newslist'][0]['en']
        data = {
            'msg': "%s@face=21@\n%s" % (content_en, content_zh)
        }
        post_data("英语一句话", data)
    except:
        print('英语一句话出错,代码为:')
        traceback.print_exc()


def poems():
    """
    古代情诗
    :return:
    """
    try:
        url = "http://api.tianapi.com/txapi/qingshi/index?key=%s" % key  # 申请地址https://www.tianapi.com/apiview/106
        res = requests.get(url).json()
        content = res['newslist'][0]['content']
        author = res['newslist'][0]['author']
        source = res['newslist'][0]['source']

        co = len(content.encode('utf-8')) - len(author.encode('utf-8')) - len(source.encode('utf-8'))
        data = {
            'msg': "%s@face=179@\n%s--%s《%s》" % (content, " " * co, author, source)
        }
        post_data("古代情诗", data)
    except:
        print('古代情诗出错,代码为:')
        traceback.print_exc()


def main():
    """
    主程序
    :return:
    """
    morning('wan')      # 这里改成zao就是早安,改成wan就是晚安
    earthy()
    weather(101210805)  # 改为你的城市代码
    zhen()
    poems()


if __name__ == '__main__':
    key = "???"  # 自己申请https://www.tianapi.com/,和上面的key通用,每天免费100次的调用,基本用不完
    main()



这个可以先拿来用用。下面的是腾讯云函数的,刚刚有很多人说报错,是因为云函数那个调用会传2个参数,然后没变量接受就报错了。。。。


需要把上面那个def main后的都删了,改成下面的就能正常调用了
测试可用,但没试时间触发。应该没问题



[Python] 纯文本查看 复制代码
def main(event,context):
    """
    主程序
    :return:
    """
    try:
        Tname=event["TriggerName"]     # 获取[color=rgba(0, 0, 0, 0.9)]触发器的名字
        if Tname=="zaoS" or Tname=="zaoF":      
            morning('zao')
            if Tname=="zaoS":            # 家和学校隔很远,在家就调用家那的天气,在学校就用学校那地的天气,可自己删
                weather(101210805)           # 城市代码
            elif Tname=="zaoF":
                weather(101210701)
        elif Tname=="wan":
            morning('wan')
        elif Tname=="twqh":
            earthy()
            zhen()
    except:
        print("直接调用测试")
        morning('wan')
        weather(101210805)
        earthy()
        zhen()


上面基本没变,就改了下运行方式。  
QQ截图20210428195218.png twqh为土味情话和英语一句话的调用,每2小时调用一次。wan就是睡觉时调用的


根据自己需求改



水到200积分真的累

免费评分

参与人数 7吾爱币 +7 热心值 +7 收起 理由
magic666 + 1 + 1 谢谢@Thanks!
取个好听的名字 + 1 + 1 哥 你这PyCharm 版本用的是哪一个 发我一份呗
hj170520 + 1 + 1 我很赞同!
kakos + 1 + 1 谢谢@Thanks!
ARtcgb + 1 + 1 用心讨论,共获提升!
领悟者的涂鸦笔 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
maple185 + 1 + 1 感谢作者对单身狗的关爱Q·Q

查看全部评分

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

kakos 发表于 2021-4-29 21:02
{"errorCode":-1,"errorMessage":"Traceback (most recent call last):\n  File \"/var/runtime/python3/bootstrap.py\", line 133, in init_handler\n    func_handler = get_func_handler(file.rsplit(\".\", 1)[0], func)\n  File \"/var/runtime/python3/bootstrap.py\", line 160, in get_func_handler\n    return getattr(mod, fname)\nAttributeError: module 'index' has no attribute 'main_handler'","requestId":"eeb3c0c6-e9f8-4b45-8a40-bc99f7d74545","statusCode":443}
报这个错该怎么处理啊?
还有if main里面该怎么写啊  报错
if __name__ == '__main__':
    key = "???"  # 自己申请https://www.tianapi.com/,
    main(event,context)
fenghong 发表于 2021-4-28 20:07
 楼主| 从零学习的歪经小 发表于 2021-4-28 20:13
yep96 发表于 2021-4-28 20:13
然而qmsg是要加其他账号的好友,除了通知推送别的没什么意义啊
wxbb979 发表于 2021-4-28 20:17
前来学习
maple185 发表于 2021-4-28 20:18
感谢作者对单身狗的关爱Q·Q
bigtitsboy 发表于 2021-4-28 20:28
牛大了哥
秦墨瑾 发表于 2021-4-28 20:34
感谢分享
扬帆大海 发表于 2021-4-28 20:47
我是来看接口的
choolyee 发表于 2021-4-28 21:00
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

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

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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