python推送文本

from webot import WechatBot
from hdfs.client import InsecureClient
from io import StringIO
import pandas as pd
import numpy as np
from tqdm import tqdm
from hdfs import HdfsError
import os
import pandas as pd
import matplotlib.pyplot as plt  
plt.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体 'SimHei'为黑体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
import matplotlib.pyplot as plt
from pandas.plotting import table


try:
    client = InsecureClient(url="http://nn2.ns12",user="admin")
    print('r1',client.list('/'))
except HdfsError as e:
    if "Operation category READ is not supported in state standby" in str(e):
        client = InsecureClient(url="http://nn1",user="admin")
        print('r2',client.list('/'))
    else:
        print("其他 HdfsError 异常:", e)


names=['pdate','sku','current_hour','success_membercnt','success_ordercnt','overavgorder_memberid_num','overavgorder_order_num',
       'yj_hb_success_membercnt','yj_hb_success_ordercnt',
       'yj_successordercnt','yj_successmembercnt','yj_avgorder',
       'cancel_membercnt','cancel_ordercnt','yj_cancelorder','xgj_yichang_order','xgj_yichang_order_rate','yj_xgj'
      ]
hdfs_file="/ns-traffic/tmp/mdapp_sku_ontime_yj_1214156_20240506"
df_data=pd.DataFrame(columns=names)
print(df_data)
for i in tqdm(client.list(hdfs_path=hdfs_file)):
    file_path=hdfs_file+'/'+i
    with client.read(file_path,encoding='utf-8', delimiter=' ') as reader:
        tmp=pd.read_csv(StringIO(' '.join(reader)), sep='x01', names=names)
    df_data=pd.concat([df_data,tmp],axis=0,ignore_index=True)
print(df_data)


 

hour=df_data.loc[0,'current_hour']
date=df_data.loc[0,'pdate']
str1 = f"截至{date}日{hour}点为止: 1.1 消费订单异常提升"
print(str1)
str2=""
abnormal = df_data.loc[df_data['yj_successordercnt'] == 1]
z=abnormal[['sku','success_ordercnt']]
has_output = False
for idx, row in z.iterrows():
    if row['sku'] and row['success_ordercnt']:
        str2= " "+f"注意:【{row['sku']}】消费订单异常提升,今日实时订单量【{row['success_ordercnt']}】"
        print(str2)
        has_output = True
if not has_output:
    str3= " "+"今日消费订单数正常"
    print(str3)
 


str4=" 1.2 消费人数异常提升"
str5=""
print(str4)
abnormal = df_data.loc[df_data['yj_successmembercnt'] == 1]
z = abnormal[['sku', 'success_membercnt']]
has_output = False
for idx, row in z.iterrows():
    if row['sku'] and row['success_membercnt']:  # 如果 'sku' 和 'success_membercnt' 都不为空
        str5= " "+f"注意:【{row['sku']}】消费人数异常提升,今日实时消费人数【{row['success_membercnt']}】"
        print(str5)
        has_output = True
if not has_output:
    str6= " "+"今日消费人数正常"
    print(str6)


    


str7=" 1.3 人均单量异常"
print(str7)
str8=""
abnormal =df_data.loc[df_data['yj_avgorder'] == 1]
z=abnormal[['sku','overavgorder_memberid_num','overavgorder_order_num']]
has_output = False
for idx, row in z.iterrows():
    if row['sku']:
        str8= " "+f"注意:【{row['sku']}】人均单量多,存在【{row['overavgorder_memberid_num']}】人已消费【{row['overavgorder_order_num']}】单"
        print(str8)
        has_output = True
if not has_output:
    str9= " "+"今日人均单量正常"
    print(str9) 

    

str10=" 1.4 取消量异常"
print(str10) 
str11=""
str12=""
abnormal =df_data.loc[df_data['yj_cancelorder'] == 1]
z=abnormal[['sku','cancel_membercnt','cancel_ordercnt']]
has_output=False
for idx, row in z.iterrows():
    if row['sku']:
        str11 += " "+f"注意:【{row['sku']}】取消量过多,存在【{row['cancel_membercnt']}】人已取消【{row['cancel_ordercnt']}】单"
        print(f"注意:【{row['sku']}】取消量过多,存在【{row['cancel_membercnt']}】人已取消【{row['cancel_ordercnt']}】单")
        has_output = True
if not has_output:
    str12 += " "+"今日取消量正常"
    print(str12) 
 
    
str13=" 1.5 非营业时间订单"
print(str1)
str14=""
abnormal = df_data.loc[(df_data['yj_xgj']==1) & (df_data['sku']=='公交')]
has_output = False
for idx,row in abnormal.iterrows():
    str14 =  " "+f"注意:非营业时间订单数: {row['xgj_yichang_order']}, 订单占比: {row['xgj_yichang_order_rate']}"
if not has_output:
    str15= " "+"今日非营业时间单量正常"
    print(str15) 

    

webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fdb7f28f-" #注意换成自己的机器人hook地址
bot = WechatBot(webhook_url) 

# 发送文本,并@all
bot.send_text(content=str1+(str2 if str2 else str3)
             +str4+(str5 if str5 else str6)
              +str7+(str8 if str8 else str9)
              +str10+(str11 if str11 else str12)   
              +str13+(str14 if str14 else str15)   
             )

关注公众号「水沐教育科技」,在手机上阅读所有教程,随时随地都能学习。内含一款搜索神器,免费下载全网书籍和视频。

公众号二维码
微信扫码关注公众号

微信交流群 关注微信公众号,加入官方交流群。内含一款搜索神器,免费下载全网书籍和视频。