jobcheck.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. # drop this at PUBLIC2
  8. # TASK LIST HERE
  9. # https://docs.google.com/spreadsheets/d/153w046qHhpnZTYU7FJ-S2xyGIH_BXbycLYfLI2fFsZc/edit#gid=0
  10. scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
  11. creds = ServiceAccountCredentials.from_json_keyfile_name('choozmointernal-2e314f3d4e12.json', scope)
  12. client = gspread.authorize(creds)
  13. message = "提醒近期預定執行的作業如下:\n\n"
  14. message2 = "提醒近期需完成的作業如下:\n\n"
  15. def notify_group(msg):
  16. gid='WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD'
  17. headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"}
  18. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": msg})
  19. sheet = client.open('ChoozMo 專案執行表單_20220720')
  20. sheet_instance = sheet.get_worksheet(0)
  21. records_data = sheet_instance.get_all_records()
  22. #print(records_data[0])
  23. for data in records_data:
  24. try:
  25. print(parser.parse(data["完成日"]))
  26. print(datetime.date.today())
  27. deltastart = (parser.parse(data["起始日"]).date()-datetime.date.today()).days
  28. deltaend = (parser.parse(data["完成日"]).date()-datetime.date.today()).days
  29. print(deltastart, deltaend)
  30. if(deltastart <= 7 and data["完成度"] == "預定執行"):
  31. message += "專案:" + data["專案"] + "\n"
  32. message += "專案項目:" + data["專案項目"] + "\n"
  33. message += "作業:" + data["執行項目"] + "\n"
  34. message += "執行人員:" + data["指派對象"] + "\n"
  35. message += "PM:" + data["PM"] + "\n"
  36. message += "執行期間:" + data["起始日"] + "-" + data["完成日"]
  37. if(deltastart>0):
  38. message += " (尚有"+ str(deltastart) + "天)\n\n"
  39. elif(deltastart==0):
  40. message += " (當天開始執行)\n\n"
  41. else:
  42. message += " (過期"+ str(deltastart).replace('-','') + "天)\n\n"
  43. elif(deltaend <= 7 and data["完成度"] == "執行中"):
  44. message2 += "專案:" + data["專案"] + "\n"
  45. message2 += "專案項目:" + data["專案項目"] + "\n"
  46. message2 += "作業:" + data["執行項目"] + "\n"
  47. message2 += "執行人員:" + data["指派對象"] + "\n"
  48. message2 += "PM:" + data["PM"] + "\n"
  49. message2 += "完成日:" + data["完成日"]
  50. if(deltaend>0):
  51. message2 += " (尚有"+ str(deltaend) + "天)\n\n"
  52. elif(deltaend==0):
  53. message2 += " (當天完成)\n\n"
  54. else:
  55. message2 += " (過期"+ str(deltaend).replace('-','') + "天)\n\n"
  56. except:
  57. print("PASS")
  58. msg1 = [message[i:i+1000] for i in range(0, len(message), 1000)]
  59. for m1 in msg1:
  60. #notify_group(m1) #annotated to prevent unintentional trigger
  61. print(m1)
  62. msg2 = [message2[i:i+1000] for i in range(0, len(message2), 1000)]
  63. for m2 in msg2:
  64. #notify_group(m2)
  65. print(m2)
  66. '''notify_group(message)
  67. time.sleep(5)
  68. notify_group(message2)'''