New handling system everywhere

This commit is contained in:
BarsTiger
2022-03-01 14:21:42 +02:00
parent a70570225f
commit d35c72bf02
8 changed files with 34 additions and 138 deletions

View File

@@ -1,6 +1,5 @@
import json
import sys
import modules.vars as horsy_vars
import ctypes
@@ -35,7 +34,7 @@ def get_auth(is_gui=False, login_ui=None, Ui_LoginWindow=None):
login_ui.login_button.clicked.connect(load_login_now)
def get_auth_without_login(is_gui=False):
def get_auth_without_login():
with open(horsy_vars.horsypath + 'config.cfg') as f:
config = json.load(f)

View File

@@ -1,10 +1,10 @@
import requests
import modules.gui as gui
from modules.auth import del_auth, get_auth
from modules.auth import get_auth
import modules.vars as horsy_vars
import json
import threading
from PyQt5 import QtWidgets
from modules.http_status import handle
def change(email):
@@ -21,34 +21,10 @@ def change(email):
try:
def change_in_new_thread():
try:
r = requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users',
json={'auth': auth, 'email': email})
try:
r = r.json()
except:
if r.text == '':
gui.cpopup('Success', 'Success, your email has been changed')
with open(horsy_vars.horsypath + 'config.cfg') as f:
config = json.load(f)
config['auth'] = {'email': email, 'password': config['auth']['password']}
with open(horsy_vars.horsypath + 'config.cfg', 'w') as f:
json.dump(config, f)
try:
if r['message'] == 'Unauthorized':
gui.cpopup('Error', 'Invalid credentials \nDeleting auth from config')
del_auth()
elif r['message'] == 'Internal server error':
gui.cpopup('Error', 'Internal server error')
return 'Internal server error'
else:
print('Unknown error, please try again')
print('Server response:')
print(r.text)
return 'Unknown error, please try again, \n Server response: \n' + str(r.text)
except:
pass
r_code = handle(requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users',
json={'auth': auth, 'email': email}).status_code)
if r_code[1] not in [200, 201]:
gui.cpopup("Error", r_code[1])
except:
gui.cpopup('Error', 'Unexpected error.')
threading.Thread(target=change_in_new_thread).start()

View File

@@ -1,8 +1,8 @@
import requests
import modules.gui as gui
from modules.auth import del_auth, get_auth
from modules.auth import get_auth
import modules.vars as horsy_vars
import json
from modules.http_status import handle
from PyQt5 import QtWidgets
@@ -26,34 +26,9 @@ def change(oldpass, newpass):
return
try:
r = requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users',
json={'auth': get_auth(True, login_ui, QtWidgets.QMainWindow()),
'password': newpass})
try:
r = r.json()
except:
if r.text == '':
gui.popup('Success', 'Success, your password has been changed')
with open(horsy_vars.horsypath + 'config.cfg') as f:
config = json.load(f)
config['auth'] = {'email': config['auth']['email'], 'password': newpass}
with open(horsy_vars.horsypath + 'config.cfg', 'w') as f:
json.dump(config, f)
try:
if r['message'] == 'Unauthorized':
gui.popup('Error', 'Invalid credentials \nDeleting auth from config')
del_auth()
elif r['message'] == 'Internal server error':
gui.popup('Error', 'Internal server error')
return 'Internal server error'
else:
print('Unknown error, please try again')
print('Server response:')
print(r)
return 'Unknown error, please try again, \n Server response: \n' + str(r)
except:
pass
gui.cpopup("Changing password",
handle(requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users',
json={'auth': get_auth(True, login_ui, QtWidgets.QMainWindow()),
'password': newpass}).status_code)[1])
except:
gui.popup('Error', 'Unexpected error.')

View File

