-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_writeTXT.py
More file actions
79 lines (66 loc) · 2.08 KB
/
Copy pathmain_writeTXT.py
File metadata and controls
79 lines (66 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import json
import sys
from excel_to_json import excel_to_json
from get_token import get_token
from elevate_token import elevate_token
from send_request import send_request
class Tee:
"""Writes output to both console and a file simultaneously."""
def __init__(self, filepath):
self.console = sys.stdout
self.file = open(filepath, "w", encoding="utf-8")
def write(self, message):
self.console.write(message)
self.file.write(message)
def flush(self):
self.console.flush()
self.file.flush()
def close(self):
self.file.close()
# Redirect stdout to Tee (console + file)
tee = Tee("output_log.txt")
sys.stdout = tee
try:
# Read Excel input, prepare JSON payloads
try:
excel_to_json()
except:
os._exit(1)
# Print out access to be granted for each user, for verification
sys.stdout = tee.console # Temporarily restore console only for input()
cont_input = input("Proceed with request (y/n)? ")
sys.stdout = tee # Resume dual output
if cont_input.lower() != "y":
print("Stopped")
os._exit(0)
print()
# Get access token
try:
access_token = get_token()
except Exception as err:
print("get_token responded with error: " + str(err))
os._exit(1)
print()
# Elevate token permissions
try:
access_token = elevate_token()
except Exception as err:
print("\nelevate_token responded with error: " + str(err))
os._exit(1)
print()
# Send JSON payloads sequentially
with open("json_requests.json", "r") as file:
data = json.load(file)
for payload in data:
print(payload["user"]["userName"])
response = send_request(payload)
if response.status_code == 200:
print("Success")
else:
print(response.json()["error"]["message"])
print()
print("Completed")
finally:
sys.stdout = tee.console # Always restore original stdout
tee.close() # Close the log file