send_bytedance_material_month.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 a.company_name AS '公司',
  28. a.project_name AS '项目',
  29. a.account_id AS '账户',
  30. a.signature AS '素材标识',
  31. b.media_time AS '上线时间',
  32. '-' 渠道,
  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.video_url AS '链接' from
  41. (select 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
  42. application.app_bytedance_video_report_busi_m a
  43. left join `jeecg-boot`.sys_user b
  44. on a.user_id=b.id
  45. where stat_mon=202201
  46. group by signature) a
  47. left join
  48. application.app_bytedance_material_info b
  49. on a.signature=b.material_id; """.format(stat_date=self.stat_date)
  50. print(sql)
  51. path = """/data/excel/send_bytedance_material_month.xls""".format(stat_date=self.stat_date)
  52. print(path)
  53. pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl')
  54. my_sender = 'zhaohailiang@c-top.com.cn' # 发件人邮箱账号
  55. my_pass = 'h27UwLdJcD797THk' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
  56. recipients = ['niuxiaoyu@c-top.com.cn'] # 收件人邮箱账号
  57. # content = "Please check the attachment. "
  58. msg = MIMEMultipart()
  59. # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
  60. msg['From'] = my_sender
  61. # [收件人邮箱昵称、收件人邮箱账号], 昵称随便
  62. msg['To'] = ",".join(recipients)
  63. # 邮件的主题,也就是邮件的标题
  64. msg['Subject'] = "头条-全量素材月报"
  65. att1 = MIMEText(
  66. open(path, 'rb').read(), 'base64', 'utf-8')
  67. att1["Content-Type"] = 'application/octet-stream'
  68. att1["Content-Disposition"] = 'attachment; filename="send_bytedance_material_month.xls"'
  69. msg.attach(att1)
  70. server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  71. server.login(my_sender, my_pass)
  72. try:
  73. server.sendmail(my_sender, recipients, msg.as_string())
  74. print("发送成功")
  75. except Exception as e:
  76. print("发送失败",e)
  77. if __name__ == '__main__':
  78. send_bytedance_material_month().handler()