# coding=utf-8 import pandas as pd import yagmail import requests import arrow import numpy as np import matplotlib as mpl from matplotlib.font_manager import _rebuild _rebuild() # reload一下 mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['font.serif'] = ['SimHei'] now = arrow.now() start_time = now.format("YYYY-MM-DD") end_time = now.shift(days=7).format("YYYY-MM-DD") filter = '''[{"cxxx"}]''' % ( start_time, end_time) brand_url = 'hxxx} headers = {xxx } def get_brand_info(tar): visible_flag = True info = requests.post(brand_url, data=data, headers=headers) df_data = info.json()['data']['records'] df = pd.DataFrame(df_data) df_8 = df[df['ad_zone_id'] == 8] df_105 = df[df['ad_zone_id'] == 105] df_8 = df_8[['ptdate', 'fill_rate']] df_105 = df_105[['ptdate', 'fill_rate']] df_8['预xxx级'] = pd.cut(df_8['fill_rate'], bins=[0, 0.05, 0.10, 0.15, np.inf], labels=['空闲', '适中', '紧张', '非常紧张']) df_105['预xxx级'] = pd.cut(df_105['fill_rate'], bins=[0, 0.05, 0.10, 0.15, np.inf], labels=['空闲', '适中', '紧张', '非常紧张']) df_8.rename(columns={'ptdate': '日期', 'fill_rate': '品xxxx率'}, inplace=True) df_105.rename(columns={'ptdate': '日期', 'fill_rate': '品xxx率'}, inplace=True) if tar == 'inner': ax_8 = df_8.plot('日期', '品xxxx率', kind='bar', title='首页未来xxxx图', rot=30, color='#0f88eb') vals_8 = ax_8.get_yticks() ax_8.set_yticklabels(['{:,.2%}'.format(x) for x in vals_8]) ax_8.set_xticklabels(df_8['日期'].tolist()) for i, v in enumerate(df_8['品xxx率'].tolist()): ax_8.text(i, v + 0.002, str(df_8['预xx级'].tolist()[i]), ha='center', fontweight='bold') ax_8.yaxis.set_visible(visible_flag) fig_8 = ax_8.get_figure() fig_8.savefig('fig8_inner.png') ax_105 = df_105.plot('日期', '品牌填充率', kind='bar', title='回xxxx图', rot=30, color='#0f88eb') vals_105 = ax_105.get_yticks() ax_105.set_yticklabels(['{:,.2%}'.format(x) for x in vals_105]) ax_105.set_xticklabels(df_105['日期'].tolist()) for index, value in enumerate(df_105['品xx率'].tolist()): ax_105.text(index, value + 0.002, str(df_105['预xxx等级'].tolist()[index]), ha='center', fontweight='bold') ax_105.yaxis.set_visible(visible_flag) fig_105 = ax_105.get_figure() fig_105.savefig('fig105_inner.png') df_8['品xxxx充率'] = round(df_8['品xxxx率'] * 100, 2).apply(str) + '%' df_105['xxxx率'] = round(df_105['品xxx率'] * 100, 2).apply(str) + '%' if tar == 'outter': visible_flag = False ax_8 = df_8.plot('日期', '品xxxx充率', kind='bar', title='首xxxxx图', rot=30, color='#0f88eb') vals_8 = ax_8.get_yticks() ax_8.set_yticklabels(['{:,.2%}'.format(x) for x in vals_8]) ax_8.set_xticklabels(df_8['日期'].tolist()) for i, v in enumerate(df_8['品xxxx率'].tolist()): ax_8.text(i, v + 0.002, str(df_8['预xxx级'].tolist()[i]), ha='center', fontweight='bold') ax_8.yaxis.set_visible(visible_flag) fig_8 = ax_8.get_figure() fig_8.savefig('fig8_outter.png') ax_105 = df_105.plot('日期', '品xxx率', kind='bar', title='回xxxx量走势图', rot=30, color='#0f88eb') vals_105 = ax_105.get_yticks() ax_105.set_yticklabels(['{:,.2%}'.format(x) for x in vals_105]) ax_105.set_xticklabels(df_105['日期'].tolist()) for index, value in enumerate(df_105['品xxxx率'].tolist()): ax_105.text(index, value + 0.002, str(df_105['xxx级'].tolist()[index]), ha='center', fontweight='bold') ax_105.yaxis.set_visible(visible_flag) fig_105 = ax_105.get_figure() fig_105.savefig('fig105_outter.png') df_8.drop('品xxxxx率', axis=1, inplace=True) df_105.drop('品xxx率', axis=1, inplace=True) all_html_8 = df_8.to_html(escape=False, index=False).replace("\n", "") all_html_105 = df_105.to_html(escape=False, index=False).replace("\n", "") body = \ """您好,以下为xxxx占量情况!
""".format(all_html_8, tar,all_html_105,tar) head = \ ''' ''' foot = \ '''近一xxxxxx量数据
{}
近xxx据
{}
—— 本次报告完 ——
''' html_msg = "" + head + body + foot + "" html_msg = html_msg.replace("\n", "") with open('./' + tar + '.html', 'w', encoding='UTF-8', newline='') as fout: fout.write(html_msg) print(df_8.head()) print(df_105.head()) def send_week_mail(tar): yag = yagmail.SMTP(user="[email protected]", password="cxxxx", host='smtp.xxxx.com') print('正在准xxxx...') if tar == 'inner': # 链接邮箱服务器 # zhihu_staff = [xxx] yag.send(to=zhihu_staff, subject='未来xxxx页占量', contents=open('./' + tar + '.html', 'r', encoding='UTF-8', newline='').read()) print('内部xxxx发送!') else: # agent_staff = ['cxxx'] agent_staff = ['zxxxx'] yag.send(to=agent_staff, subject='未来xxxxx页占量', contents=open('./' + tar + '.html', 'r', encoding='UTF-8', newline='').read()) print('外部每日xxxx发送!') def main(): target = ['inner', 'outter'] for tar in target: get_brand_info(tar) send_week_mail(tar) if __name__ == '__main__': main()