jobcheck.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import requests
  2. import datetime
  3. from dateutil import parser
  4. import gspread
  5. from oauth2client.service_account import ServiceAccountCredentials
  6. import time
  7. import smtplib
  8. from userdata import *
  9. from email.message import EmailMessage
  10. # drop this at PUBLIC2
  11. # TASK LIST HERE
  12. # https://docs.google.com/spreadsheets/d/153w046qHhpnZTYU7FJ-S2xyGIH_BXbycLYfLI2fFsZc/edit#gid=0
  13. scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
  14. creds = ServiceAccountCredentials.from_json_keyfile_name('choozmointernal-2e314f3d4e12.json', scope)
  15. client = gspread.authorize(creds)
  16. message = "提醒近期預定執行的作業如下:\n\n"
  17. message2 = "提醒近期需完成的作業如下:\n\n"
  18. def notify_group(msg):
  19. gid='WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD'
  20. headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"}
  21. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": msg})
  22. def send_line(messages):
  23. for message in messages:
  24. msg = [message[i:i+1000] for i in range(0, len(message), 1000)]
  25. for m in msg:
  26. notify_group(m)
  27. print(m)
  28. def send_email(emails, content, has_overdue):
  29. server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  30. server.ehlo()
  31. server.login(gmail_username, gmail_password)
  32. for name in content:
  33. msg = EmailMessage()
  34. msg['Subject'] = '執行表單過期作業通知' + str(datetime.date.today())
  35. msg['From'] = "service@choozmo.com"
  36. msg['To'] = emails[name]
  37. if has_overdue[name] == 1:
  38. msg.set_content(content[name])
  39. else:
  40. msg.set_content(name + "您好,\n\n您目前沒有過期的作業。\n更新執行表單請至 https://cmm.ai/pm ,謝謝您。\n\n")
  41. print ('Sending email to ' + msg['To'])
  42. server.send_message(msg)
  43. print ('Email sent to ' + msg['To'])
  44. fname = name+'.txt'
  45. with open(fname, 'w', encoding="UTF+8") as f:
  46. if has_overdue[name] == 1:
  47. f.write(content[name])
  48. else:
  49. f.write(name + "您好,\n\n您目前沒有過期的作業。\n更新執行表單請至 https://cmm.ai/pm ,謝謝您。\n\n")
  50. server.close()
  51. sheet = client.open('ChoozMo 專案執行表單_20220720')
  52. sheet_instance = sheet.get_worksheet(0)
  53. records_data = sheet_instance.get_all_records()
  54. #print(records_data[0])
  55. emails = {"Jared": "jared@choozmo.com", "Morrison": "morrison@choozmo.com", "Ginia": "ginia@choozmo.com", "Doris": "doris@choozmo.com", "Nina": "nina.huang@choozmo.com", "Yukyo": "yukyo@choozmo.com", "Andy": "andy@choozmo.com", "Oak": "oak@choozmo.com", "Zooey": "zooey@choozmo.com", "Syuanyuri": "syuanyuri@choozmo.com", "Wizer": "wizer@choozmo.com", "Jason": "jason@choozmo.com", "Tomoya": "tomoya@choozmo.com", "詠心": "noodlesloves@gmail.com", "献竤": "fxp87257@gmail.com"}
  56. overdue_email_content = {}
  57. has_overdue = {}
  58. for e in emails.keys():
  59. overdue_email_content[e] = e + "您好,\n\n提醒您有已過期的作業,請儘快完成以下作業。\n更新執行表單請至 https://cmm.ai/pm ,謝謝您。\n\n過期作業如下:\n\n"
  60. has_overdue[e] = 0
  61. for data in records_data:
  62. try:
  63. print(parser.parse(data["起始日"]), parser.parse(data["完成日"]))
  64. deltastart = (parser.parse(data["起始日"]).date()-datetime.date.today()).days
  65. deltaend = (parser.parse(data["完成日"]).date()-datetime.date.today()).days
  66. print(deltastart, deltaend)
  67. info = ""
  68. if(deltastart <= 7 and data["完成度"] == "預定執行"):
  69. info += "專案:" + data["專案"] + " / " + data["專案項目"] + "\n"
  70. info += "作業:" + data["執行項目"] + "\n"
  71. info += "執行人員:" + data["指派對象"] + "\n"
  72. info += "PM:" + data["PM"] + "\n"
  73. info += "預定執行期間:" + data["起始日"] + "-" + data["完成日"]
  74. if(deltastart>0):
  75. info += " (尚有"+ str(deltastart) + "天)\n\n"
  76. elif(deltastart==0):
  77. info += " (當天開始執行)\n\n"
  78. else:
  79. info += " (過期"+ str(deltastart).replace('-','') + "天)\n\n"
  80. overdue_email_content[data["PM"]] += info
  81. has_overdue[data["PM"]] = 1
  82. if [data["PM"]]!=[data["指派對象"]]: #sometimes you have to do the task and you are the PM
  83. overdue_email_content[data["指派對象"]] += info
  84. has_overdue[data["指派對象"]] = 1
  85. if data["PM"] != "Jared": #sometimes Jared is the PM
  86. overdue_email_content["Jared"] += info
  87. has_overdue["Jared"] = 1
  88. message += info
  89. elif(deltaend <= 7 and data["完成度"] == "執行中"):
  90. info += "專案:" + data["專案"] + " / " + data["專案項目"] + "\n"
  91. info += "作業:" + data["執行項目"] + "\n"
  92. info += "執行人員:" + data["指派對象"] + "\n"
  93. info += "PM:" + data["PM"] + "\n"
  94. info += "完成日:" + data["完成日"]
  95. if(deltaend>0):
  96. info += " (尚有"+ str(deltaend) + "天)\n\n"
  97. elif(deltaend==0):
  98. info += " (當天完成)\n\n"
  99. else:
  100. info += " (過期"+ str(deltaend).replace('-','') + "天)\n\n"
  101. overdue_email_content[data["PM"]] += info
  102. has_overdue[data["PM"]] = 1
  103. if [data["PM"]]!=[data["指派對象"]]: #sometimes you have to do the task and you are the PM
  104. overdue_email_content[data["指派對象"]] += info
  105. has_overdue[data["指派對象"]] = 1
  106. if data["PM"] != "Jared": #sometimes Jared is the PM
  107. overdue_email_content["Jared"] += info
  108. has_overdue["Jared"] = 1
  109. message2 += info
  110. except:
  111. print("PASS")
  112. message += "更新執行表單請至 https://cmm.ai/pm"
  113. message2 += "更新執行表單請至 https://cmm.ai/pm"
  114. messages = []
  115. messages.append(message)
  116. messages.append(message2)
  117. print(has_overdue)
  118. send_line(messages)
  119. send_email(emails, overdue_email_content, has_overdue)
  120. '''notify_group(message)
  121. time.sleep(5)
  122. notify_group(message2)'''