200 lines
6.4 KiB
Python
200 lines
6.4 KiB
Python
import os, requests, time, json, sys, logging, configparser
|
|
from datetime import datetime, timedelta
|
|
from http.cookies import SimpleCookie
|
|
from pathlib import Path
|
|
|
|
def checkAPIlimit():
|
|
global number_of_calls
|
|
print("API calls: ", str(number_of_calls+1))
|
|
if number_of_calls == 59:
|
|
print("Sleeping")
|
|
time.sleep(60)
|
|
number_of_calls=0
|
|
|
|
def saveValues():
|
|
YOUR_DT_API_URL = tenant_info.split(" ")[0].rstrip('\n')
|
|
YOUR_DT_API_TOKEN = tenant_info.split(" ")[1].rstrip('\n')
|
|
|
|
return YOUR_DT_API_URL, YOUR_DT_API_TOKEN
|
|
|
|
|
|
def getList():
|
|
print('Iterate through list? y/n')
|
|
iterate = input()
|
|
if iterate == 'y':
|
|
print('Enter the key of the object:')
|
|
jsonKey = input()
|
|
else:
|
|
jsonKey = None
|
|
return iterate, jsonKey
|
|
|
|
def download():
|
|
#Create /data folder
|
|
endpointLast = endpoint
|
|
if "/" in endpoint:
|
|
endpointLast = endpoint.split("/")[-1]
|
|
directory="data/"+env_id+"/"+endpointLast
|
|
Path(directory).mkdir(parents=True, exist_ok=True)
|
|
|
|
print(''.join(YOUR_DT_API_URL) + endpoint)
|
|
r = requests.get(''.join(YOUR_DT_API_URL) + endpoint, headers=headers, cookies=dict(cookies), verify=False);
|
|
|
|
data_json = json.loads(r.text)
|
|
iterate = False
|
|
for key in data_json.keys():
|
|
if type(data_json[key]) is list:
|
|
if "id" in data_json[key][0]:
|
|
iterate = True
|
|
iterative_key = key
|
|
|
|
if iterate == True:
|
|
i=0
|
|
while i<len(data_json[iterative_key]):
|
|
# mobile application id
|
|
id_rule = data_json[iterative_key][i]['id']
|
|
|
|
# Get mobile application config
|
|
rule = requests.get(''.join(YOUR_DT_API_URL) + endpoint +"/"+id_rule, headers=headers, cookies=dict(cookies), verify=False);
|
|
|
|
mobile_directory="data/"+env_id+"/"+endpointLast+"/"+id_rule
|
|
Path(mobile_directory).mkdir(parents=True, exist_ok=True)
|
|
|
|
# Save mobile application config
|
|
f = open(mobile_directory+"/"+id_rule+".json", "w")
|
|
json_data = json.loads(rule.text)
|
|
json_formatted_str = json.dumps(json_data, indent=2)
|
|
f.write(json_formatted_str)
|
|
f.close()
|
|
|
|
kua_directory="data/"+env_id+"/"+endpointLast+"/"+id_rule+"/userActionAndSessionProperties"
|
|
Path(kua_directory).mkdir(parents=True, exist_ok=True)
|
|
|
|
# Get userActionAndSessionProperties
|
|
kua = requests.get(''.join(YOUR_DT_API_URL) + endpoint +"/"+id_rule+"/userActionAndSessionProperties", headers=headers, cookies=dict(cookies), verify=False);
|
|
# Save mobile application config
|
|
f = open(kua_directory+"/userActionAndSessionProperties.json", "w")
|
|
json_data = json.loads(kua.text)
|
|
json_formatted_str = json.dumps(json_data, indent=2)
|
|
f.write(json_formatted_str)
|
|
f.close()
|
|
|
|
usp_directory="data/"+env_id+"/"+endpointLast+"/"+id_rule+"/userActionAndSessionProperties"
|
|
Path(usp_directory).mkdir(parents=True, exist_ok=True)
|
|
|
|
# Get userActionAndSessionProperties
|
|
kua = requests.get(''.join(YOUR_DT_API_URL) + endpoint +"/"+id_rule+"/userActionAndSessionProperties", headers=headers, cookies=dict(cookies), verify=False);
|
|
# Save mobile application config
|
|
f = open(usp_directory+"/userActionAndSessionProperties.json", "w")
|
|
json_data = json.loads(kua.text)
|
|
json_formatted_str = json.dumps(json_data, indent=2)
|
|
f.write(json_formatted_str)
|
|
f.close()
|
|
|
|
|
|
i+=1
|
|
else:
|
|
f = open(directory+"/"+endpointLast+".json", "w")
|
|
json_data = json.loads(r.text)
|
|
json_formatted_str = json.dumps(json_data, indent=2)
|
|
f.write(json_formatted_str)
|
|
|
|
def update():
|
|
endpointLast = endpoint
|
|
if "/" in endpoint:
|
|
endpointLast = endpoint.split("/")[-1]
|
|
|
|
#Get list of mobile app ids
|
|
r = requests.get(''.join(YOUR_DT_API_URL) + endpoint, headers=headers, cookies=dict(cookies), verify=False);
|
|
data_json = json.loads(r.text)
|
|
|
|
# Obtain iterative_key
|
|
for key in data_json.keys():
|
|
if type(data_json[key]) is list:
|
|
if "id" in data_json[key][0]:
|
|
iterative_key = key
|
|
|
|
i=0
|
|
while i<len(data_json[iterative_key]):
|
|
# Create directory for each application
|
|
id_rule = data_json[iterative_key][i]['id']
|
|
directory = "data/"+env_id+"/"+endpointLast+"/"+id_rule
|
|
|
|
# Update config mobile app
|
|
filename=id_rule+".json"
|
|
with open(os.path.join(directory, filename), 'r+') as myfile:
|
|
data=myfile.read()
|
|
r = requests.put(''.join(YOUR_DT_API_URL) + endpoint +"/"+id_rule, data, headers=headers, cookies=dict(cookies), verify=False)
|
|
|
|
# Update config mobile app userActionAndSessionProperties
|
|
if os.path.exists(directory+"/userActionAndSessionProperties"):
|
|
with open(os.path.join(directory+"/userActionAndSessionProperties", "userActionAndSessionProperties.json"), 'r+') as myfile:
|
|
data=myfile.read()
|
|
r = requests.put(''.join(YOUR_DT_API_URL) + endpoint +"/"+id_rule+"/userActionAndSessionProperties", data, headers=headers, cookies=dict(cookies), verify=False)
|
|
|
|
if os.path.exists(directory+"/keyUserActions"):
|
|
# Update config mobile app keyUserActions
|
|
with open(os.path.join(directory+"/keyUserActions", "keyUserActions.json"), 'r+') as myfile:
|
|
data=myfile.read()
|
|
r = requests.put(''.join(YOUR_DT_API_URL) + endpoint +"/"+id_rule+"/keyUserActions", data, headers=headers, cookies=dict(cookies), verify=False)
|
|
i+=1
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
number_of_calls = 0
|
|
YOUR_DT_API_URL = ''
|
|
YOUR_DT_API_TOKEN = ''
|
|
cookies = {"null":"null"}
|
|
user = "null"
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
config.read('config.ini')
|
|
|
|
updateConfig = config['ACTION']['updateConfig']
|
|
downloadConfig = config['ACTION']['downloadConfig']
|
|
|
|
endpoint = config['ENDPOINT']['endpoint']
|
|
endpoint = endpoint.rstrip("\n")
|
|
|
|
for (key, val) in config.items('TENANTS'):
|
|
tenant_info = val
|
|
extracted_tenant_info = saveValues()
|
|
|
|
YOUR_DT_API_URL = extracted_tenant_info[0]
|
|
TOKEN = extracted_tenant_info[1]
|
|
YOUR_DT_API_TOKEN = os.getenv(TOKEN)
|
|
|
|
|
|
#Build the header and cookie
|
|
headers={}
|
|
headers["Authorization"] = "Api-Token "+YOUR_DT_API_TOKEN
|
|
headers["Content-Type"] = "application/json"
|
|
|
|
#Generate env_id
|
|
if "/e/" in YOUR_DT_API_URL:
|
|
env_id = YOUR_DT_API_URL.split("/e/")[1]
|
|
else:
|
|
env_id = YOUR_DT_API_URL.split("//")[1]
|
|
|
|
#Create /data folder
|
|
if not os.path.exists('data/'):
|
|
os.mkdir('data/')
|
|
|
|
#Create tenant folder
|
|
path = "data/"+env_id
|
|
if not os.path.exists(path):
|
|
os.mkdir(path)
|
|
|
|
print("\n")
|
|
|
|
if downloadConfig=='1':
|
|
print("Downloading rules of "+ env_id)
|
|
download()
|
|
|
|
if updateConfig=='1':
|
|
print("Updating rules of "+ env_id)
|
|
update()
|
|
|
|
print("\n")
|