send_material_report_month.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2022/02/07 14:43 下午
  4. # @Site :
  5. # @File : send_material_report_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_material_report_month:
  17. def __init__(self):
  18. self.OuterStr = "mysql+pymysql://%s:%s@%s:%d/%s" % (
  19. "data", parse.quote_plus("hcst@2021"), "139.186.27.96", 3390, "jeecg-boot")
  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 a.company_name AS '公司',
  28. a.project_name AS '项目',
  29. a.account_id AS '账户',
  30. a.signature AS '素材标识',
  31. b.media_time AS '上线时间',
  32. a.channel_name '渠道',
  33. a.charge AS '消耗',
  34. a.user_name AS '运营',
  35. a.clip_name AS '剪辑',
  36. a.shot_name AS '拍摄',
  37. a.plan_name AS '编导',
  38. a.leader_name AS '负责人',
  39. a.video_name AS '素材名称',
  40. a.photo_url AS '链接'
  41. from
  42. (select company_name,project_name,account_id,signature,channel_name,sum(charge)charge,clip_name,shot_name,plan_name,a.leader_name,video_name,photo_url,b.realname user_name from
  43. application.app_kuaishou_video_report_busi_m a
  44. left join `jeecg-boot`.sys_user b
  45. on a.user_id=b.id
  46. where stat_mon=202201
  47. group by account_id,signature) a
  48. left join
  49. application.app_kuaishou_material_info b
  50. on a.signature=b.material_id; """.format(stat_date=self.stat_date)
  51. print(sql)
  52. path = """/data/excel/kuaishou_material_month.xls""".format(stat_date=self.stat_date)
  53. print(path)
  54. pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl')
  55. my_sender = 'zhaohailiang@c-top.com.cn' # 发件人邮箱账号
  56. my_pass = 'h27UwLdJcD797THk' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
  57. recipients = ['niuxiaoyu@c-top.com.cn'] # 收件人邮箱账号
  58. # content = "Please check the attachment. "
  59. msg = MIMEMultipart()
  60. # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
  61. msg['From'] = my_sender
  62. # [收件人邮箱昵称、收件人邮箱账号], 昵称随便
  63. msg['To'] = ",".join(recipients)
  64. # 邮件的主题,也就是邮件的标题
  65. msg['Subject'] = "快手-全量素材周报"
  66. att1 = MIMEText(
  67. open(path, 'rb').read(), 'base64', 'utf-8')
  68. att1["Content-Type"] = 'application/octet-stream'
  69. att1["Content-Disposition"] = 'attachment; filename="material_report_weekly.xls"'
  70. msg.attach(att1)
  71. server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  72. server.login(my_sender, my_pass)
  73. try:
  74. server.sendmail(my_sender, recipients, msg.as_string())
  75. print("发送成功")
  76. except Exception as e:
  77. print("发送失败",e)
  78. if __name__ == '__main__':
  79. send_material_report_month().handler()