@@ -1,10 +1,9 @@
import time
from modules.auth import get_auth
from horsygui import login_ui, UiLoginWindow as Ui_LoginWindow
import modules.gui as gui
import requests
import modules.vars as horsy_vars
import modules.http_status as status
from modules.http_status import handle
import json
@@ -12,33 +11,13 @@ def edit(package, UiPackageWindow):
package_ui = gui.Ui_PackageWindow()
package_ui.setupUi(UiPackageWindow)
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}").text
if r == "":
gui.cpopup("Installation", f"Package {package} not found")
return f"Package {package} not found"
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}")
try:
r_code = handle(r.status_code)
if r_code[1] not in [200, 201]:
gui.cpopup("Error", r_code[1])
r = r.text
r = json.loads(r)
except:
print("[red]Error with unsupported message[/]")
gui.cpopup("Error", "Error with unsupported message")
return "Error with unsupported message"
try:
if r["message"] == "Internal server error":
print("Internal server error")
gui.cpopup("Error", "Internal server error")
return "Internal server error"
if r["message"] == "Too many requests":
try:
if r['message'] == 'Too many requests':
while r['message'] == 'Too many requests':
time.sleep(0.5)
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}").text
try:
r = json.loads(r)
except:
pass
except:
pass
except:
pass
@@ -64,20 +43,6 @@ def edit(package, UiPackageWindow):
'run': (lambda x: x if x != '' else None)(package_ui.main_exe_box.text())
}
r = requests.put(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages", json=body)
if r.status_code == status.OK:
gui.cpopup('Success', 'Success, package edited')
elif r.status_code == status.UNAUTHORIZED:
gui.cpopup('Error', 'Unauthorized')
elif r.status_code == status.NOT_FOUND:
gui.cpopup('Error', 'Package not found or its not yours')
elif r.status_code == status.BAD_REQUEST:
gui.cpopup('Error', 'Bad request')
elif r.status_code == status.INTERNAL_SERVER_ERROR:
gui.cpopup('Error', 'Internal server error')
elif r.status_code == status.TOO_MANY_REQUESTS:
gui.cpopup('Error', 'Too many requests')
else:
gui.cpopup('Error', 'Unknown error')
handle(requests.put(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages", json=body).status_code)
package_ui.update_button.clicked.connect(send)

View File

@@ -5,6 +5,7 @@ import requests
import modules.vars as horsy_vars
import json
from rich import print
from modules.http_status import handle
client = SearchClient.create(
requests.get('https://raw.githubusercontent.com/horsy-ml/horsy/master/web_vars/search_app').json()['APP_ID'],
@@ -26,21 +27,10 @@ def search(query, is_gui=False):
def info(package, download_ui=None, UiDownloadWindow=None):
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}").text
if r == "":
print(f"[red]Package {package} not found[/]")
return f"Package {package} not found"
try:
r = json.loads(r)
except:
print("[red]Error with unsupported message[/]")
return "Error with unsupported message"
try:
if r["message"] == "Internal server error":
print("[red]Internal server error[/]")
return "Internal server error"
except:
pass
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}")
r_code = handle(r.status_code)
r = r.text
r = json.loads(r)
print(f"[bold]{r['name']}{'' if r['verified'] else ''} by {r['authorName']}[/]")
print(f"{r['description']}")
@@ -71,4 +61,3 @@ def info(package, download_ui=None, UiDownloadWindow=None):
"We don't call you to trust this app, use it at your own risk \n"
"but we recommend you more to install verified packages")
return None

View File

@@ -3,24 +3,16 @@ import json
import webbrowser
import modules.vars as horsy_vars
from rich import print
from modules.http_status import handle
def get_source(package):
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}").text
try:
r = json.loads(r)
except:
print("[red]Error with unsupported message[/]")
return "Error with unsupported message"
try:
if r["message"] == "not found":
print("[red]Package not found[/]")
return "Package not found"
if r["message"] == "Internal server error":
print("[red]Internal server error[/]")
return "Internal server error"
except:
pass
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}")
r_code = handle(r.status_code)
if r_code[1] not in [200, 201]:
return r_code[1]
r = r.text
r = json.loads(r)
try:
webbrowser.open(r["sourceUrl"])

View File

@@ -1,7 +1,7 @@
import threading
import subprocess
import time
def run_threaded(func, *args):
try:
threading.Thread(target=func, args=args).start()

View File

@@ -95,7 +95,7 @@ def upload(is_gui=False, ui=None):
}
else:
auth = get_auth_without_login(is_gui)
auth = get_auth_without_login()
project_name = ui.packagename_box.text()
if not matches(project_name) or len(project_name) > 64 or len(project_name) < 3: