versions and updates for apps

better api requests system
This commit is contained in:
BarsTiger
2022-05-05 18:39:36 +03:00
parent 6d7863a89d
commit 84512ade36
15 changed files with 170 additions and 85 deletions

View File

@@ -1,4 +1,4 @@
import requests
from modules.request import request
import modules.gui as gui
from modules.auth import get_auth
import modules.vars as horsy_vars
@@ -21,11 +21,12 @@ def change(email):
try:
def change_in_new_thread():
try:
r_code = handle(requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users',
json={'auth': auth, 'email': email}).status_code)
r_code = handle(request.put(horsy_vars.protocol + horsy_vars.server_url + '/users',
json={'auth': auth, 'email': email}).status_code)
gui.cpopup("Changing email", r_code[0])
except:
gui.cpopup('Error', 'Unexpected error.')
threading.Thread(target=change_in_new_thread).start()
gui.popup('Started', 'Check your email for confirmation')
except:

View File

@@ -1,4 +1,4 @@
import requests
from modules.request import request
import modules.gui as gui
from modules.auth import get_auth
import modules.vars as horsy_vars
@@ -27,8 +27,8 @@ def change(oldpass, newpass):
try:
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)[0])
handle(request.put(horsy_vars.protocol + horsy_vars.server_url + '/users',
json={'auth': get_auth(True, login_ui, QtWidgets.QMainWindow()),
'password': newpass}).status_code)[0])
except:
gui.popup('Error', 'Unexpected error.')

View File

