# Author renyupeng # coding=utf-8 # @Time : 2021/12/20 11:18 上午 # @Site : # @File : huaDongBytedanceMaterialReport # @Software: PyCharm # @contact: renyupeng@c-top.com.cn # @Tel 1501435553 import datetime from urllib import parse import pandas as pd import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from sqlalchemy import create_engine class send_material_report_weekly: def __init__(self): self.OuterStr = "mysql+pymysql://%s:%s@%s:%d/%s" % ( "hcst", parse.quote_plus("hcst@2021"), "192.168.0.184", 3390, "application") self.OuterConn = create_engine(self.OuterStr, pool_size=10, max_overflow=5, pool_timeout=10, pool_recycle=3600) self.stat_date = (datetime.date.today() + datetime.timedelta(-1)).strftime("%Y%m%d") def handler(self): sql = """ select ifnull(stat_datetime,0) as '日期', ifnull(video.company_name,report.company_name) as '公司', ifnull(report.project_name,'-') AS '项目', ifnull(report.signature,'-') AS '素材标识', ifnull(video.farst_cost_time,'-') AS '上线时间', ifnull(report.channel_name,'-') AS '渠道', case when video.material_innovate=1 then '非创新素材' when video.material_innovate=2 then '创新素材' else '其它' end AS '是否创新素材', ifnull(sum(report.cost),0) AS '消耗', ifnull(report.clip_name,'-') AS '剪辑', ifnull(report.shot_name,'-') AS '拍摄', ifnull(report.plan_name,'-') AS '编导', ifnull(report.leader_name,'-') AS '负责人', ifnull(report.video_name,'-') AS '素材名称', ifnull(concat('http://i.snssdk.com/video/code/1/toutiao/',info.id),'-') AS '素材' from (select t1.stat_datetime, t1.account_id, t2.company_name, t1.material_id, t1.project_name , t1.signature, t1.channel_name, t1.cost, t1.clip_name, t1.shot_name, t1.plan_name, t1.leader_name, t1.video_name, t1.video_url from application.bytedance_material_video_report_daily_dw t1 left join `jeecg-boot`.user_company t2 on t1.user_id=t2.user_id WHERE stat_datetime between concat(date_format(date_sub(now(),interval 1 day),'%%Y%%m'),'01') and date_format(date_sub(now(),interval 1 day),'%%Y%%m%%d') AND t2.company_id = '4b10089775c040119e139087517aed88') report left join application.app_bytedance_material_info video on report.signature=video.material_id left join ( select id, signature from media_api.bytedance_file_video_get group by signature )info on report.signature=info.signature group by report.signature,stat_datetime;""".format(stat_date=self.stat_date) print(sql) path = """/data/excel/huaDongBytedanceMaterial.xls""".format(stat_date=self.stat_date) print(path) pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl') my_sender = 'renyupeng@c-top.com.cn' # 发件人邮箱账号 my_pass = 'fcyJKkHUyPo6Zztw' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码 recipients = ['panyuxia@c-top.com.cn', 'daipeng@c-top.com.cn'] # 收件人邮箱账号 # content = "Please check the attachment. " msg = MIMEMultipart() # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便 msg['From'] = my_sender # [收件人邮箱昵称、收件人邮箱账号], 昵称随便 msg['To'] = ",".join(recipients) # 邮件的主题,也就是邮件的标题 msg['Subject'] = "头条-全量素材周报" att1 = MIMEText( open(path, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = 'attachment; filename="HuaDongMaterial.xls"' msg.attach(att1) server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465) server.login(my_sender, my_pass) try: server.sendmail(my_sender, recipients, msg.as_string()) print("发送成功") except Exception as e: print("发送失败", e) if __name__ == '__main__': send_material_report_weekly().handler()