BytedanceHotMaterial.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/12/20 11:18 上午
  4. # @Site :
  5. # @File : BytedanceHotMaterial.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 BytedanceHotMaterial:
  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 = """
  28. select t1.company_name AS '公司',
  29. round(t2.hot_cost, 3) AS '上线两周消耗',
  30. ifnull(t1.clip_name, '-') AS '剪辑',
  31. ifnull(t1.shot_name, '-') AS '拍摄',
  32. ifnull(t1.plan_name, '-') AS '编导',
  33. ifnull(t1.leader_name, '-') as '负责人',
  34. case
  35. when t1.material_innovate = 1 then '非创新素材'
  36. when t1.material_innovate = 2 then '创新素材'
  37. else '其它' end AS '是否创新素材',
  38. ifnull(t1.video_url, '-') AS '素材链接',
  39. ifnull(pro.project_name, '-') as '项目名称'
  40. from (select material_id,
  41. company_id,
  42. company_name,
  43. clip_name,
  44. shot_name,
  45. plan_name,
  46. leader_name,
  47. video_url,
  48. material_innovate
  49. from application.app_bytedance_material_info
  50. where faddish_date >= date_format(date_sub({stat_date}, INTERVAL 6 DAY), '%%Y%%m%%d') AND faddish_date<={stat_date}
  51. and company_id is not null) t1
  52. left join
  53. (select * from application.hot_material_two_week_cost where media_id = 1) t2
  54. on t1.material_id = t2.signature
  55. left join `jeecg-boot`.ctop_material_info info
  56. on t1.material_id = info.code
  57. left join `jeecg-boot`.ctop_project pro
  58. on info.project_id = pro.id
  59. order by hot_cost desc;
  60. """.format(stat_date=self.stat_date)
  61. print(sql)
  62. path = """/data/excel/bytedance_hot_material_{stat_date}.xls""".format(stat_date=self.stat_date)
  63. print(path)
  64. pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl')
  65. my_sender = 'renyupeng@c-top.com.cn' # 发件人邮箱账号
  66. my_pass = 'fcyJKkHUyPo6Zztw' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
  67. recipients = ['wanglei@c-top.com.cn',
  68. 'shiyuke@s-hcst.com',
  69. 'lihuan@c-top.com.cn'] # 收件人邮箱账号
  70. # content = "Please check the attachment. "
  71. msg = MIMEMultipart()
  72. # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
  73. msg['From'] = my_sender
  74. # [收件人邮箱昵称、收件人邮箱账号], 昵称随便
  75. msg['To'] = ",".join(recipients)
  76. # 邮件的主题,也就是邮件的标题
  77. msg['Subject'] = "头条-近一周爆款素材全明细"
  78. att1 = MIMEText(
  79. open(path, 'rb').read(), 'base64', 'utf-8')
  80. att1["Content-Type"] = 'application/octet-stream'
  81. att1["Content-Disposition"] = 'attachment; filename="bytedance_hot_material.xls"'
  82. msg.attach(att1)
  83. server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  84. server.login(my_sender, my_pass)
  85. try:
  86. server.sendmail(my_sender, recipients, msg.as_string())
  87. print("发送成功")
  88. except Exception as e:
  89. print("发送失败", e)
  90. if __name__ == '__main__':
  91. BytedanceHotMaterial().handler()