send_bytedance_material_report_weekly.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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@2024"), "192.168.0.101", 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. WHEN a.clip_company_id = 'ace886fa51a244818e918b5d6d1bcc62' then
  41. '北京聚意创量文化传媒有限公司'
  42. when a.clip_company_id = '3f362f527a804e8a9db6552ea12f7574'
  43. then '石家庄汇创思拓数字科技有限公司'
  44. else '-'
  45. END) as '公司',
  46. a.project_name AS '项目',
  47. a.signature AS '素材标识',
  48. b.frast_cost_time AS '上线时间',
  49. '-' 渠道,
  50. a.charge AS '消耗',
  51. trim(a.clip_name) AS '剪辑',
  52. trim(a.shot_name) AS '拍摄',
  53. trim(a.plan_name) AS '编导',
  54. trim(a.leader_name) AS '负责人',
  55. shot_leader_name as `拍摄负责人`,
  56. a.video_name AS '素材名称',
  57. a.video_url AS '链接'
  58. from (select clip_company_id,
  59. shot_leader_name,
  60. company_name,
  61. project_name,
  62. signature,
  63. channel_name,
  64. sum(cost) charge,
  65. clip_name,
  66. shot_name,
  67. plan_name,
  68. a.leader_name,
  69. video_name,
  70. video_url
  71. from application.bytedance_material_video_report_daily_dw a
  72. where stat_datetime >= date_format(date_sub({stat_date}, INTERVAL 6 DAY), '%%Y%%m%%d')
  73. and stat_datetime <= {stat_date}
  74. group by signature) a
  75. left join
  76. application.bytedance_material_farst_cost_time b
  77. on a.signature = b.signature""".format(stat_date=self.stat_date)
  78. print(sql)
  79. path = """/data/excel/toutiao_material_week_{stat_date}.xls""".format(stat_date=self.stat_date)
  80. print(path)
  81. pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl')
  82. my_sender = 'renyupeng@c-top.com.cn' # 发件人邮箱账号
  83. my_pass = 'fcyJKkHUyPo6Zztw' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
  84. recipients = ['wanglei@c-top.com.cn',
  85. 'shiyuke@s-hcst.com',
  86. 'lihuan@c-top.com.cn'] # 收件人邮箱账号
  87. # content = "Please check the attachment. "
  88. msg = MIMEMultipart()
  89. # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
  90. msg['From'] = my_sender
  91. # [收件人邮箱昵称、收件人邮箱账号], 昵称随便
  92. msg['To'] = ",".join(recipients)
  93. # 邮件的主题,也就是邮件的标题
  94. msg['Subject'] = "头条-近一周素材消耗全明细"
  95. att1 = MIMEText(
  96. open(path, 'rb').read(), 'base64', 'utf-8')
  97. att1["Content-Type"] = 'application/octet-stream'
  98. att1["Content-Disposition"] = 'attachment; filename="bytedance_material_report_weekly.xls"'
  99. msg.attach(att1)
  100. server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  101. server.login(my_sender, my_pass)
  102. try:
  103. server.sendmail(my_sender, recipients, msg.as_string())
  104. print("发送成功")
  105. except Exception as e:
  106. print("发送失败", e)
  107. if __name__ == '__main__':
  108. send_material_report_weekly().handler()