main.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import pandas as pd
  2. import datetime as dt
  3. import pymysql
  4. pymysql.install_as_MySQLdb()
  5. import dataset
  6. import requests
  7. import gspread
  8. from gspread_pandas import Spread, Client
  9. from oauth2client.service_account import ServiceAccountCredentials
  10. import smtplib
  11. from userdata import *
  12. from email.message import EmailMessage
  13. def process_worksheet(ws, n):
  14. data = []
  15. r1 = ws.row_values(7)
  16. r2 = ws.row_values(8)
  17. data.append(r1)
  18. data.append(r2)
  19. f = pd.DataFrame(data)
  20. f.columns = f.iloc[0]
  21. f = f[1:]
  22. f.to_excel(c[0] + str(n) + " f.xlsx")
  23. print(f)
  24. return f
  25. def notify_group(message):
  26. gid='WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD'
  27. headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"}
  28. msg = [message[i:i+1000] for i in range(0, len(message), 1000)]
  29. print(msg)
  30. for m in msg:
  31. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": m})
  32. print(m)
  33. def send_email(message):
  34. server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  35. server.ehlo()
  36. server.login(gmail_username, gmail_password)
  37. emails = {"Jared": "jared@choozmo.com", "Morrison": "morrison@choozmo.com", "Wizer": "wizer@choozmo.com", "Doris": "doris@choozmo.com"}
  38. for name in emails:
  39. msg = EmailMessage()
  40. msg['Subject'] = '廣告績效通知' + str(dt.date.today())
  41. msg['From'] = "service@choozmo.com"
  42. msg['To'] = emails[name]
  43. msg.set_content(name + "您好,\n\n" + message)
  44. print ('Sending email to ' + msg['To'])
  45. #server.send_message(msg)
  46. print ('Email sent to ' + msg['To'])
  47. fname = name+'.txt'
  48. with open(fname, 'w', encoding="UTF+8") as f:
  49. f.write(name + "您好,\n\n" + message)
  50. server.close()
  51. scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
  52. credentials = ServiceAccountCredentials.from_json_keyfile_name("choozmointernal-2e314f3d4e12.json", scope)
  53. gc = gspread.authorize(credentials)
  54. clients = [['仁本', '1BZlXtEPD0OFdRRVYdTkXhHBcds6mhB0ILkB3ZB3EW3M', 'https://docs.google.com/spreadsheets/d/1BZlXtEPD0OFdRRVYdTkXhHBcds6mhB0ILkB3ZB3EW3M'], ['元利樂樺', '1jYPBgv2U0nnxN3WqI8X8tHcUon3GTMmnC8YrLFz1c84', 'https://docs.google.com/spreadsheets/d/1jYPBgv2U0nnxN3WqI8X8tHcUon3GTMmnC8YrLFz1c84']]
  55. fields = ["All conv.", "All conv. rate", "All conv. value", "Conversions", "Conv. rate", "Conv. value", "CTR", "Clicks", "Avg. CPC", "Impr.", "Cost", "Avg. cost"]
  56. msg = "以下是目前廣告績效:\n\n"
  57. for c in clients:
  58. msg += "[" + c[0] + "]\n詳細資料: " + c[2] + "\n\n"
  59. msg += "[昨日資料與前日比較: 昨日/前日/變化/變化%]\n"
  60. sh = gc.open_by_key(c[1])
  61. file = process_worksheet(sh.worksheet(c[0] + ' 昨日'), 1)
  62. for f in fields:
  63. f1 = f
  64. f2 = f + " (Compare to)"
  65. f3 = f + " (Change)"
  66. f4 = f + " (Change %)"
  67. msg += f + ": " + file[f1].values[0] + ' / ' + file[f2].values[0] + ' / ' + file[f3].values[0] + ' / ' + file[f4].values[0] + "\n"
  68. msg += "\n[累積資料]\n"
  69. file = process_worksheet(sh.worksheet(c[0] + ' 累積'), 2)
  70. for f in fields:
  71. msg += f + ": " + file[f].values[0] + "\n"
  72. msg += "\n"
  73. msg = msg.replace("#NUM!", "--")
  74. print(msg)
  75. notify_group(msg)
  76. send_email(msg)