sp-hydra-veil-gui/SMS-Exchange-Linux-GUI/local_data_operations/local_logs.py
2026-01-28 13:13:57 +01:00

290 lines
No EOL
10 KiB
Python
Executable file

from interact_with_rest_of_app.which_profile_is_being_used import which_profile_is_being_used
# Loading and using JSONs
from local_data_operations.search_or_setup_json import get_the_path_of_the_folder_for, load_json_as_dict, search_key_in_json
# Generic:
import json
import os
def return_profile_filename_and_path(product):
# filename:
current_profile_number = which_profile_is_being_used()
if product == "email":
json_file_name = f"email_{current_profile_number}.json"
else: # this is sms:
json_file_name = f"{current_profile_number}.json"
# folder path:
path_for_configs = get_the_path_of_the_folder_for('.config', 'sms')
# full path:
# Check if the folder exists
if not os.path.exists(path_for_configs):
# Create the folder if it doesn't exist
os.makedirs(path_for_configs)
print(f"Folder '{path_for_configs}' created.")
else:
print(f"Folder '{path_for_configs}' already existed.")
# Full path for the JSON file
full_json_file_path = os.path.join(path_for_configs, json_file_name)
return json_file_name, path_for_configs, full_json_file_path
def check_if_profile_exists_already(full_json_file_path):
# Check if the JSON file exists
if os.path.exists(full_json_file_path):
return True
else:
return False
# When a profile is initiated, we want to know if it has a country and proxy already assigned.
def get_profile_location_and_proxy_status_as_tuple():
# get the path and other variables that are not needed, but have to be unpacked:
json_file_name, path_for_configs, full_json_file_path = return_profile_filename_and_path("sms")
# Check if the JSON file exists:
if os.path.exists(full_json_file_path):
# if so get the data:
with open(full_json_file_path, 'r') as json_file:
data = json.load(json_file)
# get the location, or return "none" if it can't be found.
# return json_dict.get(key, default_value_if_not_found)
assigned_location = data.get("profile", {}).get("location", False)
assigned_proxy = data.get("profile", {}).get("assigned_proxy", False)
if assigned_location == "":
return False
else:
return (assigned_location, assigned_proxy)
else:
return False
# If it does not exist already, then create it from scratch: # darth vadar
def setup_email_config(email_operator, full_email, email_password, email_url):
print("SETUP EMAIL CONFIG triggered")
email_json_file_name, path_for_configs, email_full_json_file_path = return_profile_filename_and_path("email")
# Add the email data:
new_data = {
"email_operator": email_operator,
"full_email": full_email,
"email_url": email_url,
"email_password": email_password
}
try:
# Update the JSON Profile with new data:
with open(email_full_json_file_path, 'w') as json_file:
json.dump(new_data, json_file, indent=4)
return True # it worked:
except:
return "error"
def wipe_entire_profile_data():
json_file_name, path_for_configs, full_json_file_path = return_profile_filename_and_path("sms")
# Check if the file exists
if os.path.isfile(full_json_file_path):
# Delete the file
os.remove(full_json_file_path)
print(f"{full_json_file_path} has been deleted.")
return True
else:
print(f"{full_json_file_path} does not exist.")
return False
def wipe_email_data():
email_json_file_name, path_for_configs, email_full_json_file_path = return_profile_filename_and_path("email")
# Check if the file exists
if os.path.isfile(email_full_json_file_path):
# Delete the file
os.remove(email_full_json_file_path)
print(f"{email_full_json_file_path} has been deleted.")
return True
else:
print(f"{email_full_json_file_path} does not exist.")
return False
def get_email_data():
print("READ EMAIL CONFIG triggered")
email_json_file_name, path_for_configs, email_full_json_file_path = return_profile_filename_and_path("email")
# Check if the JSON file exists
if os.path.exists(email_full_json_file_path):
try:
with open(email_full_json_file_path, 'r') as json_file:
email_data = json.load(json_file)
email_operator = email_data['email_operator']
full_email = email_data['full_email']
email_url = email_data['email_url']
email_password = email_data['email_password']
packed_email_data = (email_operator, full_email, email_url, email_password)
return packed_email_data
except:
return "error"
else:
return False
def wipe_location_and_proxy():
# then get the profile number & path:
json_file_name, path_for_configs, full_json_file_path = return_profile_filename_and_path("sms")
if os.path.exists(full_json_file_path):
# if so get the data:
with open(full_json_file_path, 'r') as json_file:
data = json.load(json_file)
# clear it:
data['profile']['location'] = ""
data['profile']['assigned_proxy'] = False
try:
# Update the JSON Profile with new data:
with open(full_json_file_path, 'w') as json_file:
json.dump(data, json_file, indent=4)
return True # it worked:
except:
return "error"
# if it does NOT exist:
else:
return True
# If it does not exist already, then create it from scratch:
def create_or_add_to_profile_config(config_file_data, peer_to_peer_billing_id, chosen_location, proxy_or_not, chosen_service, email_operator):
# first check if the user disabled local data saving in the config file:
do_we_want_logs = search_key_in_json(config_file_data, "local_data", True)
if do_we_want_logs == False:
return False
else:
# then get the profile number & path:
json_file_name, path_for_configs, full_json_file_path = return_profile_filename_and_path("sms")
if os.path.exists(full_json_file_path):
# if so get the data:
with open(full_json_file_path, 'r') as json_file:
data = json.load(json_file)
# If it's a blank location, then update it with the new choices:
pre_existing_location = data['profile']['location']
if pre_existing_location == "":
data['profile']['location'] = chosen_location
data['profile']['assigned_proxy'] = proxy_or_not
else:
pass
# Add the new order:
data["orders"][peer_to_peer_billing_id] = {
"chosen_service": chosen_service,
"wants_sms": True,
"completed": False
}
try:
# Update the JSON Profile with new data:
with open(full_json_file_path, 'w') as json_file:
json.dump(data, json_file, indent=4)
return True # it worked:
#updated_json = json.dumps(data, indent=4)
except:
return "error"
# if it does NOT exist:
else:
# We are creating a new Profile Config:
# stock the JSON with values for a new profile:
default_json_content = {
"profile": {
"location": chosen_location,
"assigned_proxy": proxy_or_not,
"got_proxy": False,
"email_operator": email_operator
},
"orders": {
peer_to_peer_billing_id: {
"chosen_service": chosen_service,
"wants_sms": True,
"completed": False
}
}
}
try:
# Create the JSON file:
with open(full_json_file_path, 'w') as json_file:
json.dump(default_json_content, json_file, indent=4)
return True # it worked:
except:
return "error"
def return_all_locally_saved_orders_for_the_current_profile():
# get the path and other variables that are not needed, but have to be unpacked:
json_file_name, path_for_configs, full_json_file_path = return_profile_filename_and_path("sms")
# Check if the JSON file exists:
if os.path.exists(full_json_file_path):
# setup a list to save the orders:
list_of_all_past_orders = []
try:
# if so get the data:
with open(full_json_file_path, 'r') as json_file:
data = json.load(json_file)
# Iterate through the orders & get their past details:
for order in data["orders"]:
chosen_service = data["orders"][order]["chosen_service"]
completed = data["orders"][order]["completed"]
order_data_as_tuple = (order, chosen_service, completed)
list_of_all_past_orders.append(order_data_as_tuple)
except:
return False
finally:
return list_of_all_past_orders
else:
return False
def delete_an_order(which_order):
# get the profile number & path:
json_file_name, path_for_configs, full_json_file_path = return_profile_filename_and_path("sms")
if os.path.exists(full_json_file_path):
# if so get the data:
with open(full_json_file_path, 'r') as json_file:
data = json.load(json_file)
try:
# delete it:
del data["orders"][which_order]
# Update the JSON Profile with new data:
with open(full_json_file_path, 'w') as json_file:
json.dump(data, json_file, indent=4)
return True # it worked
except:
return "error"
'''
# see if email operator is blank even though they have one from the past:
if email_operator == None:
# then get the profile number & path:
json_file_name, path_for_configs, full_json_file_path = return_profile_filename_and_path("sms")
try:
with open(full_json_file_path, 'r') as json_file:
data = json_file.load(json_file)
email_operator = data[profile][email_operator]
except:
email_operator = None
'''
#email_operator = None
#full_email = "test@t.com"
#email_password = "dd"
#email_url = "aa.com"
#config_file_data = "a"
#setup_email_config(config_file_data, email_operator, full_email, email_password, email_url)