@@ -7,6 +7,7 @@ import signal
from functools import partial
from threading import Event
from urllib.request import urlopen
from urllib.parse import unquote
from rich.progress import (
BarColumn,
@@ -59,7 +60,7 @@ def dl(urls, dest_dir: str):
with progress:
with ThreadPoolExecutor(max_workers=len(urls)) as pool:
for url in urls:
filename = url.split("/")[-1]
filename = unquote(url.split("/")[-1])
dest_path = os.path.join(dest_dir, filename)
task_id = progress.add_task("download", filename=filename, start=False)
pool.submit(copy_url, task_id, url, dest_path)

View File

@@ -1,6 +1,7 @@
import json
import threading
from modules.http_status import handle
from modules.request import request
import requests
import modules.vars as horsy_vars
import os
@@ -9,10 +10,11 @@ from modules.virustotal import get_key, scan_file, get_report
from horsygui import UiDownloadWindow, download_ui
from modules.gui import cpopup
from PyQt5 import QtGui
from urllib.parse import unquote
def install(package):
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}")
r = request.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)
@@ -23,7 +25,7 @@ def install(package):
try:
UiDownloadWindow.show()
download_ui.logs_box.clear()
download_ui.logs_box.append(f"Downloading {r['url'].split('/')[-1]}")
download_ui.logs_box.append(f"Downloading {unquote(r['url'].split('/')[-1])}")
def install_this():
if not os.path.exists('{1}apps/{0}'.format(r['name'], horsy_vars.horsypath)):
@@ -35,7 +37,8 @@ def install(package):
file_r = requests.get(r['url'], stream=True)
chunk_size = int(int(file_r.headers['Content-Length']) / 100)
percent = 0
with open('{2}apps/{0}/{1}'.format(r['name'], r['url'].split('/')[-1], horsy_vars.horsypath), "wb") as f:
with open('{2}apps/{0}/{1}'.format(r['name'], unquote(r['url'].split('/')[-1]), horsy_vars.horsypath),
"wb") as f:
for chunk in file_r.iter_content(chunk_size=chunk_size):
if chunk:
percent += 1
@@ -47,15 +50,15 @@ def install(package):
threads.append(threading.Thread(target=dl_main_file))
if r['download']:
download_ui.logs_box.append(f"Downloading {r['download'].split('/')[-1]}")
download_ui.logs_box.append(f"Downloading {unquote(r['download'].split('/')[-1])}")
download_ui.logs_box.moveCursor(QtGui.QTextCursor.End)
def dl_dep_file():
global success
file_r = requests.get(r['download'], stream=True)
chunk_size = int(int(file_r.headers['Content-Length']) / 100)
with open('{2}apps/{0}/{1}'.format(r['name'], r['download'].split('/')[-1], horsy_vars.horsypath),
"wb") as f:
with open('{2}apps/{0}/{1}'.format(r['name'], unquote(r['download'].split('/')[-1]),
horsy_vars.horsypath), "wb") as f:
for chunk in file_r.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)
@@ -78,9 +81,9 @@ def install(package):
download_ui.logs_box.moveCursor(QtGui.QTextCursor.End)
if r['url'].split('.')[-1] == 'zip':
download_ui.logs_box.append(f"Extracting {r['url'].split('/')[-1]}")
download_ui.logs_box.append(f"Extracting {unquote(r['url'].split('/')[-1])}")
download_ui.logs_box.moveCursor(QtGui.QTextCursor.End)
unzip('{2}apps/{0}/{1}'.format(r['name'], r['url'].split('/')[-1], horsy_vars.horsypath),
unzip('{2}apps/{0}/{1}'.format(r['name'], unquote(r['url'].split('/')[-1]), horsy_vars.horsypath),
'{1}apps/{0}'.format(r['name'], horsy_vars.horsypath))
download_ui.logs_box.append("")
@@ -95,12 +98,15 @@ def install(package):
download_ui.logs_box.append("If you want to disable scan, type horsy --vt disable in terminal")
download_ui.logs_box.append("Starting virustotal scan for program")
download_ui.logs_box.moveCursor(QtGui.QTextCursor.End)
scan_file('{2}apps/{0}/{1}'.format(r['name'], r['url'].split('/')[-1], horsy_vars.horsypath))
analysis = get_report('{2}apps/{0}/{1}'.format(r['name'], r['url'].split('/')[-1],
scan_file('{2}apps/{0}/{1}'.format(r['name'], unquote(r['url'].split('/')[-1]),
horsy_vars.horsypath))
analysis = get_report('{2}apps/{0}/{1}'.format(r['name'], unquote(r['url'].split('/')[-1]),
horsy_vars.horsypath))
download_ui.logs_box.append(f"Scan finished for program \nYou can see report for program by opening: "
download_ui.logs_box.append(f"Scan finished for program \nYou can see report for program by "
f"opening: "
f"{analysis['link']} \n"
f"{analysis['detect']['malicious']} antivirus flagged this file as malicious")
f"{analysis['detect']['malicious']} antivirus flagged this file as "
f"malicious")
download_ui.logs_box.moveCursor(QtGui.QTextCursor.End)
except:
pass
@@ -110,11 +116,11 @@ def install(package):
download_ui.logs_box.append("")
download_ui.logs_box.append("Starting virustotal scan for dependency")
download_ui.logs_box.moveCursor(QtGui.QTextCursor.End)
scan_file('{2}apps/{0}/{1}'.format(r['name'], r['download'].split('/')[-1],
scan_file('{2}apps/{0}/{1}'.format(r['name'], unquote(r['download'].split('/')[-1]),
horsy_vars.horsypath))
download_ui.logs_box.append(f"Scan finished for dependency")
download_ui.logs_box.moveCursor(QtGui.QTextCursor.End)
analysis = get_report('{2}apps/{0}/{1}'.format(r['name'], r['download'].split('/')[-1],
analysis = get_report('{2}apps/{0}/{1}'.format(r['name'], unquote(r['download'].split('/')[-1]),
horsy_vars.horsypath))
download_ui.logs_box.append(f"You can see report for dependency by opening: {analysis['link']}")
download_ui.logs_box.append(f"{analysis['detect']['malicious']} "
@@ -139,7 +145,7 @@ def install(package):
download_ui.logs_box.append("Generating launch script")
with open('{1}apps/{0}.bat'.format(r['name'], horsy_vars.horsypath), 'w') as f:
f.write(f"@ECHO off\n")
f.write(f"""{r['run'].replace('$appdir$', f'%horsypath%/apps/{r["name"]}')} %*\n""")
f.write(f""""{r['run'].replace('$appdir$', f'%horsypath%/apps/{r["name"]}')}" %*\n""")
download_ui.logs_box.append("")
download_ui.logs_box.moveCursor(QtGui.QTextCursor.End)
@@ -149,8 +155,17 @@ def install(package):
download_ui.logs_box.moveCursor(QtGui.QTextCursor.End)
threading.Thread(target=os.system, args=('{2}apps/{0}/{1}'.format(r['name'], r['install'],
horsy_vars.horsypath),)).start()
# Update versions file
with open(horsy_vars.horsypath + 'apps/versions.json', 'r') as f:
versions = json.load(f)
with open(horsy_vars.horsypath + 'apps/versions.json', 'w') as f:
versions[r['name']] = r['version']
f.write(json.dumps(versions))
download_ui.logs_box.append(f"All done!\nYou can run your app by entering {r['name']} in terminal")
download_ui.logs_box.moveCursor(QtGui.QTextCursor.End)
threading.Thread(target=install_this).start()
except:

View File

@@ -1,4 +1,4 @@
import requests
from modules.request import request
import modules.vars as horsy_vars
from modules.auth import get_auth
@@ -17,6 +17,6 @@ def send(package, type, is_gui=False, login_ui=None, Ui_LoginWindow=None):
"rate": type,
"packageName": package
}
r = requests.post(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/rate", json=body).json()
r = request.post(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/rate", json=body).json()
print(r["message"])
return r["message"]

View File

@@ -1,4 +1,4 @@
import requests
from modules.request import request
from PyQt5 import QtWidgets
import modules.gui as gui
from modules.auth import get_auth, del_auth
@@ -14,9 +14,9 @@ def loginload():
with open(horsy_vars.horsypath + 'config.cfg') as f:
config = json.load(f)
if config['auth'] is not None:
return (lambda x: (x if x != "Forbidden" else "Invalid login"))\
(requests.get(horsy_vars.protocol + horsy_vars.server_url + '/users/login',
json={'auth': config['auth']}).json()['message'])
return (lambda x: (x if x != "Forbidden" else "Invalid login")) \
(request.get(horsy_vars.protocol + horsy_vars.server_url + '/users/login',
json={'auth': config['auth']}).json()['message'])
except:
pass

View File

@@ -1,7 +1,7 @@
import json
import threading
from rich import print
import requests
from modules.request import request
import modules.vars as horsy_vars
import os
import zipfile
@@ -11,7 +11,7 @@ from modules.download import dl
def install(package):
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}")
r = request.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)
@@ -78,7 +78,15 @@ def install(package):
with open('{1}apps\{0}.bat'.format(r['name'], horsy_vars.horsypath), 'w+') as f:
f.write(f"@ECHO off\n")
f.write(f"""{r['run'].replace('$appdir$', f'%horsypath%/apps/{r["name"]}')} %*\n""")
f.write(f""""{r['run'].replace('$appdir$', f'%horsypath%/apps/{r["name"]}')}" %*\n""")
# Update versions file
with open(horsy_vars.horsypath + 'apps/versions.json', 'r') as f:
versions = json.load(f)
with open(horsy_vars.horsypath + 'apps/versions.json', 'w') as f:
versions[r['name']] = r['version']
f.write(json.dumps(versions))
print(f"Versions file updated")
# Done message
print(f"[green][OK] All done![/]")
@@ -114,4 +122,4 @@ def apps_list(is_gui=False):
elif file.endswith(".bat"):
apps.append(file.split('.')[0])
if is_gui:
return apps
return sorted(apps)

View File

@@ -1,7 +1,7 @@
from modules.auth import get_auth
from horsygui import login_ui, UiLoginWindow as Ui_LoginWindow
import modules.gui as gui
import requests
from modules.request import request
import modules.vars as horsy_vars
from modules.http_status import handle
import json
@@ -11,7 +11,7 @@ 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}")
r = request.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]:
@@ -44,15 +44,15 @@ def edit(package, UiPackageWindow):
}
gui.cpopup("Updating",
handle(requests.put(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages",
json=body).status_code)[0])
handle(request.put(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages",
json=body).status_code)[0])
package_ui.update_button.clicked.connect(send)
def push_version(package):
gui.cpopup("Pushing version",
handle(requests.post(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/push-version", json={
handle(request.post(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/push-version", json={
'auth': get_auth(True, login_ui, Ui_LoginWindow),
'name': package
}).status_code)[0])

29
modules/request.py Normal file
View File

@@ -0,0 +1,29 @@
import time
import requests
class request:
@staticmethod
def _request(url, method, json=None):
while True:
r = requests.request(method, url, json=json)
if r.status_code != 429:
return r
else:
time.sleep(0.5)
@staticmethod
def get(url, json=None):
return request._request(url, 'GET', json=json)
@staticmethod
def post(url, json=None):
return request._request(url, 'POST', json=json)
@staticmethod
def put(url, json):
return request._request(url, 'PUT', json=json)
@staticmethod
def delete(url):
return request._request(url, 'DELETE')

View File

@@ -1,6 +1,7 @@
import textwrap
from algoliasearch.search_client import SearchClient
import os
from modules.request import request
import requests
import modules.vars as horsy_vars
import json
@@ -27,7 +28,7 @@ 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}")
r = request.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)

View File

@@ -1,4 +1,4 @@
import requests
from modules.request import request
import json
import webbrowser
import modules.vars as horsy_vars
@@ -7,7 +7,7 @@ from modules.http_status import handle
def get_source(package):
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}")
r = request.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[0]

View File

@@ -1,22 +1,44 @@
import json
import requests
from modules.request import request
import modules.vars as horsy_vars
from modules.http_status import handle
from modules.manager import apps_list
from rich import print
def check():
r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/"
f"{','.join(apps_list(True))}")
def check(gui=False):
r = request.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/"
f"{','.join(apps_list(True))}")
r_code = handle(r.status_code)
if r_code[1] not in [200, 201]:
return r_code[0]
r = r.text
r = json.loads(r)
print(r)
update_this = list()
with open(horsy_vars.horsypath + 'apps/versions.json', 'r') as f:
versions = json.load(f)
return update_this
need_update = list()
for app in r:
try:
if versions[app] < r[app]['version']:
need_update.append(app)
except (TypeError, KeyError):
if r[app]['version'] > 0:
need_update.append(app)
except Exception as e:
print(f"[red]Unexpected error![/]")
print(e)
need_update.sort()
if not gui:
return need_update
apps = list()
for app in apps_list(True):
if app in need_update:
apps.append("!" + app)
else:
apps.append(app)
return sorted(apps)

