NewKuaiShouAllMaterialFourD.py 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/12/20 11:18 上午
  4. # @Site :
  5. # @File : NewKuaiShouAllMaterialFourD.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 NewKuaiShouAllMaterialFourD:
  17. def __init__(self):
  18. self.OuterStr = "mysql+pymysql://%s:%s@%s:%d/%s" % (
  19. "hcst", parse.quote_plus("hcst@2024"), "192.168.0.249", 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 t2.company_name IS NOT NULL THEN t2.company_name ELSE '-' END) AS '公司',
  28. ifnull(t2.project_name, '-') AS '项目',
  29. ifnull(t2.signature, '-') AS '素材标识',
  30. (CASE
  31. WHEN t3.material_innovate = 1 THEN '非创新素材'
  32. WHEN t3.material_innovate = 2 THEN '创新素材'
  33. ELSE '-' END) AS '是否创新素材',
  34. ifnull(t1.frast_cost_time, '-') AS '上线时间',
  35. ifnull(t2.channel_name, '-') AS '渠道',
  36. sum(t2.charge) AS '消耗',
  37. ifnull(t2.clip_name, '-') AS '剪辑',
  38. ifnull(t2.shot_name, '-') AS '拍摄',
  39. ifnull(t2.plan_name, '-') AS '编导',
  40. ifnull(t2.leader_name, '-') AS '负责人',
  41. ifnull(t2.video_name, '-') AS '素材名称',
  42. ifnull(t2.photo_url, '-') AS '素材'
  43. from (select signature,frast_cost_time
  44. from application.material_farst_cost_time
  45. where frast_cost_time >= date_format(date_sub({stat_date}, INTERVAL 13 DAY), '%%Y%%m%%d') and frast_cost_time<={stat_date}) t1
  46. left join
  47. (select *
  48. from application.kuaishou_material_video_report_daily_dw
  49. where stat_date >= date_format(date_sub({stat_date}, INTERVAL 13 DAY), '%%Y%%m%%d')) t2
  50. on t1.signature = t2.signature
  51. left join `jeecg-boot`.ctop_material_info t3
  52. on t1.signature=t3.code
  53. where t2.signature is not null
  54. group by t1.signature;""".format(stat_date=self.stat_date)
  55. print(sql)
  56. path = """/data/excel/NewKuaiShouAllMaterialFourD_{stat_date}.xls""".format(stat_date=self.stat_date)
  57. print(path)
  58. pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl')
  59. my_sender = 'huichuangsituo@c-top.com.cn' # 发件人邮箱账号
  60. my_pass = 'rfZV5qRfendzM3Ph' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
  61. recipients = ['wanglei@c-top.com.cn',
  62. 'shiyuke@s-hcst.com',
  63. 'lihuan@c-top.com.cn'] # 收件人邮箱账号
  64. # content = "Please check the attachment. "
  65. msg = MIMEMultipart()
  66. # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
  67. msg['From'] = my_sender
  68. # [收件人邮箱昵称、收件人邮箱账号], 昵称随便
  69. msg['To'] = ",".join(recipients)
  70. # 邮件的主题,也就是邮件的标题
  71. msg['Subject'] = "快手-近14天上线素材消耗全明细"
  72. att1 = MIMEText(
  73. open(path, 'rb').read(), 'base64', 'utf-8')
  74. att1["Content-Type"] = 'application/octet-stream'
  75. att1["Content-Disposition"] = 'attachment; filename="NewKuaiShouAllMaterialFourD.xls"'
  76. msg.attach(att1)
  77. server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  78. server.login(my_sender, my_pass)
  79. try:
  80. server.sendmail(my_sender, recipients, msg.as_string())
  81. print("发送成功")
  82. except Exception as e:
  83. print("发送失败", e)
  84. if __name__ == '__main__':
  85. NewKuaiShouAllMaterialFourD().handler()