send_bytedance_material_month.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2022/02/07 14:43 下午
  4. # @Site :
  5. # @File : send_bytedance_material_month.py
  6. # @Software: PyCharm
  7. # @contact: zhaohailiang@c-top.com.cn
  8. # @Tel 1501435553
  9. import datetime
  10. from urllib import parse
  11. import pandas as pd
  12. import smtplib
  13. from email.mime.text import MIMEText
  14. from email.mime.multipart import MIMEMultipart
  15. from sqlalchemy import create_engine
  16. class send_bytedance_material_month:
  17. def __init__(self):
  18. self.OuterStr = "mysql+pymysql://%s:%s@%s:%d/%s" % (
  19. "hcst", parse.quote_plus("hcst@2021"), "192.168.0.184", 3390, "application")
  20. self.OuterConn = create_engine(self.OuterStr,
  21. pool_size=10,
  22. max_overflow=5,
  23. pool_timeout=10,
  24. pool_recycle=3600)
  25. self.stat_date = (datetime.date.today() + datetime.timedelta(-1)).strftime("%Y-%m-%d")
  26. def handler(self):
  27. sql = """select (CASE
  28. WHEN a.clip_company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
  29. '北京汇创思拓数字科技有限公司'
  30. WHEN a.clip_company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
  31. '广州汇创思拓数字科技有限公司'
  32. WHEN a.clip_company_id = '4b10089775c040119e139087517aed88' THEN
  33. '上海汇创思拓数字科技有限公司'
  34. WHEN a.clip_company_id = '5e46b5dea3d740eeb8ff1a2895015a4b' THEN
  35. '北京骄阳数字科技有限公司'
  36. WHEN a.clip_company_id = '1648c7ceb4d449a9a13fbed7a8d9976c' THEN
  37. '杭州妙构思数字科技有限公司'
  38. WHEN a.clip_company_id = '3321d5893bcd430cab1238a99bc7db11' THEN
  39. '北京次元像素'
  40. else '-'
  41. END) as '公司',
  42. a.project_name AS '项目',
  43. concat('`',a.account_id,'`') AS '账户',
  44. a.signature AS '素材标识',
  45. b.media_time AS '上线时间',
  46. '-' 渠道,
  47. a.charge AS '消耗',
  48. a.user_name AS '运营',
  49. a.clip_name AS '剪辑',
  50. a.shot_name AS '拍摄',
  51. a.plan_name AS '编导',
  52. a.leader_name AS '负责人',
  53. a.video_name AS '素材名称',
  54. a.video_url AS '链接' from
  55. (select clip_company_id,account_id,company_name,project_name,signature,channel_name,sum(cost)charge,clip_name,shot_name,plan_name,a.leader_name,video_name,video_url,b.realname user_name from
  56. application.app_bytedance_video_report_busi_m a
  57. left join `jeecg-boot`.sys_user b
  58. on a.user_id=b.id
  59. where stat_mon=202201
  60. group by signature) a
  61. left join
  62. application.app_bytedance_material_info b
  63. on a.signature=b.material_id; """.format(stat_date=self.stat_date)
  64. print(sql)
  65. path = """/data/excel/send_bytedance_material_month.xls""".format(stat_date=self.stat_date)
  66. print(path)
  67. pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl')
  68. my_sender = 'zhaohailiang@c-top.com.cn' # 发件人邮箱账号
  69. my_pass = 'h27UwLdJcD797THk' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
  70. recipients = ['niuxiaoyu@c-top.com.cn'] # 收件人邮箱账号
  71. # content = "Please check the attachment. "
  72. msg = MIMEMultipart()
  73. # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
  74. msg['From'] = my_sender
  75. # [收件人邮箱昵称、收件人邮箱账号], 昵称随便
  76. msg['To'] = ",".join(recipients)
  77. # 邮件的主题,也就是邮件的标题
  78. msg['Subject'] = "头条-全量素材月报"
  79. att1 = MIMEText(
  80. open(path, 'rb').read(), 'base64', 'utf-8')
  81. att1["Content-Type"] = 'application/octet-stream'
  82. att1["Content-Disposition"] = 'attachment; filename="send_bytedance_material_month.xls"'
  83. msg.attach(att1)
  84. server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  85. server.login(my_sender, my_pass)
  86. try:
  87. server.sendmail(my_sender, recipients, msg.as_string())
  88. print("发送成功")
  89. except Exception as e:
  90. print("发送失败",e)
  91. if __name__ == '__main__':
  92. send_bytedance_material_month().handler()