View File

@@ -1,6 +1,6 @@
import json
import time
import requests
from modules.request import request
from rich import print
from modules.auth import get_auth, del_auth, get_auth_without_login
import re
@@ -83,7 +83,7 @@ def upload(is_gui=False, ui=None):
print('[red]Please, specify runtime[/red]')
run = None
request = {
request_body = {
'auth': auth,
'name': project_name,
'description': description,
@@ -127,7 +127,7 @@ def upload(is_gui=False, ui=None):
if run == '':
return 'Please, specify runtime'
request = {
request_body = {
'auth': auth,
'name': project_name,
'description': description,
@@ -141,7 +141,7 @@ def upload(is_gui=False, ui=None):
r = None
while r is None:
try:
r = requests.post(horsy_vars.protocol + horsy_vars.server_url + '/packages/new', json=request)
r = request.post(horsy_vars.protocol + horsy_vars.server_url + '/packages/new', json=request_body)
r_code = handle(r.status_code)
r = r.text
r = json.loads(r)
@@ -150,15 +150,15 @@ def upload(is_gui=False, ui=None):
print('[red]Invalid credentials[/red]')
print('Deleting auth from config')
del_auth()
request['auth'] = get_auth()
request_body['auth'] = get_auth()
print(r)
r = None
elif r_code[1] in [200, 201]:
print('[green]Success, your project is created. You can install it by running[/] '
'[i]horsy i {0}[/]'.format(request['name']))
'[i]horsy i {0}[/]'.format(request_body['name']))
return 'Success, your project is created. You can install it by running horsy i {0}'.format(
request['name'])
request_body['name'])
return r_code[0]
except: