288 lines
12 KiB
Python
288 lines
12 KiB
Python
import pandas as pd
|
||
import numpy as np
|
||
import glob, os, re, time
|
||
from datetime import datetime
|
||
from docx import Document
|
||
from docx.oxml.ns import qn
|
||
from docx.shared import Pt,RGBColor
|
||
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
||
import http.client
|
||
from urllib import parse
|
||
################
|
||
################
|
||
TEST = False # True为测试状态,不发短信; False为正式状态,发送短信。
|
||
################
|
||
################
|
||
|
||
dDate = {
|
||
'dateStart': '6月8日',
|
||
'dateEnd': '14日'
|
||
}
|
||
fn = 'D:/Projects/POM/DATA/2023年6月/6.16周预警/周预警_2023.6.15.xlsx'
|
||
outPath = 'D:/Projects/POM/DATA/2023年6月/6.16周预警/'
|
||
################
|
||
|
||
cities = {'白银市', '武威市',
|
||
'庆阳市',
|
||
'酒泉市',
|
||
'天水市',
|
||
'临夏回族自治州', '平凉市', '定西市', '定西市', '嘉峪关市',
|
||
'兰州新区','陇南市', '张掖市', '庆阳市宁县', '庆阳市镇原县', } #
|
||
|
||
cities = {'天水市', '平凉市', '定西市', '定西市', '嘉峪关市',
|
||
'兰州新区','陇南市', '张掖市', '庆阳市镇原县', } #
|
||
|
||
#cities = {'酒泉市'}
|
||
|
||
# 电话号码
|
||
contactsDWL = {
|
||
'szq': '13359446622',
|
||
'zyb': '13609346975',
|
||
'shx': '18089386522'
|
||
}
|
||
contacts = {
|
||
'天水市': {'王慧': '18706936366', '王肖肖': '17793816150'},
|
||
'白银市': {'高雅丽': '15393391905', '张静静': '13830021006'},
|
||
'定西市': {'张勇':'13993200605', '高刚': '18993265998'},
|
||
'酒泉市': {'吴建平': '13389370534'},
|
||
'临夏回族自治州': {'周世泽': '13830103221', '马清明': '13993012391', '马静': '13993096392'},
|
||
'平凉市': {'雷勇': '13809330195', '万朵': '15193383961'},
|
||
'武威市': {'马巨龙': '15379291530'}, #'孙彪': '17793551918'},
|
||
'嘉峪关市': {'彭松涛': '18893605128'},
|
||
'庆阳市': {'孙德勋': '13909342931', '闫红': '18993490882'},
|
||
#'庆阳市华池县': {'李保宁': '13739343092'},
|
||
'庆阳市宁县': {'zyb': '13609346975'},
|
||
'庆阳市镇原县': {'zyb': '13609346975'},#'刘主任': '13994327967', '王怡文': '18219942918'
|
||
'兰州新区': {'高天晓副主任':'13993685885', '刘玉明科长':'17726983336', '闫鹏':'15117091122', },
|
||
'陇南市': {'王军主任':'18093988558', '杨帅兵':'13830941310'},
|
||
'张掖市': {'张炜':'18993628432', '李伟璟':'13909365376'}
|
||
}
|
||
|
||
df = pd.read_excel(fn)
|
||
df.replace('\s+', '', regex=True, inplace=True)
|
||
df.loc[df['账号类型']=='微信服务号', '账号类型'] = '微信'
|
||
df.loc[df['账号类型']=='微信订阅号', '账号类型'] = '微信'
|
||
df['账号类型'] = df['账号类型'].str.replace(r'^其他\+','')
|
||
|
||
# SMS
|
||
sms_host = "sms.yunpian.com"
|
||
port = 443
|
||
sms_tpl_send_uri = "/v2/sms/tpl_single_send.json"
|
||
apikey = "304eb08353f7ebf00596737acfc31f53"
|
||
def tpl_send_sms(sms_host, port, sms_tpl_send_uri, apikey, tpl_id, tpl_value, mobile):
|
||
"""
|
||
模板接口发短信
|
||
"""
|
||
params = parse.urlencode({
|
||
'apikey': apikey,
|
||
'tpl_id': tpl_id,
|
||
'tpl_value': parse.urlencode(tpl_value),
|
||
'mobile': mobile
|
||
})
|
||
headers = {
|
||
"Content-type": "application/x-www-form-urlencoded",
|
||
"Accept": "text/plain"
|
||
}
|
||
conn = http.client.HTTPSConnection(sms_host, port=port, timeout=30)
|
||
conn.request("POST", sms_tpl_send_uri, params, headers)
|
||
response = conn.getresponse()
|
||
response_str = response.read()
|
||
conn.close()
|
||
return response_str
|
||
aa = 0
|
||
bb = 0
|
||
for city in cities:
|
||
print('~~~~~~~~~~~~~~~~~~~~~~~~')
|
||
dfC = df.loc[df['市/省局']==city].copy()
|
||
cc = dfC.shape[0]
|
||
if city == '庆阳市':
|
||
dfC = df.loc[(df['市/省局']==city)
|
||
& (df['区县/地方部门']!='华池县')
|
||
& (df['区县/地方部门']!='宁县')
|
||
& (df['区县/地方部门']!='镇原县')
|
||
].copy()
|
||
if city == '庆阳市宁县':
|
||
dfC = df.loc[(df['市/省局'] == '庆阳市')
|
||
& (df['区县/地方部门'] == '宁县')].copy()
|
||
cc = dfC.shape[0]
|
||
if city == '庆阳市华池县':
|
||
dfC = df.loc[(df['市/省局'] == '庆阳市')
|
||
& (df['区县/地方部门'] == '华池县')].copy()
|
||
cc = dfC.shape[0]
|
||
if city == '庆阳市镇原县':
|
||
dfC = df.loc[(df['市/省局'] == '庆阳市')
|
||
& (df['区县/地方部门'] == '镇原县')].copy()
|
||
cc = dfC.shape[0]
|
||
|
||
|
||
dfCU = dfC.loc[dfC['监测结果']!='合格'].copy()
|
||
|
||
warningText = '【甘肃大未来科技】政务新媒体监测预警:{}至{},监测{}政务新媒体账号{}个,更新频次和发布内容正常。'.format(dDate['dateStart'], dDate['dateEnd'], city, cc)
|
||
warningLists = []
|
||
if dfCU.shape[0] > 0:
|
||
warningText = '【甘肃大未来科技】政务新媒体监测预警:{}至{},{}被监测的{}个政务新媒体账号中,有{}个账号无更新,具体名单附后,请予以关注提醒。'.format(dDate['dateStart'], dDate['dateEnd'], city, cc, dfCU.shape[0])
|
||
print( warningText )
|
||
if dfCU.shape[0] > 0:
|
||
group = dfCU.groupby('账号类型')
|
||
for type, dfa in group:
|
||
astr = ''
|
||
for index, row in dfa.iterrows():
|
||
astr += row['账号名称'] + ', '
|
||
s = '{}({}个): {}'.format(type, dfa.shape[0], astr[:-2])
|
||
warningLists.append( s )
|
||
print(s)
|
||
print(' ')
|
||
|
||
# 生成总览文本
|
||
|
||
# 生成市州报告文本,存档
|
||
if 1:
|
||
|
||
doc = Document()
|
||
doc.styles['Normal'].font.name = u'宋体'
|
||
doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
|
||
doc.styles['Normal'].font.size = Pt(16)
|
||
doc.styles['Normal'].font.color.rgb = RGBColor(0, 0, 0)
|
||
p1 = doc.add_heading(city + '政务新媒体监测预警', 0)
|
||
p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||
p2 = doc.add_paragraph(warningText)
|
||
p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||
#p2.paragraph_format.left_indent = 406400
|
||
p2.paragraph_format.first_line_indent = 406400
|
||
for s in warningLists:
|
||
doc.add_paragraph(s)
|
||
|
||
doc.save(outPath + city + '.docx')
|
||
dfCU.shape[0]
|
||
|
||
# 发短信
|
||
if 1:
|
||
contacts[city].update(contactsDWL) # 向该市号码列表中添加大未来的号码
|
||
log = ''
|
||
sss = ''
|
||
for contact in contacts[city].keys():
|
||
log += contact + contacts[city][contact] + ', '
|
||
if dfCU.shape[0] > 0:
|
||
tpl_id = 4058906 # 【甘肃大未来科技】政务新媒体监测预警:#dateStart#至#dateEnd#,监测#city#政务新媒体账号#count#个,有#uq#个账号#problem#,未发现涉及敏感的错误内容。具体名单发至相关工作人员,请予以关注。
|
||
tpl_value = {'#dateStart#': dDate['dateStart'], '#dateEnd#': dDate['dateEnd'], '#city#': city,
|
||
'#count#': cc,
|
||
'#uq#': dfCU.shape[0], '#problem#': '未更新', }
|
||
sss = '【甘肃大未来科技】政务新媒体监测预警:' + dDate['dateStart'] + '至' + dDate[
|
||
'dateEnd'] + ',' + city + '被监测的' + str(cc) + '个政务新媒体账号中,有' \
|
||
+ str(dfCU.shape[0]) + '个账号' + '未更新' + ',未发现涉及敏感的错误内容。具体名单发至相关工作人员,请予以关注。'
|
||
if not TEST:
|
||
sss = tpl_send_sms(sms_host, port, sms_tpl_send_uri, apikey, tpl_id, tpl_value,
|
||
contacts[city][contact]).decode('utf-8')
|
||
else:
|
||
tpl_id = 4348890 # 【甘肃大未来科技】#dateStart#至#dateEnd#,监测#city#政务新媒体账号#amount#个,更新频次和发布内容正常。
|
||
tpl_value = {'#dateStart#': dDate['dateStart'], '#dateEnd#': dDate['dateEnd'], '#city#': city,
|
||
'#amount#': cc}
|
||
sss = '【甘肃大未来科技】政务新媒体监测预警:' + dDate['dateStart'] + '至' + dDate[
|
||
'dateEnd'] + ',监测' + city + '政务新媒体账号' + str(cc) + '个,更新频次和发布内容正常。'
|
||
if not TEST:
|
||
sss = tpl_send_sms(sms_host, port, sms_tpl_send_uri, apikey, tpl_id, tpl_value,
|
||
contacts[city][contact]).decode('utf-8')
|
||
|
||
# print(' sendSMS ', contact, contacts[city][contact], sss)
|
||
print("sendSMS:", sss)
|
||
print(" ", log[:-2])
|
||
|
||
#写出表格形式
|
||
if 1:
|
||
if city in ['陇南市']:
|
||
dfCU.loc[dfCU['区县/地方部门']=='', '区县/地方部门'] = '市直单位'
|
||
dfCU['区县/地方部门'] = dfCU['区县/地方部门'].fillna('市直单位')
|
||
|
||
# 按县区统计账号数量
|
||
dfLN = dfCU.groupby('区县/地方部门').agg({"账号名称":"count"})
|
||
# 按类型汇总账号名称
|
||
dfLNR = dfCU.groupby(['区县/地方部门','账号类型'])['账号名称'].apply(lambda x:x.str.cat(sep=', ')).reset_index()
|
||
# 统计各平台账号个数
|
||
dfLNS = dfCU.groupby(['账号类型']).agg({"账号名称":"count"})
|
||
|
||
print('-=-=-=')
|
||
print(dfLNS)
|
||
print('-=-=-=')
|
||
|
||
# 构建DataFrame
|
||
#types = list(dfCU['账号类型'].unique())
|
||
types = ['微信', '新浪微博', '今日头条', '抖音短视频']
|
||
l = ['区县', '未更新数'] + types
|
||
dfw = pd.DataFrame([],columns=l)
|
||
|
||
# 暂存数据
|
||
d = dict()
|
||
|
||
# 往暂存区写入县区名称和数量
|
||
for i, r in dfLN.iterrows():
|
||
d[i] = [r[0], '', '', '', '']
|
||
#d[i] = [r[0],] + types
|
||
|
||
# 区县/地方部门 账号类型 账号名称
|
||
#0 两当县 抖音短视频 陇南两当兴化乡
|
||
#1 两当县 新浪微博 陇南两当工信和商务, 陇南市两当地震
|
||
# 往暂存区分平台写入账号名称
|
||
for i, r in dfLNR.iterrows():
|
||
sCounty = r['区县/地方部门']
|
||
sType = r['账号类型']
|
||
sAccount = r['账号名称']
|
||
print(' ', sCounty, sType, sAccount)
|
||
d[sCounty][1 + types.index(sType)]=sAccount
|
||
|
||
print(d)
|
||
print('--')
|
||
# 从暂存区写入DataFrame
|
||
for k in d:
|
||
print(k, d[k][0], d[k][1], d[k][2], d[k][3], d[k][4])
|
||
dfw.loc[len(dfw)] = {'区县':k,'未更新数':d[k][0],types[0]:d[k][1],types[1]:d[k][2],types[2]:d[k][3],types[3]:d[k][4]}
|
||
# 调整输出表格列顺序
|
||
#order = ['区县', '未更新数', '微信', '新浪微博', '今日头条', '抖音短视频', ]
|
||
#dfw = dfw[order]
|
||
|
||
print('====')
|
||
|
||
# 增加 总计 行
|
||
if types[0] in dfLNS.index:
|
||
d0 = dfLNS.loc[types[0],'账号名称']
|
||
else:
|
||
d0 = 0
|
||
|
||
if types[1] in dfLNS.index:
|
||
d1 = dfLNS.loc[types[1],'账号名称']
|
||
else:
|
||
d1 = 0
|
||
|
||
if types[2] in dfLNS.index:
|
||
d2 = dfLNS.loc[types[2],'账号名称']
|
||
else:
|
||
d2 = 0
|
||
|
||
if types[3] in dfLNS.index:
|
||
d3 = dfLNS.loc[types[3],'账号名称']
|
||
else:
|
||
d3 = 0
|
||
|
||
|
||
dfw.loc[len(dfw)] = { '区县':'总 计', '未更新数':dfw['未更新数'].sum(),
|
||
types[0]:d0, types[1]:d1,
|
||
types[2]:d2, types[3]:d3 }
|
||
|
||
print('==-==', d0, d1, d2, d3)
|
||
|
||
# 写出, 添加标题
|
||
sFn = outPath + city + '周预警' + datetime.now().strftime('_%Y.%m.%d') + '.xlsx'
|
||
print('======== write to ', sFn)
|
||
writer = pd.ExcelWriter(sFn)
|
||
dfw.to_excel(writer, index=None, startrow=1) # , header=None
|
||
ws = writer.sheets['Sheet1']
|
||
title = '政务新媒体周预警未更新账号统计表({}-{})'.format(dDate['dateStart'], dDate['dateEnd'])
|
||
ws.write_string(0, 0, title)
|
||
writer.save()
|
||
#if city in ['陇南市']
|
||
|
||
#if 1 # 输出excel
|
||
|
||
aa += dfCU.shape[0]
|
||
bb += cc
|
||
print('----{}----({}/{})'.format(city, dfCU.shape[0], cc))
|
||
print('----{}----({}/{})'.format('ALL', aa, bb)) |