send_material_report_weekly.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/12/20 11:18 上午
  4. # @Site :
  5. # @File : send_material_report_weekly.py.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@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_material_report_weekly:
  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 when pro.agent_code=1 then '汇创'
  28. when pro.agent_code=2 then '骄阳'
  29. when pro.agent_code=3 then '华东' end AS '公司',
  30. ifnull(t1.project_name,'-') AS '项目',
  31. ifnull(t1.signature,'-') AS '素材标识',
  32. ifnull(t3.media_time,'-') AS '上线时间',
  33. ifnull(t1.channel_name,'-') AS '渠道',
  34. round(sum(t1.charge),3) AS '消耗',
  35. ifnull(t1.clip_name,'-') AS '剪辑',
  36. ifnull(t1.shot_name,'-') AS '拍摄',
  37. ifnull(t1.plan_name,'-') AS '编导',
  38. ifnull(t1.leader_name,'-') AS '负责人',
  39. case when t3.material_innovate=1 then '非创新素材'
  40. when t3.material_innovate=2 then '创新素材'
  41. else '其它' end AS '是否创新素材',
  42. ifnull(t1.video_name,'-') AS '素材名称',
  43. ifnull(t1.photo_url,'-') AS '链接'
  44. from application.kuaishou_material_video_report_daily_dw t1
  45. left join (select material_id,media_time,company_name,material_innovate from application.app_kuaishou_material_info) t3
  46. on t1.signature=t3.material_id
  47. left join `jeecg-boot`.user_company t2
  48. on t1.user_id=t2.user_id
  49. left join `jeecg-boot`.ctop_project pro
  50. on t1.project_id=pro.id
  51. where t1.stat_date between date_format(date_sub({stat_date}, INTERVAL 6 DAY),'%%Y%%m%%d') and {stat_date}
  52. group by t1.signature; """.format(stat_date=self.stat_date)
  53. print(sql)
  54. path = """/data/excel/material_week_{stat_date}.xls""".format(stat_date=self.stat_date)
  55. print(path)
  56. pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl')
  57. my_sender = 'renyupeng@c-top.com.cn' # 发件人邮箱账号
  58. my_pass = 'fcyJKkHUyPo6Zztw' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
  59. recipients = ['wanglei@c-top.com.cn', 'niuxiaoyu@c-top.com.cn', 'dukexuan@c-top.com.cn', 'yangyan@c-top.com.cn',
  60. 'zhangnannan@c-top.com.cn'] # 收件人邮箱账号
  61. # content = "Please check the attachment. "
  62. msg = MIMEMultipart()
  63. # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
  64. msg['From'] = my_sender
  65. # [收件人邮箱昵称、收件人邮箱账号], 昵称随便
  66. msg['To'] = ",".join(recipients)
  67. # 邮件的主题,也就是邮件的标题
  68. msg['Subject'] = "快手-全量素材周报"
  69. att1 = MIMEText(
  70. open(path, 'rb').read(), 'base64', 'utf-8')
  71. att1["Content-Type"] = 'application/octet-stream'
  72. att1["Content-Disposition"] = 'attachment; filename="material_report_weekly.xls"'
  73. msg.attach(att1)
  74. server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  75. server.login(my_sender, my_pass)
  76. try:
  77. server.sendmail(my_sender, recipients, msg.as_string())
  78. print("发送成功")
  79. except Exception as e:
  80. print("发送失败", e)
  81. if __name__ == '__main__':
  82. send_material_report_weekly().handler()