# Author renyupeng # coding=utf-8 # @Time : 2021/12/20 11:18 上午 # @Site : # @File : BytedanceHotMaterial.py # @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 BytedanceHotMaterial: def __init__(self): self.OuterStr = "mysql+pymysql://%s:%s@%s:%d/%s" % ( "hcst", parse.quote_plus("hcst@2024"), "192.168.0.249", 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 t1.company_name AS '公司', round(t2.hot_cost, 3) AS '上线两周消耗', ifnull(t1.clip_name, '-') AS '剪辑', ifnull(t1.shot_name, '-') AS '拍摄', ifnull(t1.plan_name, '-') AS '编导', ifnull(t1.leader_name, '-') as '负责人', case when t1.material_innovate = 1 then '非创新素材' when t1.material_innovate = 2 then '创新素材' else '其它' end AS '是否创新素材', ifnull(t1.video_url, '-') AS '素材链接', ifnull(pro.project_name, '-') as '项目名称' from (select material_id, company_id, company_name, clip_name, shot_name, plan_name, leader_name, video_url, material_innovate from application.app_bytedance_material_info where faddish_date >= date_format(date_sub({stat_date}, INTERVAL 6 DAY), '%%Y%%m%%d') AND faddish_date<={stat_date} and company_id is not null) t1 left join (select * from application.hot_material_two_week_cost where media_id = 1) t2 on t1.material_id = t2.signature left join `jeecg-boot`.ctop_material_info info on t1.material_id = info.code left join `jeecg-boot`.ctop_project pro on info.project_id = pro.id order by hot_cost desc; """.format(stat_date=self.stat_date) print(sql) path = """/data/excel/bytedance_hot_material_{stat_date}.xls""".format(stat_date=self.stat_date) print(path) pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl') my_sender = 'huichuangsituo@c-top.com.cn' # 发件人邮箱账号 my_pass = 'rfZV5qRfendzM3Ph' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码 recipients = ['wanglei@c-top.com.cn', 'shiyuke@s-hcst.com', 'lihuan@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="bytedance_hot_material.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__': BytedanceHotMaterial().handler()