-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (114 loc) · 4.13 KB
/
main.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
import sys
import subprocess
import importlib
def check_and_install_packages():
required_packages = {
"dotenv": "python-dotenv",
"requests": "requests",
"qrcode": "qrcode",
"PIL": "pillow",
}
missing_packages = []
for module_name, package_name in required_packages.items():
try:
importlib.import_module(module_name)
except ImportError:
missing_packages.append(package_name)
if missing_packages:
print("The following packages are missing:")
for pkg in missing_packages:
print(f"- {pkg}")
choice = input("Would you like to install them now? (y/n): ").strip().lower()
if choice == 'y':
try:
subprocess.check_call([sys.executable, "-m", "pip", "install"] + missing_packages)
print("All missing packages have been installed successfully.")
print("Restarting the script to apply changes...")
os.execv(sys.executable, [sys.executable] + sys.argv)
except subprocess.CalledProcessError:
print("An error occurred while installing packages. Please install them manually.")
sys.exit(1)
else:
print("Cannot proceed without installing the required packages.")
sys.exit(1)
else:
print("All required packages are already installed.")
def initialize_environment():
from dotenv import load_dotenv
load_dotenv(override=True)
return os.getenv("API_KEY")
def check_and_set_api_key():
api_key = os.getenv("API_KEY")
if not api_key or api_key.strip() == "":
new_api_key = input("API Key missing. Please enter an API Key: ").strip()
if not new_api_key:
print("Invalid API Key. Exiting...")
sys.exit(1)
update_env_file("API_KEY", new_api_key)
print("API Key successfully updated. Restarting application...")
restart_application()
else:
print(f"API Key found: {api_key}")
def update_env_file(key, value):
if not os.path.exists(".env"):
with open(".env", "w") as file:
file.write(f"{key}={value}\n")
else:
with open(".env", "r") as file:
lines = file.readlines()
updated = False
with open(".env", "w") as file:
for line in lines:
if line.startswith(f"{key}="):
file.write(f"{key}={value}\n")
updated = True
else:
file.write(line)
if not updated:
file.write(f"{key}={value}\n")
def restart_application():
print("Restarting application...")
os.execv(sys.executable, [sys.executable] + sys.argv)
def select_operation():
print("\nOperation options:")
print("1. Check balance")
print("2. Proof of Payment")
print("3. Receive")
print("4. Send")
print("5. Change API Key")
choice = input("Enter an option (1-5): ").strip()
python_command = "python3" if sys.platform != "win32" else "python"
if choice == "1":
os.system(f"{python_command} balance.py")
elif choice == "2":
os.system(f"{python_command} proof.py")
elif choice == "3":
os.system(f"{python_command} receive.py")
elif choice == "4":
os.system(f"{python_command} send.py")
elif choice == "5":
new_api_key = input("Enter new API Key: ").strip()
if not new_api_key:
print("Invalid API Key. Returning to menu...")
return
update_env_file("API_KEY", new_api_key)
print("API Key successfully updated. Restarting application...")
restart_application()
else:
print("Invalid selection. Please try again.")
def main():
check_and_install_packages()
from dotenv import load_dotenv
import requests
import qrcode
load_dotenv(override=True)
check_and_set_api_key()
while True:
select_operation()
cont = input("Would you like to perform another operation? (y/n): ").strip().lower()
if cont != "y":
print("Exiting...")
break
if __name__ == "__main__":
main()