Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. import threading
  2. import requests
  3. import json
  4. from dateutil import parser
  5. from time import sleep
  6. import time
  7.  
  8. a = 0
  9.  
  10. def worker(thread_name):
  11. global a
  12. global treatedApplications
  13. global treatedApplicationsIntermediaire
  14. global startTime
  15. global flagTimer
  16. while 1:
  17. #r = requests.get('https://api.staffomaticapp.com/v3/kerast/locations/26462/shifts.json', auth=('jb2.collet@gmail.com', 'jbbb1565'))
  18. r = requests.get('https://api.staffomaticapp.com/v3/kerast/locations/26462/shifts.json?from=2017-06-26T00%3A00%3A00%2B02%3A00&until=2017-08-01T00%3A00%3A00%2B02%3A00', auth=('solarderet@hotmail.fr', 'Lupercal01'))
  19. rJsonResponse = json.loads(r.text)
  20. if flagTimer == 1:
  21. startTime = int(round(time.time() * 1000))
  22. flagTimer = 0
  23. toTreat = -1
  24. for x in range(0, len(rJsonResponse)):
  25. for y in range(0, len(applications)):
  26. dtStart = applications[y]["shift"]["startsAt"]
  27. dtStart2 = rJsonResponse[x]["starts_at"]
  28.  
  29. dtEnd = applications[y]["shift"]["endsAt"]
  30. dtEnd2 = rJsonResponse[x]["ends_at"]
  31.  
  32.  
  33. if dtStart == dtStart2 and dtEnd == dtEnd2 and rJsonResponse[x]["department_id"] == applications[y]["shift"]["department"]["staffomaticId"] and rJsonResponse[x]["location_id"] == applications[y]["shift"]["department"]["location"]["staffomaticId"]:
  34.  
  35. if not applications[y]["id"] in treatedApplicationsIntermediaire:
  36. treatedApplicationsIntermediaire.append(applications[y]["id"])
  37. toTreat = rJsonResponse[x]["id"]
  38. user_id = 322176
  39. email = 'jb2.collet@gmail.com'
  40. password = "jbbb1565"
  41. r2 = requests.post('https://api.staffomaticapp.com/v3/kerast/shifts/'+str(toTreat)+'/applications.json', auth=(email, password), json={"user_id": user_id,"do": "assign"})
  42. r2Jsonresponse = json.loads(r2.text)
  43. if 'shift_id' not in r2Jsonresponse:
  44.  
  45. if '{"errors":{"user_id":["already exists"]}}' == r2.text :
  46. treatedApplications.append(applications[y]["id"])
  47. print "|treated - " + thread_name + " - " + str(int(round(time.time() * 1000)) - startTime) + " - " + str(r2.status_code) + "- " + str(toTreat) + "\n"
  48. else:
  49. treatedApplicationsIntermediaire.remove(applications[y]["id"])
  50.  
  51. else :
  52.  
  53. treatedApplications.append(applications[y]["id"])
  54. print "|treated - " + thread_name + " - " + str(int(round(time.time() * 1000)) - startTime) + " - " + str(r2.status_code) + "- " + str(toTreat) + "\n"
  55.  
  56.  
  57. #print r2.text
  58.  
  59.  
  60. for x in range(0, len(rJsonResponse)):
  61. for y in range(0, len(applications)):
  62. dtStart = applications[y]["shift"]["startsAt"]
  63. dtStart2 = rJsonResponse[x]["starts_at"]
  64.  
  65. dtEnd = applications[y]["shift"]["endsAt"]
  66. dtEnd2 = rJsonResponse[x]["ends_at"]
  67.  
  68.  
  69. if dtStart == dtStart2 and dtEnd == dtEnd2 and rJsonResponse[x]["department_id"] == applications[y]["shift"]["department"]["staffomaticId"] and rJsonResponse[x]["location_id"] == applications[y]["shift"]["department"]["location"]["staffomaticId"]:
  70. if not applications[y]["id"] in treatedApplications:
  71. treatedApplications.append(applications[y]["id"])
  72. toTreat = rJsonResponse[x]["id"]
  73. user_id = 322176
  74. email = 'jb2.collet@gmail.com'
  75. password = "jbbb1565"
  76. r2 = requests.post('https://api.staffomaticapp.com/v3/kerast/shifts/'+str(toTreat)+'/applications.json', auth=(email, password), json={"user_id": user_id,"do": "assign"})
  77. r2Jsonresponse = json.loads(r2.text)
  78. if 'shift_id' in r2Jsonresponse or '{"errors":{"user_id":["already exists"]}}' == r2.text:
  79. treatedApplications.append(applications[y]["id"])
  80. print "|treated FAILSAFE - " + thread_name + " - " + str(int(round(time.time() * 1000)) - startTime) + " - " + str(r2.status_code) + "- " + str(toTreat) + "\n"
  81.  
  82.  
  83.  
  84.  
  85.  
  86. a += 1
  87. print " " + str(a) + "/" + str(r.status_code) + "/" + "\n"
  88.  
  89.  
  90. allShifts = requests.get('https://api.staffomaticapp.com/v3/kerast/locations/26462/shifts.json?from=2017-06-19T00%3A00%3A00%2B02%3A00&until=2017-06-16T00%3A00%3A00%2B02%3A00', auth=('jb2.collet@gmail.com', 'jbbb1565'))
  91. allShiftsJsonResponse = json.loads(allShifts.text)
  92. initialShiftsCounter = len(allShiftsJsonResponse)
  93.  
  94.  
  95. with open('inputApplications.json') as data_file:
  96. applications = json.load(data_file)
  97.  
  98.  
  99. threads = []
  100. treatedApplications = []
  101. treatedApplicationsIntermediaire= []
  102. flagTimer = 1
  103. startTime = int(round(time.time() * 1000))
  104.  
  105. for x in range(0,10):
  106. sleep( 0.05 )
  107. t = threading.Thread(target=worker, args=(str(x)+"-thread",))
  108. threads.append(t)
  109. t.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement