suzaomaterial.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 suzaomaterial:
  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 project_name,a.signature,frast_cost_time,video_name,photo_url from
  29. (select signature, frast_cost_time
  30. from application.material_farst_cost_time
  31. where frast_cost_time = date_format(date_sub(now(), interval 1 day), '%%Y%%m%%d')) a
  32. inner join(
  33. select signature,video_name,photo_url,project_name from application.kuaishou_material_video_report_daily_dw where stat_date= date_format(date_sub(now(), interval 1 day), '%%Y%%m%%d')
  34. and channel_type=1
  35. group by signature
  36. )b
  37. on a.signature=b.signature
  38. """.format(date=self.stat_date)
  39. print(sql)
  40. path = """/data/excel/塑造素材_{stat_date}.xls""".format(stat_date=self.stat_date)
  41. print(path)
  42. pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl')
  43. my_sender = 'renyupeng@c-top.com.cn' # 发件人邮箱账号
  44. my_pass = 'fcyJKkHUyPo6Zztw' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
  45. recipients = ['wanglei@c-top.com.cn'] # 收件人邮箱账号
  46. # content = "Please check the attachment. "
  47. msg = MIMEMultipart()
  48. # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
  49. msg['From'] = my_sender
  50. # [收件人邮箱昵称、收件人邮箱账号], 昵称随便
  51. msg['To'] = ",".join(recipients)
  52. # 邮件的主题,也就是邮件的标题
  53. msg['Subject'] = "塑造素材数据_{stat_date}".format(stat_date=self.stat_date)
  54. att1 = MIMEText(
  55. open(path, 'rb').read(), 'base64', 'utf-8')
  56. att1["Content-Type"] = 'application/octet-stream'
  57. att1["Content-Disposition"] = 'attachment; filename="SuZaoMaterial.xls"'
  58. msg.attach(att1)
  59. server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  60. server.login(my_sender, my_pass)
  61. try:
  62. server.sendmail(my_sender, recipients, msg.as_string())
  63. print("发送成功")
  64. except Exception as e:
  65. print("发送失败", e)
  66. if __name__ == '__main__':
  67. suzaomaterial().handler()